[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]
Back to Vectors. On to Lists, Concluded.
Held Friday, March 12
Summary
Contents
Handouts
Notes
Comparable interface>
/**
* Objects that can sometimes be compared to each other.
*
* @author Samuel A. Rebelsky
* @aversion 1.0 of March 1999
*/
public interface Comparable {
/**
* Determine if this object is ``less than'' another object.
* If the other object can't be compared to this one, then
* throws an exception.
*/
public boolean lessThan(Comparable other)
throws Exception;
/**
* Determine if this object is equal to another object.
*/
} // interface Comparable
Fraction class to support comparision of
fractions.
public class Fraction
implements Comparable
{
...
/**
* Determine if this fraction is less than another fraction.
* Precondition: Neither fraction has a denom. of 0.
* Precondition: Both fractions are simplified.
* Postcondition: Returns true if this fraction is smaller;
* returns false otherwise.
*/
public boolean lessThan(Fraction other) {
// a/b < c/d if a*d < c*b. Why? because (1) a/b = a*d/b*d,
// c/d = c*b/b*d. Since the denominators are now the same,
// we need only compare numerators.
return this.num * other.denom < other.num * this.denom;
} // lessThan(Fraction)
/**
* Determine if this fraction is less than another comparable
* object. Only successful if the other object is a Fraction.
* Otherwise, throws an exception.
* Precondition: This fraction does not have a denominator of 0.
* Precondition: The fraction is simplified.
* Precondition: If the parameter is a Fraction, then it has
* a nonzero demoninator and is simplified.
* Postcondition: If the parameter is a Fraction, and this Fraction
* is smaller than the parameter, returns true. If the parameter
* is a Fraction, and this Fraction is not smaller than the parameter,
* returns false. Otherwise, throws an exception.
*/
public boolean lessThan(Comparable other)
throws Exception
{
if (other instanceof Fraction) {
return lessThan((Fraction) other);
}
else
throw new Exception();
} // lessThan(Comparable)
...
} // class Fraction
/**
* Things that know how to compare other things.
*
* @author Samuel A. Rebelsky
* @version 1.0 of March 1999
*/
public class Comparator {
/** Compare two objects for equality. */
public boolean equals(Object alpha, Object beta) {
return alpha.equals(beta);
} // equals(Object,Object)
/**
* See if one object is less than another. Throws an
* exception if they can't be compared.
*/
public boolean lessThan(Object left, Object right)
throws Exception;
{
// Assume we can't compare them.
throw new Exception("Can't compare " + left.toString() +
" and " + right.toString());
} // lessThan(Object,Object)
} // class Comparator
/**
* Something that can compare two fractions.
*/
public class FractionComparator
implements Comparator
{
/**
* Is one fraction less than another?
*/
public boolean lessThan(Fraction left, Fraction right) {
return left.num*right.denom < right.num*left.denom;
} // lessThan(Fraction,Fraction)
/**
* Is one object less than another?
*/
public boolean lessThan(Object left, Object right)
throws Exception
{
if ((left instanceof Fraction) && (right instanceof Fraction)) {
return lessThan((Fraction) left, (Fraction) right);
}
else {
return super(left,right);
}
} lessThan(Object,Object)
} // class FractionComparator
History
Back to Vectors. On to Lists, Concluded.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99S/Outlines/outline.28.html
Source text last modified Sun Mar 14 13:55:46 1999.
This page generated on Sun Mar 14 14:29:13 1999 by SiteWeaver. Validate this page's HTML.
Contact our webmaster at rebelsky@math.grin.edu