SSD 3 Practical Quiz 2 答案

本文介绍了一个用于分析DNA序列的Java类,该类能够统计序列中A、T、C、G四种核苷酸的数量,并检查特定核苷酸是否连续出现两次。通过实例展示了如何使用此类进行基本测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * This class represents a DNA sequence of nucleotides 'A', 'T', 'C' and 'G'
 * as a sequence of characters in a {@link String} object.
 *
 * @author  vivizhyy
 * @version  1.0.0
 */
public class DNASequence  {

    /* Contains the sequence of nucleotides. */
    private String  sequence;

    /* Contains the number of nucleotide A */
    private int numberOfA;

    /* Contains the number of nucleotide T */
    private int numberOfT;

    /* Contains the number of nucleotide C */
    private int numberOfC;

    /* Contains the number of nucleotide G */
    private int numberOfG;

    /**
     * Test driver for class <code>DNA</code>.
     *
     * @param args  not used.
     */
    public static void  main(String[] args)  {

        String s = "ATTATCGGGGTAA";
        DNASequence dnaSequence = new DNASequence(s);

        if ((dnaSequence.getNumberOfA() == 4)
                && (dnaSequence.getNumberOfT() == 4)
                && (dnaSequence.getNumberOfC() == 1)
                && (dnaSequence.getNumberOfG() == 4)) {
            System.out.println("Test case 1:  get methods passed/n");
        } else {
            //System.out.println("numberA = "+dnaSequence.getNumberOfA());
            //System.out.println("numberT = "+dnaSequence.getNumberOfT());
            //System.out.println("numberC = "+dnaSequence.getNumberOfC());
            //System.out.println("numberG = "+dnaSequence.getNumberOfG());
            System.out.println("Test case 1:  get methods failed !!!/n");
        }

        if (dnaSequence.twoConsecutive('T')) {
            System.out.println("Test case 2: method twoConsecutive passed/n");
        } else {
            System.out.println(
                "Test case 2: method twoConsecutive failed !!!/n");
        }

        if (dnaSequence.twoConsecutive('A')) {
            System.out.println("Test case 3: method twoConsecutive passed/n");
        } else {
            System.out.println(
                "Test case 3: method twoConsecutive failed !!!/n");
        }

        if (!dnaSequence.twoConsecutive('C')) {
            System.out.println("Test case 4: method twoConsecutive passed/n");
        } else {
            System.out.println(
                "Test case 4: method twoConsecutive failed !!!/n");
        }
    }

    /**
     * Constructs a <code>DNASequence</code> object that represents the sequence
     * of nucleotides indicated by the chars in the <code>string</code> argument
     * and counts the occurrences of each nucleotide in the sequence.
     *
     * @param initialSequence  a string with the initial sequence of nucleotides.
     */
    public  DNASequence(String initialSequence)  {

        sequence = initialSequence;
        countNucleotides();
    }

    /*
     * Counts the occurrences of each nucleotide in the DNA sequence and then
     * updates the values of the variables numberOfA, numberOfT, numberOfC, and
     * numberOfG .
     */
    private void  countNucleotides()  {

        //int numberOfA = 0;
        //int numberOfT = 0;
        //int numberOfC = 0;
        //int numberOfG = 0;

        int index = 0;

        while (index < sequence.length()) {

            char nucleotide = sequence.charAt(index);

            if (nucleotide == 'A') {
                numberOfA++;
            } else if (nucleotide == 'T') {
                numberOfT++;
            } else if (nucleotide == 'C') {
                numberOfC++;
            } else if (nucleotide == 'G') {
                numberOfG++;
            }
            index++;
        }

        return;
    }

    /**
     * Obtains the number of occurrences of nucleotide A in the sequence.
     *
     * @return  the number of occurrences of nucleotide A.
     */
    public int  getNumberOfA( )  {

        return numberOfA;
    }

    /**
     * Obtains the number of occurrences of nucleotide T in the sequence.
     *
     * @return  the number of occurrences of nucleotide T.
     */
    public int  getNumberOfT( )  {

        return numberOfT;
    }

    /**
     * Obtains the number of occurrences of nucleotide C in the sequence.
     *
     * @return  the number of occurrences of nucleotide C.
     */
    public int  getNumberOfC( )  {

        return numberOfC;
    }

    /**
     * Obtains the number of occurrences of nucleotide G in the sequence.
     *
     * @return  the number of occurrences of nucleotide G.
     */
    public int  getNumberOfG( )  {

        return numberOfG;
    }

