package rebelsky.hw20; /** * A collection of methods that help us at various times. * * @author Samuel A. Rebelsky * @version 1.0 of September 2004 */ public class Assistant { /** * Find the smaller of two comparable objects. * * @exception IncomparableException * when the objects cannot be compared. * @exception InconsistentException * when the objects have different senses of their orders. * @exception EqualException * when the two objects are equal */ public Comparable min(Comparable a, Comparable b) throws IncomparableException,InconsistentException,EqualException { try { if ((a.compareTo(b) < 0) && (b.compareTo(a) > 0)) return a; else if ((a.compareTo(b) > 0) && (b.compareTo(a) < 0)) return b; else if ((a.compareTo(b) == 0) && (b.compareTo(a) == 0)) throw new EqualException("neither " + a + " nor " + b + " is smaller."); else throw new InconsistentException(a + " and " + b + " have inconsistent orders."); } catch (ClassCastException cce) { throw new IncomparableException("Unable to compare " + a + " and " + b + "."); } // catch } // min(Comparable,Comparable) } // class Assistant