Project Part I %G~C Content Calculator Program First, you will develop a Java pr
ID: 3565381 • Letter: P
Question
Project Part I
%G~C Content Calculator Program
First, you will develop a Java program that computes the %G~C Content of a DNA sequence.
Your program should:
This is my code:
// *********************************************************
// Project.java
//
// Calculate total percent of A, T, G, and C
// *********************************************************
import java.util.Scanner;
public class Project
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
int A, T, G, C;
A = 1;
T = 1;
G = 1;
C = 1;
System.out.println ("Please enter DNA sequence: ");
String seq = scan.nextLine();
int len = seq.length();
for(int i=0;i System.out.println ();
char b = seq.charAt(i);
switch(b)
{
case 'A':
System.out.println ("Total A's: " + A);
++A;
break;
case 'T':
System.out.println ("Total T's: " + T);
++T;
break;
case 'G':
System.out.println ("Total G's: " + G);
++G;
break;
case 'C':
System.out.println ("Total C's: " + C);
++C;
break;
default:
System.out.println ("Error: A character other than A, T, C, or G was entered. ");
}
}
}
}
and this is the solution im getting:
Explanation / Answer
Since the print statement was written inside the for loop, for each itearation of the loop, the print statement was executed whenever a match for the bases was satisfied.
To get desired output, move the print statements outside the loop. Thus the print statement would succeed the loop and print the values of each variable after the counting is completed by the loop.
Also initialize you variables to 0, since initially each bases count should start from 0.
Here is the modified code. The changes made has been highlighted.
// *********************************************************
// Project.java
//
// Calculate total percent of A, T, G, and C
// *********************************************************
import java.util.Scanner;
public class Project
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
int A, T, G, C;
A = 0;
T = 0;
G = 0;
C = 0;
System.out.println ("Please enter DNA sequence: ");
String seq = scan.nextLine();
int len = seq.length();
for(int i=0;i<len;i++){
System.out.println ();
char b = seq.charAt(i);
switch(b){
case 'A':
//print statement removed
++A;
break;
case 'T':
//print statement removed
++T;
break;
case 'G':
//print statement removed
++G;
break;
case 'C':
//print statement removed
++C;
break;
default:
System.out.println ("Error: A character other than A, T, C, or G was entered. ");
}
}
//print statements are placed after the loop so that information will be printed once, and only after the counting is over
System.out.println ("Total C's: " + C);
System.out.println ("Total T's: " + T);
System.out.println ("Total A's: " + A);
System.out.println ("Total G's: " + G);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.