Genome.java


Below is the syntax highlighted version of Genome.java from §3.3 Designing Data Types.


/******************************************************************************
 *  Compilation:  javac Genome.java
 *  Execution:    java Genome
 *
 *
 ******************************************************************************/

public class Genome {
    private char[] a = new char[2];
    private int n = 0;

    public void addNucleotide(char c) {
        if (n == a.length) resize();
        if (c == 'A' || c == 'C' || c == 'G' || c == 'T')
            a[n++] = c;
        else throw new RuntimeException("Illegal nucleotide");
    }

    // double the size of the array
    private void resize() {
        char[] temp = new char[2*n];
        for (int i = 0; i < n; i++)
            temp[i] = a[i];
        a = temp;
    }

    public char nucleotideAt(int i) {
        if (i < n) return a[i];
        else throw new RuntimeException("Genome out of bounds");
    }

    public int length() {
        return n;
    }

    // sample client for testing
    public static void main(String[] args) {
        Genome genome = new Genome();
        genome.addNucleotide('A');
        genome.addNucleotide('G');
        genome.addNucleotide('C');
        genome.addNucleotide('C');
        genome.addNucleotide('T');
        for (int i = 0; i < genome.length(); i++) {
            StdOut.print(genome.nucleotideAt(i));
        }
        StdOut.println();
    }

}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.