public abstract class Grundflaeche implements Comparable { public abstract double umfang(); public abstract double flaeche(); @Override public int compareTo(Grundflaeche o) { /* * Hinweise (Vorgabe nach Java Dokumentation): * - compareTo(o)==0 <=> equals(o)==true (empfohlen) * - compareTo(null)->NullPointerException während equals(null)==false * - this.compareTo(o) < 0, wenn this vor o "einsortiert" werden sollte, * this.compareTo(o) > 0, wenn this "größer" ist als o (danach kommt) * siehe docs.oracle.com/javase/7/docs/api/java/ lang/Comparable.html */ if (o == null) throw new NullPointerException("Grundfläche wurde mit null verglichen."); // Standardmäßg: // return (int) (this.flaeche() - o.flaeche()); // Da dadurch aber bspw. 0.2 auch 0 wäre und wir nicht gegen das // vorgeschlagene Schema verstoßen wollen, lösen wir es z. B. so: double result = this.flaeche() - o.flaeche(); if (result == 0) return 0; // equals == true else if (result < 1 && result > -1) { // -> würde zu 0 werden if (result < 0) return -1; else return +1; } else return (int) result; } @Override public String toString() { return "GF{umfang=" + umfang() + "; flaeche=" + flaeche() + "}"; } }