SHARE
TWEET

AlgoDatTut2

a guest Apr 25th, 2019 127 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.LinkedList;
  2. import java.util.Collections;
  3.  
  4. public class StringGenome implements Comparable<StringGenome> {
  5.  
  6.     private String s = "";
  7.  
  8.     public void addNucleotide(char c) {
  9.         if (c == 'A'|| c == 'B'|| c == 'G'|| c == 'T')
  10.             s = s + c;
  11.         else throw new RuntimeException("Illegal nucleotide");
  12.     }
  13.  
  14.     public char nucleotideAt(int i) {
  15.         if (i < s.length()) return s.charAt(i);
  16.         else throw new RuntimeException("Genome out of bounds");
  17.     }
  18.  
  19.     public int length(){ return s.length(); }
  20.  
  21.     // overwrite toString()
  22.     public String toString(){
  23.         return s;
  24.     }
  25.  
  26.     @Override
  27.     // equals() angepasst, sd es funktioniert fuer contains()
  28.     public boolean equals(Object obj){
  29.         StringGenome sg = (StringGenome) obj;
  30.         return sg.s.equals(this.s);
  31.     }
  32.  
  33.     // compareTo() fuer unsere Comparable interface
  34.     public int compareTo(StringGenome sg){
  35.         return sg.s.compareTo(this.s);
  36.     }
  37.  
  38.     //MAIN: zu testen und implementieren
  39.     public static void main(String[] args){
  40.         StringGenome genome1 = new StringGenome();
  41.         genome1.addNucleotide('A');
  42.         genome1.addNucleotide('T');
  43.         System.out.println(genome1);
  44.  
  45.         StringGenome genome2 = new StringGenome();
  46.         genome2.addNucleotide('A');
  47.         genome2.addNucleotide('T');
  48.         genome2.addNucleotide('G');
  49.         System.out.println(genome2);
  50.  
  51.  
  52.         StringGenome genome3 = new StringGenome();
  53.         genome3.addNucleotide('A');
  54.         genome3.addNucleotide('G');
  55.         genome3.addNucleotide('G');
  56.  
  57.         // equals()
  58.         System.out.println(genome1.equals(genome2));
  59.  
  60.         LinkedList<StringGenome> samples = new LinkedList<>();
  61.  
  62.         // Linked list w/ contains() method
  63.         samples.add(genome1);
  64.         samples.add(genome2);
  65.         samples.add(genome3);
  66.  
  67.         System.out.println(samples.contains(genome2));
  68.  
  69.         System.out.println("samples before: " + samples);
  70.  
  71.         Collections.sort(samples);
  72.  
  73.         System.out.println("samples after: " + samples);
  74.     }
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top