






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Project; Professor: Cleaveland; Class: OBJECT-ORIENTED PROG I; Subject: Computer Science; University: University of Maryland; Term: Fall 2006;
Typology: Study Guides, Projects, Research
1 / 12
This page cannot be seen from the preview
Don't miss anything!







CMSC 131 Fall 2006Rance Cleaveland
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Project #5 Assigned!
Project due Tuesday, 10/31 at 11 pm
Project is
closed
^
You must complete the project by yourself ^
Assistance can only be provided by teaching assistants(TAs) and instructors ^
You must not look at other students' code
Start now! ^
Read entire assignment from beginning to end beforestarting to code ^
Check out assignment now from CVS ^
Follow the instructions
exactly
, as much of grading is
automated
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
min / max Revisited ^
Recall motivating example for interfaces ^
Wanted polymorphic min / max routines
Needed
compareTo
method similar to
String
One argument
Return values ^
-^
if <
0
if ==
1
if >
Can interfaces help? Yes! ^
Java includes interface
Comparable
as part of
java.lang
Interface contains one abstract method: int
compareTo(Object
o)
Return type:
int
Argument type:
Object
(We’ll see about this in a minute)
We can use this interface to redefine min / max!See
PolyMinMax.java
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Adapting Date to ImplementComparable
Must implement
compareTo
method:
int
compareTo(Object
o)
What is
Object
?
^
Type of all possible objects in any class ^
Shortcoming of (earlier) Java: no good way to say “sametype as
this
”
^
Instead: ^
Implementation must take any object ^
An error should be raised if argument is not of correct type
^
How to raise error? ^
We can use type casting ^
This is a topic for later in semester
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Using Polymorphic min / max
See
Driver.java
for last class
Note that same min / max methods used on^ Date
,^
String
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
What about
int
?
char
?
Polymorphic
min
/
max
can be used on any
class implementing
Comparable
What about primitive types (
int
,^
char
,
double
, etc.)?
They are not classes
They do not implement
Comparable
Hence
min
/
max
cannot be used on them
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
The
Integer
Wrapper
The documntation is on-line at^ http://java.sun.com/j2se/1.5.0/docs/api/
Notes^
Constructors
Implements
Comparable
^
Documentation says “Comparable
Comparable
in Java 5.0 is a
generic
interface
^
We’ll understand this more later
Has
equals
method, etc.
CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland
Wrappers Allow Use of Max /Min
What is printed as result of following:
Integer i = new Integer (1);Integer j = new Integer (2);System.out.println (PolyMinMax.min(i,j));
Answer:
1
Integer implements
Comparable
, so
compareTo
exists
It also implements
toString
, so
System.out.println
prints integer value
correctly