2

I am trying to calculates and returns where two DNA sequences of equal lengths differ. For example, given strings "ATGT" and "GTGA", the result should be array { true, false, false, true }. I am getting an error of False/true cannot be resolved to a variable heres what i got so far

/**
   * Calculates and returns where two DNA sequences of equal lengths differ. For
   * example, given strings "ATGT" and "GTGA", the result should be array
   * { true, false, false, true }.
   * 
   * @param dna1 a String representing a DNA sequence of arbitrary length (equal
   *          to dna2's length), containing only the characters A, C, G and T
   * @param dna2 a String representing a DNA sequence of arbitrary length (equal
   *          to dna1's length), containing only the characters A, C, G and T
   * @return an array of boolean values, of length equivalent to both
   *         parameters' lengths, containing true in each subscript where the
   *         parameter strings differ, and false where they do not differ
   */
  public static boolean[] mutationPoints(String dna1, String dna2) {
      boolean [] mutPoint =  new boolean [dna1.length()]; 
      for( int i = 0; i < i; i++) {
          if( dna1 != dna2) {
              mutPoint[i] = False; 
          }
          else if (dna1 == dna2) {
              mutPoint[i] = True; 
          }
      }
4
  • 1
    Use lower case true & false. Commented Oct 16, 2015 at 2:22
  • What are you trying to achieve by using for( int i = 0; i < i; i++) { This is always 0 Commented Oct 16, 2015 at 2:22
  • Lowercase both true and false, but the next mystery will be the loop condition ` i < i` Commented Oct 16, 2015 at 2:22
  • 1
    Use equals to compare two Strings and also your loop is mysteriously built. We never enter in it. Commented Oct 16, 2015 at 2:23

4 Answers 4

2

You need to iterate mutPoint.length times (not i times in your loop). You want to compare the characters at the i index (not the Strings). And you need to return the array. Something like,

boolean[] mutPoint = new boolean[dna1.length()];
for (int i = 0; i < mutPoint.length; i++) {
    mutPoint[i] = dna1.charAt(i) != dna2.charAt(i);
}
return mutPoint;

or like

char[] dna1arr = dna1.toCharArray();
char[] dna2arr = dna2.toCharArray();
boolean[] mutPoint = new boolean[dna1arr.length];
for (int i = 0; i < mutPoint.length; i++) {
    mutPoint[i] = dna1arr[i] != dna2arr[i];
}
return mutPoint;
Sign up to request clarification or add additional context in comments.

Comments

0

Try the following algorithm:

String dna1 = "ATGT";
        String dna2 = "GTGA";

        if(dna1.length() != dna2.length()) return;

        boolean [] mutPoint =  new boolean [dna1.length()];
        for( int i = 0; i < dna1.length(); i++) {
            if( dna1.charAt(i) == dna2.charAt(i)) {
                mutPoint[i] = false;
            }
            else {
                mutPoint[i] = true;
            }
        }

        System.out.println(Arrays.toString(mutPoint));

It prints: [true, false, false, true].

Points to note:

  1. Loop should run for length of dna.
  2. You need to check if length of two dnas is equal or not.
  3. You need to compare equality for characters in the string.

1 Comment

Thanks! I changed the loop to run length of mutPoint.length I am having issues with the logic in the condition statement, should I not use i < mutPoint.length since I am trying to get dna1 equal to the other dna2.
0

Here is a short answer :

public static boolean[] mutationPoints(String dna1, String dna2) {
          boolean [] mutPoint =  new boolean [dna1.length()]; 
          for (int i = 0 ; i < mutPoint.length ; i++){
              mutPoint[i] = dna1.charAt(i) != dna2.charAt(i);
          }
    }

Comments

0

I assumed that you need to compare the dna sequence only if they are of equal length.

public static boolean[] mutationPoints(String dna1, String dna2) {
        if(dna1.length() == dna2.length()){
          boolean [] mutPoint =  new boolean [dna1.length()]; 
          for( int i = 0; i < dna1.length(); i++) {
              if( dna1.charAt(i)== dna2.charAt(i)) {
                  mutPoint[i] = true; 
              }

             else  {
                  mutPoint[i] = false; 
              }
          }

              return mutPoint;  }

        else{
            return null;
        }
    }

    public static void main(String args[]) {
        boolean abc[]=mutationPoints("AWQA","AQQA");

    System.out.println(Arrays.toString(abc));

      }

Output:

[true, false, true, true]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.