简体   繁体   中英

How to include a print statement from another method inside the class

My question is how do I change my code so that it prints out my first print line along with the lines printed from the testBuildCodonMap method? The print line that currently doesn't print out is System.out.println(s+"\\t"+codonMap.get(s)); . I want this to be included with the other print statements.

import java.util.*;
import edu.duke.*;

public class CodonCount {
    private HashMap<String,Integer> codonMap;
    public CodonCount() {
        codonMap = new HashMap<String,Integer>();
    }
    private void buildCodonMap(int start, String dna) {
        //clear out map before building
        codonMap.clear();
        //This method will build a new map of codons mapped to 
        //their counts from the string dna with the reading frame
        //with the position start (a value of 0, 1, or 2).
        for (int index=start; dna.length() - index > 3;index+=3) {
            String currentCodon = dna.substring(index,index+3);
            if (!codonMap.containsKey(currentCodon)) {
                codonMap.put(currentCodon,1);
            }
            else {
                codonMap.put(currentCodon,codonMap.get(currentCodon)+1);
            }
        }
    }
    private String getMostCommonCodon() {
        //get the codon in a reading frame that has the largest count
        //this method assumes the HashMap of codons to counts has already been built
        int currentHigh = 0;
        String mostCommonCodon = "";
        for (String s : codonMap.keySet()) {
            int currentCount = codonMap.get(s);
            if (currentCount > currentHigh) {
                mostCommonCodon = s;
                currentHigh = currentCount;
            }
        }
        return mostCommonCodon;
    }
    private void printCodonCounts(int start, int end) {
        //This method prints all the codons in the HashMap along with their
        //counts if their count is between start and end, inclusive.
        for (String s : codonMap.keySet()) {
            if (codonMap.get(s) >= start && codonMap.get(s) <= end) {
                System.out.println(s+"\t"+codonMap.get(s));
            }
        }
    }
    public void testBuildCodonMap() {
        FileResource fileResource = new FileResource();
        String dna = fileResource.asString();
        dna = dna.toUpperCase();

        for (int index=0;index <= 2;index++) {
            System.out.println("\nTesting with start position "+index+":\n");
            buildCodonMap(index,dna);
            String mostCommonCodon = getMostCommonCodon();
            System.out.println("Total unique codons found: "+codonMap.size());
            System.out.println("\nMost common codon: "+mostCommonCodon
                                +"\t"+codonMap.get(mostCommonCodon));
            printCodonCounts(4,8);
        }
    }
}

Sample file for testing: CGTTCAAGTTCAA

EDIT: I want the output to look something like this:

Reading frame starting with 0 results in 3 unique codons

and most common codon is TCA with count 2

Counts of codons between 1 and 5 inclusive are:

CGT 1

TCA 2

AGT 1

Reading frame starting with 1 results in 2 unique codons

and most common codon is CAA with count 2

Counts of codons between 1 and 5 inclusive are:

CAA 2

GTT 2

Reading frame starting with 2 results in 2 unique codons

and most common codon is TTC with count 2

Counts of codons between 1 and 5 inclusive are:

TTC 2

AAG 1

I got it! The line printCodonCounts(4,8); inside of the public void testBuildCodonMap() method needs to be changed to be printCodonCounts(1,3); . That allows the print statement inside of method private void printCodonCounts(int start, int end) to execute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM