Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- import java.util.Collections;
- public class StringGenome implements Comparable<StringGenome> {
- private String s = "";
- public void addNucleotide(char c) {
- if (c == 'A'|| c == 'B'|| 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(); }
- // overwrite toString()
- public String toString(){
- return s;
- }
- @Override
- // equals() angepasst, sd es funktioniert fuer contains()
- public boolean equals(Object obj){
- StringGenome sg = (StringGenome) obj;
- return sg.s.equals(this.s);
- }
- // compareTo() fuer unsere Comparable interface
- public int compareTo(StringGenome sg){
- return sg.s.compareTo(this.s);
- }
- //MAIN: zu testen und implementieren
- public static void main(String[] args){
- StringGenome genome1 = new StringGenome();
- genome1.addNucleotide('A');
- genome1.addNucleotide('T');
- System.out.println(genome1);
- StringGenome genome2 = new StringGenome();
- genome2.addNucleotide('A');
- genome2.addNucleotide('T');
- genome2.addNucleotide('G');
- System.out.println(genome2);
- StringGenome genome3 = new StringGenome();
- genome3.addNucleotide('A');
- genome3.addNucleotide('G');
- genome3.addNucleotide('G');
- // equals()
- System.out.println(genome1.equals(genome2));
- LinkedList<StringGenome> samples = new LinkedList<>();
- // Linked list w/ contains() method
- samples.add(genome1);
- samples.add(genome2);
- samples.add(genome3);
- System.out.println(samples.contains(genome2));
- System.out.println("samples before: " + samples);
- Collections.sort(samples);
- System.out.println("samples after: " + samples);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement