In: Computer Science
Biologists use a sequence of the letters A, C, T, and G to model a genome. A gene is a substring of a genome that starts after a triplet ATG and ends before a triplet TAG, TAA, or TGA. Furthermore, the length of a gene string is a multiple of 3, and the gene does not contain any of the triplets ATG, TAG, TAA, or TGA. Write a program that prompts the user to enter a genome and displays all genes in the genome. If no gene is found in the input sequence, display “no gene is found”. use a for loop and string to create this program.
Here are the sample runs:
Enter a genome string:
TTATGTTTTAAGGATGGGGCGTTAGTT
TTT
GGGCGT
Enter a genome string:
TGTGTGTATAT
no gene is found
package MainPackage;
import java.util.Scanner;
public class GenomeString {
   public static void main(String[] args) {
       String result = "";
       System.out.println("Enter a Genome
string:");
       Scanner s = new
Scanner(System.in);
       String genome = s.next();
       int i=0,j=0;
       /*For and while both do the same
functionality but here iam using increment of counter variable in
middle of loop so I used while */
       while(i <= genome.length()-3)
{
           //Iam iterating
through all the letters and checking whether three letters starting
with the letter matches "ATG"
          
if(genome.substring(i, i+3).equals("ATG")) {
          
    // If three letters match we need start checking
for the ending triplet of genome after third letter so added
3
          
    i = j = i+3;
          
    //Iterating through the remaining word for
ending triplet
          
    while(j <= genome.length()-3) {
          
        /* As the the gene cannot
contain "ATG", if there is ATG I will start checking for ending
triplet from the second "ATG" */
          
        if(genome.substring(j,
j+3).equals("ATG")) {
          
            i = j =
j+3;
          
        }
          
        if(genome.substring(j,
j+3).equals("TAG") || genome.substring(j, j+3).equals("TAA") ||
genome.substring(j, j+3).equals("TGA")) {
          
            result =
genome.substring(i,j);
          
           
System.out.println(result);
          
           
break;
          
        }
          
        j++;
          
    }
          
    continue;
           }
           i++;
       }
       if(result == "") {
          
System.out.println("No gene found");
       }
   }
}
Sample Outputs:-


