// A class that holds ints for comparison purposes. // (c) 1996 duane a. bailey package structure; public class ComparableInt implements Comparable { protected Integer data; // where the integer is stored. public ComparableInt(int x) // post: construct an integer object that may be compared. { data = new Integer(x); } public boolean lessThan(Comparable other) // pre: other is a non-null ComparableInt // post: returns true iff the value of other is less than this { Assert.pre(other instanceof ComparableInt, "lessThan expects a ComparableInt"); ComparableInt that = (ComparableInt)other; return value() < that.value(); } public boolean equals(Object other) // pre: other is a ComparableInt // post: returns true iff other is logically equal { Assert.pre(other instanceof ComparableInt, "equals expects a ComparableInt"); ComparableInt that = (ComparableInt)other; return value() == that.value(); // alternatively: // return data.equals(otherInt.data); } public int hashCode() // post: return hashcode associated with underlying Integer. { return data.hashCode(); } public String toString() // post: return string representation of the contained integer. { return data.toString(); } public int value() // post: returns the value associated with this object { return data.intValue(); } }