Debug java代码
/*** This class represents a DNA sequence of nucleotides 'A', 'T', 'C' and 'G'
* as a sequence of characters in a {@link String} object.
*
* @author author name
* @version 1.0.0
*/
public class DNASequence {
private String sequence;
private int numberOfA;
private int numberOfT;
private int numberOfC;
private int numberOfG;
public static void main(String[] args) {
String s = "ATTATCGGGGTAA";
DNASequence dnaSequence = new DNASequence(s);
if ((dnaSequence.getNumberOfA() == 4)
&& (dnaSequence.getNumberOfT() == 4)
&& (dnaSequence.getNumberOfC() == 1)
&& (dnaSequence.getNumberOfG() == 4)) {
System.out.println("Test case 1: get methods passed\n");
} else {
System.out.println("Test case 1: get methods failed !!!\n");
}
if (dnaSequence.twoConsecutive('T')) {
System.out.println("Test case 2: method twoConsecutive passed\n");
} else {
System.out.println(
"Test case 2: method twoConsecutive failed !!!\n");
}
if (dnaSequence.twoConsecutive('A')) {
System.out.println("Test case 3: method twoConsecutive passed\n");
} else {
System.out.println(
"Test case 3: method twoConsecutive failed !!!\n");
}
if (!dnaSequence.twoConsecutive('C')) {
System.out.println("Test case 4: method twoConsecutive passed\n");
} else {
System.out.println(
"Test case 4: method twoConsecutive failed !!!\n");
}
}
public DNASequence(String initialSequence) {
sequence = initialSequence;
countNucleotides();
}
private void countNucleotides() {
int numberOfA = 0;
int numberOfT = 0;
int numberOfC = 0;
int numberOfG = 0;
int index = 0;
while (index < sequence.length()) {
char nucleotide = sequence.charAt(index);
if (nucleotide == 'A') {
numberOfA++;
} else if (nucleotide == 'T') {
numberOfT++;
} else if (nucleotide == 'C') {
numberOfC++;
} else if (nucleotide == 'G') {
numberOfC++;
}
index++;
}
return;
}
public int getNumberOfA( ) {
return numberOfA;
}
public int getNumberOfT( ) {
return numberOfT;
}
public int getNumberOfC( ) {
return numberOfC;
}
public int getNumberOfG( ) {
return numberOfG;
}
public boolean twoConsecutive(char input) {
int index = 1;
while (index < sequence.length() - 1) {
int indexOfFirstNucleotide = index - 1;
char firstNucleotide = sequence.charAt(indexOfFirstNucleotide);
char secondNucleotide = sequence.charAt(index);
if ((firstNucleotide == input) || (secondNucleotide == input)) {
return true;
}
index++;
}
return false;
}
}
第一次接触debug,实在不知道代码里面的bug在哪,只看出结果是错误的。麻烦各位指点一下。