繁体   English   中英

如何在类中包含另一个方法的打印语句

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

我的问题是如何更改代码,以使其打印出第一条打印行以及从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

三氯乙酸

AGT 1

以1开头的阅读框会产生2个唯一的密码子

最常见的密码子是带有2的CAA

1至5(含)之间的密码子计数为:

CAA 2

GTT 2

以2开头的阅读框会产生2个唯一密码子

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

1至5(含)之间的密码子计数为:

TTC 2

AAG 1

我知道了! printCodonCounts(4,8); 内部public void testBuildCodonMap()方法的内部需要更改为printCodonCounts(1,3); 这样就可以执行方法private void printCodonCounts(int start, int end)内部的print语句。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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