Develop a Java program that computes the %G~C Content of a DNA sequence. Your pr
ID: 3565383 • Letter: D
Question
Develop a Java program that computes the %G~C Content of a DNA sequence.
Your program should:
(What i have below is correct except for part 5-3)
/*File : GCContent.java*/
import java.util.Scanner;
public class GCContent {
public static void main(String args[])throws Exception{
Scanner scan = new Scanner(System.in);
int gc, tb;
gc=0; //variable to count no of G,C base
tb=0; //variable to count totals no of base in sequence
System.out.println("Enter a DNA sequence.");
String seq = scan.nextLine(); //take DNA sequence as user input
int len = seq.length(); //find length of sequence
for(int i=0;i
char b = seq.charAt(i); //extract character from sequence at index i
switch(b){
case 'G': //matches with G or C
case 'C':
++gc; //increment the count for G and C
case 'T':
case 'A':
++tb; //for all valid bases, increment count for total no of bases
break;
default: //invalid characters in sequence, error message is displayed
System.out.println("Character "+ b + " is not a valid base.");
break;
}
}
double percent = (double) (gc*100)/tb; //%G-C is calculated
System.out.println("total count of all bases = " + tb); //total no. of bases is displayed
System.out.println("the total number of As, Ts, Gs, and Cs = " + tb); //total no. of (A,T,G,C)s is displayed
System.out.println("The %G~C pair content = " + percent + " %"); //%G~C content is displayed
scan.close();
}
}
Explanation / Answer
/*File : GCContent.java
changes made are highlighted with bold fonts
*/
import java.util.Scanner;
public class GCContent {
public static void main(String args[])throws Exception{
Scanner scan = new Scanner(System.in);
int g, c, t, a;
g=c=t=a=0; //variable to count no of individual base
System.out.println("Enter a DNA sequence.");
String seq = scan.nextLine(); //take DNA sequence as user input
int len = seq.length(); //find length of sequence
for(int i=0;i<len;i++){ //run loop for each sequence length times
char b = seq.charAt(i); //extract character from sequence at index i
switch(b){
case 'G':
++g;
break;
case 'C':
++c;
break;
case 'T':
++t;
break;
case 'A':
++a;
break;
default: //invalid characters in sequence, error message is displayed
System.out.println("Character "+ b + " is not a valid base.");
break;
}
}
int tb = a + c + g + t; //calculate no of total bases
int gc = g + c; //calculate no of G-C base
double percent = (double) (gc*100)/tb; //%G-C is calculated
System.out.println("Total no of A
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.