StringGenome.java
This is the syntax highlighted version of StringGenome.java
from 3.4 Encapsulation of
Introduction to Computer Science by
Robert Sedgewick and Kevin Wayne.
/*************************************************************************
* Compilation: javac StringGenome.java
* Execution: java StringGenome
*
*
*************************************************************************/
public class StringGenome {
private String s = "";
public void addNucleotide(char c) {
if (c == 'a' || c == 'c' || c == 'g' || c == 't')
s = s + c;
else throw new RuntimeException("Illegal nucleotide");
}
public char nucleotideAt(int i) {
if (i < s.length()) return s.charAt(i);
else throw new RuntimeException("Genome out of bounds");
}
public int length() { return s.length(); }
public String toString() { return s; }
// sample client for testing
public static void main(String[] args) {
StringGenome g = new StringGenome();
g.addNucleotide('a');
g.addNucleotide('g');
g.addNucleotide('c');
g.addNucleotide('c');
g.addNucleotide('t');
System.out.println(g);
for (int i = 0; i < g.length(); i++)
System.out.println(g.nucleotideAt(i));
}
}
Last updated: Fri Jun 11 16:58:23 EDT 2004
.
Copyright © 2004, Robert Sedgewick and Kevin Wayne.