I\'m trying to figure out how to change the rounding in the method getSumOfAllMa
ID: 3902237 • Letter: I
Question
I'm trying to figure out how to change the rounding in the method getSumOfAllMasses. the output is showing the correct number but all decimals are rounded to .0 this needs to be more specific like .3 or .8
import java.util.*;
import java.io.*;
/** This program scans data from a file given by the user containing coded DNA information. The DNA information
* is reported in a nucleotide sequence with each letter being A, C, G, or T. By checking the frequency and order
* of occurrence this program verifies if the DNA strings are real proteins. It also calculates the mass
* percentage based on known weights of each nucleotide. The program will put its findings in a newly created
* file that is named by the user.
*
* @author Cecil
*
*/
public class DNA3 {
/** Main entry point for class DNA
*
* @param args the arguments to main
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
System.out.println("This program reports information about DNA");
System.out.println("neuclotide sequences that may encode proteins.");
System.out.print("Input file name? ");
String inputFileName = console.nextLine();
Scanner input = new Scanner(new File(inputFileName));
System.out.print("Output file name? ");
String outputFileName = console.nextLine();
PrintStream outputFile = new PrintStream(outputFileName);
while(input.hasNextLine()) {
String iDtext = input.nextLine();
String nucleotides = input.nextLine();
String nucleotidesNew = nucleotides.toUpperCase();
double junksCount = getJunksCount(nucleotidesNew);
int[] nucleotidesCounts = getNucleotidesCounts(nucleotidesNew);
double[] massValues = new double[4];
double sum = getSumOfAllMasses(nucleotidesCounts, junksCount, massValues);
String[] codons = getCodons(nucleotidesNew);
String isProtein = findItsProtein(codons, massValues);
printResults(outputFile, iDtext, nucleotidesNew, nucleotidesCounts, massValues,
sum, codons, isProtein);
}
}
/** This method totals each nucleotide and returns their value to be used for
* the total weight calculations.
*
* @param nucleotidesNew counts each nucleotide
* @return counter gives the total count of each specific nucleotide
*/
public static int[] getNucleotidesCounts(String nucleotidesNew) {
int[] counter = new int[4];
for(int i = 0; i < nucleotidesNew.length(); i++) {
if(nucleotidesNew.charAt(i)==('A')) {
counter[0]++;
} else if(nucleotidesNew.charAt(i) == 'C') {
counter[1]++;
} else if(nucleotidesNew.charAt(i) == 'G') {
counter[2]++;
} else if(nucleotidesNew.charAt(i) == 'T') {
counter[3]++;
}
}
return counter;
}
/** Within the DNA strings are "junk" values represented with a "-". This method
* makes sure they get counted for weight but not to mess up the order of the codons.
*
* @param nucleotidesNew counts each nucleotide
* @return total count of nucleotides
*/
public static double getJunksCount(String nucleotidesNew) {
double total = 0.0;
for(int i = 0; i < nucleotidesNew.length(); i++) {
if(nucleotidesNew.charAt(i) == '-'){
total += 100.00;
}
}
return total;
}
/** Takes each count to get a total sum of the weight of the DNA code.
*
* @param nucleotidesCount
* @param junkCount
* @param massValues
* @return value of junk
*/
public static double getSumOfAllMasses(int[] nucleotidesCount, double junkCount, double[] massValues) {
double total = junkCount;
double masses[] = {135.128, 111.103, 151.128, 125.107};
for(int i = 0; i < massValues.length; i++) {
massValues[i] = (double)nucleotidesCount[i] * masses[i];
total += (double)nucleotidesCount[i] * masses[i];
}
for(int i = 0; i < massValues.length; i++) {
massValues[i] = Math.round((massValues[i] / total) * 1000.0 / 10.0);
}
return Math.round(total * 10.0) / 10.0;
}
/** Gathers the nucleotides in sets of three while accounting for the junk
* data as well.
*
* @param nucleotidesNew total count of each nucleotide
* @return Grouped nucleotides that could be codons.
*/
public static String[] getCodons(String nucleotidesNew) {
String junk = "";
String str = "";
int index = 0;
for(int i = 0; i < nucleotidesNew.length(); i++) {
if(nucleotidesNew.charAt(i) != '-') {
str += nucleotidesNew.charAt(i);
} else
junk += nucleotidesNew.charAt(i);
}
String[] codons = new String[str.length() / 3];
for(int i = 0; i < str.length(); i = i+3) {
String str1 = str.substring(i, i+3);
codons[index]= str1;
index++;
}
return codons;
}
/** Validates if the codon is a protein. Based on nucleotide order and total mass.
*
* @param codons string of 3 nucleotides in a specific order
* @param massValues total weight of the DNA string (including junk)
* @return Yes if both the order of nucleotides and total weight is within the range
* @return No if either the order of nucleotides or total weight is out of range
*/
public static String findItsProtein(String[] codons, double[] massValues) {
if((codons[0].equals("ATG"))) {
if(codons.length >= 5 && (massValues[1] + massValues[2]) >= 30.0) {
if(codons[codons.length - 1].equals("TAA") || codons[codons.length - 1].equals("TAA") || codons[codons.length - 1].equals("TGA")){
return "Yes";
}
}
}
return "No";
}
/** Gathers all the parameters and complies it into a logical order for presentation while also
* inputing all the information into a new file specified by the user.
*
* @param outputFile name of output file given by user
* @param iDOfDNA name of DNA string from file
* @param nucleotidesNew changed letters to upper case
* @param nucleotidesCounts totals of each nucleotide
* @param massValues total weight of each nucleotide
* @param sum combined weight of nucleotides and junk
* @param codons readout of all valid codons
* @param isProtein validates if codons and mass values match protein characteristics
*/
public static void printResults(PrintStream outputFile, String iDOfDNA, String nucleotidesNew, int[] nucleotidesCounts, double[] massValues, double sum, String[] codons, String isProtein) {
outputFile.println("Region Name: " + iDOfDNA);
outputFile.println("Nucleotides: " + nucleotidesNew);
outputFile.println("Nuc. Counts: " + Arrays.toString(nucleotidesCounts));
outputFile.println("Total Mass: " + Arrays.toString(massValues) + "of" +sum);
outputFile.println("Codons List: " + Arrays.toString(codons));
outputFile.println("Is Protein?: " + isProtein);
outputFile.println();
}
}
Explanation / Answer
import java.util.*;
import java.io.*;
/** This program scans data from a file given by the user containing coded DNA information. The DNA information
* is reported in a nucleotide sequence with each letter being A, C, G, or T. By checking the frequency and order
* of occurrence this program verifies if the DNA strings are real proteins. It also calculates the mass
* percentage based on known weights of each nucleotide. The program will put its findings in a newly created
* file that is named by the user.
*
* @author Cecil
*
*/
public class DNA3 {
/** Main entry point for class DNA
*
* @param args the arguments to main
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
System.out.println("This program reports information about DNA");
System.out.println("neuclotide sequences that may encode proteins.");
System.out.print("Input file name? ");
String inputFileName = console.nextLine();
Scanner input = new Scanner(new File(inputFileName));
System.out.print("Output file name? ");
String outputFileName = console.nextLine();
PrintStream outputFile = new PrintStream(outputFileName);
while(input.hasNextLine()) {
String iDtext = input.nextLine();
String nucleotides = input.nextLine();
String nucleotidesNew = nucleotides.toUpperCase();
double junksCount = getJunksCount(nucleotidesNew);
int[] nucleotidesCounts = getNucleotidesCounts(nucleotidesNew);
double[] massValues = new double[4];
double sum = getSumOfAllMasses(nucleotidesCounts, junksCount, massValues);
String[] codons = getCodons(nucleotidesNew);
String isProtein = findItsProtein(codons, massValues);
printResults(outputFile, iDtext, nucleotidesNew, nucleotidesCounts, massValues,
sum, codons, isProtein);
}
}
/** This method totals each nucleotide and returns their value to be used for
* the total weight calculations.
*
* @param nucleotidesNew counts each nucleotide
* @return counter gives the total count of each specific nucleotide
*/
public static int[] getNucleotidesCounts(String nucleotidesNew) {
int[] counter = new int[4];
for(int i = 0; i < nucleotidesNew.length(); i++) {
if(nucleotidesNew.charAt(i)==('A')) {
counter[0]++;
} else if(nucleotidesNew.charAt(i) == 'C') {
counter[1]++;
} else if(nucleotidesNew.charAt(i) == 'G') {
counter[2]++;
} else if(nucleotidesNew.charAt(i) == 'T') {
counter[3]++;
}
}
return counter;
}
/** Within the DNA strings are "junk" values represented with a "-". This method
* makes sure they get counted for weight but not to mess up the order of the codons.
*
* @param nucleotidesNew counts each nucleotide
* @return total count of nucleotides
*/
public static double getJunksCount(String nucleotidesNew) {
double total = 0.0;
for(int i = 0; i < nucleotidesNew.length(); i++) {
if(nucleotidesNew.charAt(i) == '-'){
total += 100.00;
}
}
return total;
}
/** Takes each count to get a total sum of the weight of the DNA code.
*
* @param nucleotidesCount
* @param junkCount
* @param massValues
* @return value of junk
*/
public static double getSumOfAllMasses(int[] nucleotidesCount, double junkCount, double[] massValues) {
double total = junkCount;
double masses[] = {135.128, 111.103, 151.128, 125.107};
for(int i = 0; i < massValues.length; i++) {
massValues[i] = (double)nucleotidesCount[i] * masses[i];
total += (double)nucleotidesCount[i] * masses[i];
}
for(int i = 0; i < massValues.length; i++) {
massValues[i] = (double) Math.round((massValues[i] / total) *10) / 10.0;
}
return (double) Math.round((total * 10)) / 10.0;
}
/** Gathers the nucleotides in sets of three while accounting for the junk
* data as well.
*
* @param nucleotidesNew total count of each nucleotide
* @return Grouped nucleotides that could be codons.
*/
public static String[] getCodons(String nucleotidesNew) {
String junk = "";
String str = "";
int index = 0;
for(int i = 0; i < nucleotidesNew.length(); i++) {
if(nucleotidesNew.charAt(i) != '-') {
str += nucleotidesNew.charAt(i);
} else
junk += nucleotidesNew.charAt(i);
}
String[] codons = new String[str.length() / 3];
for(int i = 0; i < str.length(); i = i+3) {
String str1 = str.substring(i, i+3);
codons[index]= str1;
index++;
}
return codons;
}
/** Validates if the codon is a protein. Based on nucleotide order and total mass.
*
* @param codons string of 3 nucleotides in a specific order
* @param massValues total weight of the DNA string (including junk)
* @return Yes if both the order of nucleotides and total weight is within the range
* @return No if either the order of nucleotides or total weight is out of range
*/
public static String findItsProtein(String[] codons, double[] massValues) {
if((codons[0].equals("ATG"))) {
if(codons.length >= 5 && (massValues[1] + massValues[2]) >= 30.0) {
if(codons[codons.length - 1].equals("TAA") || codons[codons.length - 1].equals("TAA") || codons[codons.length - 1].equals("TGA")){
return "Yes";
}
}
}
return "No";
}
/** Gathers all the parameters and complies it into a logical order for presentation while also
* inputing all the information into a new file specified by the user.
*
* @param outputFile name of output file given by user
* @param iDOfDNA name of DNA string from file
* @param nucleotidesNew changed letters to upper case
* @param nucleotidesCounts totals of each nucleotide
* @param massValues total weight of each nucleotide
* @param sum combined weight of nucleotides and junk
* @param codons readout of all valid codons
* @param isProtein validates if codons and mass values match protein characteristics
*/
public static void printResults(PrintStream outputFile, String iDOfDNA, String nucleotidesNew, int[] nucleotidesCounts, double[] massValues, double sum, String[] codons, String isProtein) {
outputFile.println("Region Name: " + iDOfDNA);
outputFile.println("Nucleotides: " + nucleotidesNew);
outputFile.println("Nuc. Counts: " + Arrays.toString(nucleotidesCounts));
outputFile.println("Total Mass: " + Arrays.toString(massValues) + "of" +sum);
outputFile.println("Codons List: " + Arrays.toString(codons));
outputFile.println("Is Protein?: " + isProtein);
outputFile.println();
}
}
Output:
237.678 -> 237.7
234.4456 -> 234.4