// uses Java 5.0 Generics!!! class Fraction implements Comparable { private int n,d; // Constructors public Fraction () { // default n=0; d=1; } public Fraction (int n, int d) { // with parameters this.n = n; // this is the member variable this.d = d; } // std method for conversion to string public String toString () { return ("Fraction("+n+"/"+d+")=" + (double) n/ (double) d); } // this implements Comparable (see: Interface java.lang.Comparable) // uses Java 5.0 Generics public int compareTo(Fraction o) { double me = (double) n/ (double) d; double other = (double) o.n / (double) o.d; if (me < other) { return -1; } if (me > other) { return 1; } return 0; } }