[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]
Held: Monday, February 16, 1998
Integer to a Float?
(Probably yes)
Integer to a String?
Probably not, unless we wanted to convert the Integer
to a String.
compare method for Integer that
only takes Integers and Floats as parameters.
equals is supposed to take an Object
as a parameter.
instanceof operation, of the form
object instanceof class
(String) alphaalpha as a string, even if it
didn't know that alpha was a string.
String to Integer), it throws a
ClassCastException.
equals method that is implemented by Object
and that most classes are expected to override.
equals
equals that takes a "reasonable" type
as a parameter.
equals that tests the type of the
object and calls the more reasonable equals when
necessary.
public class Sample
{
/**
* The only attribute of this class.
*/
int info;
/**
* See if another Sample is equivalent to this one.
*/
public boolean equals(Sample other)
{
return other.info == info;
} // equals(Sample)
/**
* See if another object is equivalent to this Sample.
*/
public boolean equals(Object other)
{
if (other instanceof Sample) {
return equals((Sample) other);
}
else {
return false;
}
// This could be written more concisely as
// return ((other instanceof Sample) && equals((Sample) other))
} // equals(Object)
} // Sample
Comparable InterfacecompareTo
method in
java.lang.String
compareTo function like Java uses?
lessThan.
/**
* An interface to support objects that we can compare.
*
* @author Samuel A. Rebelsky
* @version 1.0 of February 1998
*/
public interface Comparable
{
/**
* Determine whether the current object is less than the parameter.
* <br>pre: The other object is of a "reasonable" class (one we
* can reasonably expect to compare to the current object).
* <br>post: Returns true if the current object (this) is less than the
* the parameter (other). Returns false if the current object
* is equal to or greater than the other object.
*
* @exception IncomparableException
* When the other object cannot reasonably be compared in this manner.
*/
public boolean lessThan(Comparable other)
throws IncomparableException;
} // Comparable
Complex class to support our new
interface?
/**
* Determine whether the current complex number is less than the parameter.
* Uses distance from the origin of the "point" corresponding to the
* complex number as the metric for comparison.
* pre: The parameter is a complex number.
* post: Returns true if the current object (this) is less than the
* the parameter (other). Returns false if the current object
* is not less than the other parameter.
*
* @exception IncomparableException
* When the other object is not a complex number.
*/
public boolean lessThan(Comparable other)
throws IncomparableException
{
if (!(other instanceof Complex)) {
throw new IncomparableException("Can't compare a Complex to a " +
other.getClass());
}
return lessThan((Complex) other);
} // lessThan(Comparable)
/**
* Compare two Complex numbers according to the description given
* above.
*/
public boolean lessThan(Complex other)
{
return this.getRadius() < other.getRadius();
} // lessThan(Complex)
lessThan
or our assumptions in designing the Comparable interface?
On to Introduction to Algorithm Analysis
Back to Experimenting with Vectors and Dictionaries
Outlines:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Current position in syllabus
[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [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/98S/home/rebelsky/public_html/Courses/CS152/98S/Outlines/outline.17.html
Source text last modified Tue Jan 12 11:52:21 1999.
This page generated on Mon Jan 25 09:49:07 1999 by SiteWeaver. Validate this page.
Contact our webmaster at rebelsky@math.grin.edu