首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在类中包含来自另一个方法的print语句

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

Stack Overflow用户
提问于 2016-01-13 23:23:57
回答 1查看 1.1K关注 0票数 1

我的问题是如何更改我的代码,以便它打印出我的第一行打印和从testBuildCodonMap方法打印的行?目前没有打印出来的打印行是System.out.println(s+"\t"+codonMap.get(s));。我想把这个和其他打印声明一起包括进去。

代码语言:javascript
复制
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个唯一的密码子

最常见的密码子是具有计数2的TCA。

包括在1至5之间的密码子数如下:

CGT 1

TCA 2

AGT 1

从1开始的阅读框会产生两个独特的密码子

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

包括在1至5之间的密码子数如下:

CAA 2

GTT 2

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

最常见的密码子是具有计数2的TTC。

包括在1至5之间的密码子数如下:

TTC 2

AAG 1

EN

回答 1

Stack Overflow用户

发布于 2016-01-14 00:01:43

这样啊,原来是这么回事!需要将public void testBuildCodonMap()方法中的行public void testBuildCodonMap()更改为printCodonCounts(1,3);。它允许方法private void printCodonCounts(int start, int end)中的print语句执行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34778985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档