    /**
     * Checks if the nucleotide specified by the argument occurs in two
     * consecutive locations in the DNA sequence.
     *
     * @param input a <code>char</code> that represent the nucleotide.
     * @return  <code>true</code> if the specified nucleotide occurs two
     *          consecutive locations in the sequence.
     *          Returns <code>false</code> otherwise.
     */
    public boolean  twoConsecutive(char input)  {

        int index = 1;

        //while (index < sequence.length() - 1) {
            while (index < sequence.length()) {

            int indexOfFirstNucleotide = index - 1;

            char firstNucleotide = sequence.charAt(indexOfFirstNucleotide);
            char secondNucleotide = sequence.charAt(index);

            //if ((firstNucleotide == input) || (secondNucleotide == input)) {
            if ((firstNucleotide == input) && (secondNucleotide == input)) {
                return true;

            }
            index++;
        }

        return false;
    }
}

==============================================================================================

Results:
Test case 1:  get methods failed !!!

Test case 2: method twoConsecutive passed

Test case 3: method twoConsecutive passed !!!

---------------------------------------------------------------------------
Bug #1

Description of the bug:
When the  index gets 13, and the numberOfC = 5,numberOfG = 0 ;


Lines of source code that contain the bug:
103:} else if (nucleotide == 'G') {
104:            numberOfC++;


Lines of the fixed code:
103:} else if (nucleotide == 'G') {
104:            numberOfG++;

---------------------------------------------------------------------------
Bug #2

Description of the bug:
the numberOfA=0,numberOfT=0,numberOfG=0,numberOfC=0

Lines of source code that contain the bug:
private void  countNucleotides()  {

        int numberOfA = 0;
        int numberOfT = 0;
        int numberOfC = 0;
        int numberOfG = 0;


Lines of the fixed code:
private void  countNucleotides()  {

        //int numberOfA = 0;
        //int numberOfT = 0;
        //int numberOfC = 0;
        //int numberOfG = 0;


---------------------------------------------------------------------------
Bug #3

Description of the bug:
the function twoConsecutive(char input) is used to couunt the number of
 A,G,C,T is two or not.

Lines of source code that contain the bug:
if ((firstNucleotide == input) || (secondNucleotide == input)) {


Lines of the fixed code:
if ((firstNucleotide == input) && (secondNucleotide == input)) {


---------------------------------------------------------------------------
Bug #4

Description of the bug:
the loop while(index < sequence.length() - 1)
dosen't count the last A

Lines of source code that contain the bug:
while (index < sequence.length() - 1) {


Lines of the fixed code:
while (index < sequence.length()) {


---------------------------------------------------------------------------


卡耐基梅陇的SSD课程答案 import java.io.*; import java.util.*; /* DOCUMENT THIS CLASS */ public class ShoppingCartApplication { private static BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); private static PrintWriter stdOut = new PrintWriter(System.out, true); private static PrintWriter stdErr = new PrintWriter(System.err, true); private ShoppingCart cart; /* DOCUMENT THIS PUBLIC METHOD */ public static void main(String[] args) throws IOException { ShoppingCartApplication application = new ShoppingCartApplication(); application.run(); } private void run() throws IOException { cart = new ShoppingCart(); int choice = getChoice(); while (choice != 0) { if (choice == 1) { cart.addProduct(readProduct()); } else if (choice == 2) { stdOut.println(cart.toString()); } else if (choice == 3) { stdOut.println(cart.getTotalValue()); } choice = getChoice(); } } private int getChoice() throws IOException { do { int input; try { stdErr.println(); stdErr.print("[0] Quit\n" + "[1] Add Product\n" + "[2] Display Products\n" + "[3] Display Total\n" + "choice>"); stdErr.flush(); input = Integer.parseInt(stdIn.readLine()); if (0 <= input && 3 >= input) { return input; } else { stdErr.println("Invalid choice: " + input); } } catch (NumberFormatException nfe) { stdErr.println(nfe); } } while (true); } private Product readProduct() throws IOException { final String DELIM = "_"; String name = ""; int quantity = 0; double price = 0.0; /* PLACE YOUR CODE HERE */ for (;;) { try { stdErr.print("product [name_qty_price]> "); stdErr.flush(); String s = stdIn.readLine(); StringTokenizer st = new StringTokenizer(s, DELIM); if (st.countTokens() != 3) { stdErr.println("Invalid input"); } else { name = st.nextToken(); quantity = Integer.parseInt(st.nextToken()); price = Double.parseDouble(st.nextToken()); if ((quantity >= 0) && (price >= 0.0D)) { return new Product(name, quantity, price); } stdErr.println("Invalid input"); } } catch (NumberFormatException iae) { stdErr.println(iae); } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值