Hello there! I've got the following assignment:
Write a method in Java called FirstDNAMatch(shortDNA, longDNA) that receives 2 strings with the letters that have to do with the four main nucleobases found in the nucleic acids DNA (A for Adenosine, G for Guanine, C for Cytosine and T for Thymine). The method must return the index of the first spot where the first DNA chain can connect to the second or -1 if there is no spot for the 2 chains to match. A matches with T (and other way round) and C matches with G (and other way round). For instance, Chain1: T A A C G G T A C G and Chain2: G C C A match on the index 3.
My Code is:
public class DNA extends ConsoleProgram{
public void run(){
println("This program receives 2 DNA chains(a combination of A, G, C, T) and evaluates whether they can actually match at a specific position(shows the position) or not(returns -1)");
println("Make sure that the 2nd chain you insert is of the same or longer length than the first please! ");
boolean flag1 = true;
boolean flag2 = true;
String shortDNA = readLine("-Insert the first DNA chain: ");
String longDNA = readLine("-Insert the second DNA chain: ");
shortDNA= shortDNA.toUpperCase();
longDNA= longDNA.toUpperCase();
while (flag2==true){
for( int i=0; i<shortDNA.length(); i++){
char ch = shortDNA.charAt(i);
if (ch == 'A'|| ch == 'G' || ch == 'C' || ch == 'T'){
flag2 = false;
}else {
println("The chains should contain only a combination of the letters A, G, C, T !!! ");
shortDNA = readLine("-Insert the first DNA chain(correct this time please!): ");
longDNA = readLine("-Insert the second DNA chain(correct this time please!): ");
shortDNA= shortDNA.toUpperCase();
longDNA= longDNA.toUpperCase();
break;
}
}
for( int i=0; i<longDNA.length(); i++){
char ch = longDNA.charAt(i);
if (ch == 'A'|| ch == 'G' || ch == 'C' || ch == 'T'){
flag2 = false;
}else {
println("The chains should contain only a combination of the letters A, G, C, T !!! ");
shortDNA = readLine("-Insert the first DNA chain(correct this time please!): ");
longDNA = readLine("-Insert the second DNA chain(correct this time please!): ");
shortDNA= shortDNA.toUpperCase();
longDNA= longDNA.toUpperCase();
break;
}
}
if (shortDNA.length()>longDNA.length()) {
println("Please insert the chain lengths correctly as requested above...(Second DNA chain should be longer than the first one.)");
shortDNA = readLine("-Insert the first DNA chain(correct this time please!): ");
longDNA = readLine("-Insert the second DNA chain(correct this time please!): ");
shortDNA= shortDNA.toUpperCase();
longDNA= longDNA.toUpperCase();
flag2=true;
}else{
flag2=false;
}
}
println(findFirstMatchingPosition(shortDNA, longDNA));
}
public static int findFirstMatchingPosition(String shortDNA, String longDNA){
char s;
char l;
boolean match = true;
int count = 1;
int counter = 0;
boolean matchFound = false;
for (int i = 0; i < shortDNA.length(); i++) {
s = shortDNA.charAt(i);
//Part 2: This part determines if the characters following the 1st matching character are also matches
if (matchFound == true) {
for (int j = count + counter; j < longDNA.length(); j++) {
l = longDNA.charAt(j);
if (s != l) {
match = false;
} else {
counter++;
matchFound = true;
break;
}
}
}
//Part1: This part determines whether the 1st character of shortDNA has a match in longDNA
//If a match is found, Part 2 happens
if (matchFound == false) {
for (int k = 0; k < longDNA.length(); k++) {
l = longDNA.charAt(k);
if (s != l) {
count ++;
matchFound = false;
} else {
matchFound = true;
break;
}
match = true;
}
}
}
if (match == true){
return count;
}else{
return -1;
}
}
}
Sadly, my code is wrong since if the first chain is ATGC and the second is GGGTACG the result is -1. Can anyone please help me? I'd appreciate it a lot...