簡體   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