如何在类中包含来自另一个方法的print语句

时间:2016-01-13 23:23:57

标签: java

我的问题是如何更改我的代码,以便打印出我的第一个打印行以及从testBuildCodonMap方法打印的行?当前不打印的打印行是System.out.println(s+"\t"+codonMap.get(s));。我希望将其包含在其他打印语句中。

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);
        }
    }
}

供测试的示例文件: CGTTCAAGTTCAA

编辑:我希望输出看起来像这样:

以0开头的读框导致3个独特的密码子

最常见的密码子是TCA,计数为2

1至5之间密码子的计数包括:

CGT 1

TCA 2

AGT 1

从1开始的阅读框导致2个独特的密码子

并且最常见的密码子是具有计数2的CAA

1至5之间密码子的计数包括:

CAA 2

GTT 2

从2开始的阅读框导致2个独特的密码子

最常见的密码子是TTC,计数为2

1至5之间密码子的计数包括:

TTC 2

AAG 1

1 个答案:

答案 0 :(得分:1)

我明白了! printCodonCounts(4,8);方法中的行public void testBuildCodonMap()需要更改为printCodonCounts(1,3);。 这允许方法private void printCodonCounts(int start, int end)内的print语句执行。