public class Spiller implements Comparable { private int antallforsøk; private String spillernavn; private double tid; public Spiller(String spillernavn) { this.spillernavn = spillernavn; this.antallforsøk = 0; this.tid = 0; } public int getAntallforsøk() { return antallforsøk; } public void setAntallforsøk(int antallforsøk) { this.antallforsøk = antallforsøk; } public String getSpillernavn() { return spillernavn; } public void setSpillernavn(String spillernavn) { this.spillernavn = spillernavn; } public double getTid() { return tid; } public void setTid(double tid) { this.tid = tid; } public boolean equals(Spiller cmp_spiller) { // Kaster exception om objektet er null if(cmp_spiller==null) throw new NullPointerException("cmp_spiller is null"); if (this.getSpillernavn().equals(cmp_spiller.getSpillernavn())) return true; else return false; } public int compareTo(Object cmp_spiller) { // Kaster exception om objektet er null if(cmp_spiller==null) throw new NullPointerException("cmp_spiller is null"); Spiller spiller=(Spiller) cmp_spiller; if ((this.getTid()) == (spiller.getTid())) { return 0; } else if ((this.getTid()) > (spiller.getTid())) { return 1; } else { return -1; } } } import java.util.Arrays; import java.util.List; import java.util.Vector; public class Spillersamling { private Spiller spiller; private List samling; public Spillersamling() { samling = new Vector(); } public boolean leggTilSpiller(Spiller nyspiller) { for(int i = 0; i < samling.size(); i++) { spiller = (Spiller) samling.get(i); // Sjekker om det finnes et slikt spillerobjekt allerede if(spiller.equals(nyspiller)) return false; } samling.add(nyspiller); return true; } public void tømVector() { this.samling.clear(); } public List getVector() { return this.samling; } public void sorterSamling() { Spiller[] temp = new Spiller[samling.size()]; temp = (Spiller[]) samling.toArray(new Spiller[0]); Arrays.sort(temp); } }