Question
Java method help Need to fix printAll method(please keep the while (true) and break ) Then write the method named countGenes that has a String
Java method help
Need to fix printAll method(please keep the while (true) and break )
Then write the method named countGenes that has a String parameter named dna representing a string of DNA. This method returns the number of genes found in dna. For example the call countGenes(ATGTAAGATGCCCTAGT) returns 2.
Write the void method named testCountGenes that has no parameters. This method calls countGenes and prints the result for each.
public class Part1 { public int findStop(String dnaStr, int startIndex, String stopCodon){ //Find stop codon from end of ATG int currIndex=dnaStr.indexOf(stopCodon, startIndex+3); //as long as curr index not -1 while(currIndex !=-1){ //check for multiple of 3 if((currIndex - startIndex) % 3==0){ //if so, text between is substring return currIndex; } else{ //if not,Update currIndex currIndex=dnaStr.indexOf(stopCodon,currIndex+1); } } return dnaStr.length();
}
public String findGene(String dna){ //First occurrence of ATG int startIndex =dna.indexOf("ATG"); //if no ATG is found return null if(startIndex == -1){ return ""; } //stop codons to look for in index of dna string int taaIndex=findStop(dna, startIndex, "TAA"); int tagIndex=findStop(dna, startIndex, "TAG"); int tgaIndex=findStop(dna, startIndex, "TGA"); //store smallest index of ending codon values int min1=0; int min2=0;
if(taaIndex == -1 ||(tgaIndex!=-1 && tgaIndex< taaIndex)){ min2 = tgaIndex; } else{ min2 = taaIndex; } if(min2 ==-1 ||(tagIndex!=-1 && tagIndex < min2)){ min2=tagIndex; }
//is full dna string none were found so print null if(min2 == -1){ return ""; } else{ return dna.substring(startIndex, min2+3); } }
public void printGenes(String dna) { //set start index int startIndex =0; //loop while (true){ //find next gene after start index String currGene =findGene(dna); // no gene found leave loop if(currGene.isEmpty()){ break; } //Print gene out System.out.println(currGene); //Set start index to 1 past end of last gene startIndex= dna.indexOf(currGene, startIndex) + currGene.length(); } }
public void testfindStop(){ String dna="ACTATGGAAGCATAA"; System.out.println("DNA strand is "+ dna); int gene=findStop(dna,0,"TAA"); System.out.println("Gene is "+ gene);
dna="ACTATGTAAGCATGATAA"; System.out.println("DNA strand is "+ dna); gene=findStop(dna,0,"TGA"); System.out.println("Gene is "+ gene);
dna="ACTATGTACCATAACTAG"; System.out.println("DNA strand is "+ dna); gene=findStop(dna,0,"TAG"); System.out.println("Gene is "+ gene);
}
public void testfindGene(){ String dna = "ATTATGGTCTAGTAA"; System.out.println("DNA strand is "+ dna); String gene= findGene(dna); if(gene.isEmpty()){ System.out.println ("");} else{ System.out.println("Gene is "+ gene); } }
public void testprintGenes(String dna){ System.out.println("DNA strand is "+ dna); printGenes(dna); }
public void printAll(String dna){ while(true){ int index=0; int startIndex=dna.indexOf("ATG",index); System.out.println("start:" + startIndex); if(startIndex ==-1){ System.out.println(""); break; } } }
public void testprintall(){ String dna = "ATGGAATAGTCGATGGCTTAA"; System.out.println("DNA is "+ dna); printAll(dna); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started