Instructions:Omitted
Getting Ready:Omitted
Comparator
interface. (Note: For the time being, when you see a class with
a one-letter name like T
, you should assume that it is
the Object
class.)
Comparator
interface?
public int compare(Object firstObject, Object secondObject)
public boolean equals(Object other)
DirectoryComparator
class will compile
without error even though it doesn't implement the
equals(Object)
method required by the
Comparator
interface. Why?
Object
class. However, even if it didn't,
it would compile because it is an abstract class and, as such, is not
required to implement all of its methods (even those it "promises" to
implement).
DirectoryComparator
class is abstract.
What does that mean?
DirectoryComparator
(i.e., you can't create objects that are members of the
class DirectoryComparator
).
DirectoryComparator
class
must be implemented by concrete children?
compare(File, File)
method must be implemented.
The equals(Object)
method needn't be implemented
because it is inherited from the Object
class.
DirectoryNameComparator
extends DirectoryComparator.
What method must it implement (in order to be concrete)?
compare(File, File)
method (as discussed above).
DirectoryNameComparator
implement the Comparator
interface?
main()
method of the DirectorySorter
class, the variable comparatorToUse
is declared
to be a Comparator
. Why is it possible
to assign a DirectoryNameComparator
or
a DirectoryDateComparator
to
comparatorToUse
?
DirectoryNameComparator
and
DirectoryDateComparator
implements the
Comparator
interface. Hence, for all intents
and purposes, it behaves like it "is a" Comparator
.
DirectorySorter
is executed with no command-line
arguments, the compare(Object, Object)
message
is sent to comparatorToUse
by Arrays.sort()
.
Trace the methods that are executed as a result of this message.
[Note: For each step, list the signature of the method
that is called and the class it is in.]
1. compare(Object, Object) in DirectoryComparator 2. compare(File, File) in DirectoryNameComparator
DirectoryComparator
?
compare(Object, Object)
method casts the
parameters as File
objects and calls the
compare(File, File)
method.
DirectoryComparator
class? In other words, why shouldn't we just include the
compare(Object, Object)
method in the
DirectoryDateComparator
class?
compare(Object, Object)
method in the
DirectoryComparator
class.
DirectorySorter
class contains a
containsSwitch
method. Why must this method
be static?
It is called from the main()
method which is static.
(Since main()
is static, it can be executed even if
a DirectorySorter
object does not exist. Hence,
all methods and attributes it uses must also be static.)
java.lang.Comparator
,
DirectoryComparator
,
DirectoryDateComparator
,
DirectoryNameComparator
, and
java.lang.Object
.
(NOTE: You do not have to submit your answer to this question.)
Familiarize yourself with (or refresh your memory about) these classes and interfaces. (Note: In UML, abstract methods/classes are denoted by slanted text, specialization is denoted by an arrow with a solid line, and realization/implementation of an interface is denoted by an arrow with a dashed line.)
TwoPartMeasure
class in this design? In other words, what would be bad about omitting
the abstract TwoPartMeasure
class?
Length
and Weight
.
If it were omitted, those two classes would contain duplicate code.
TwoPartMeasure
must have an
initializeUnits()
method. What must be done in this method?
smallsPerLarge
must be initialized.
TwoPartMeasure
(rather than, for example, simply including a comment that says
all subclasses must have such a method)?
initializeUnits()
method is
called in the TwoPartMeasure
class' constructor.
TwoPartMeasure
class
be declared abstract?
TwoPartMeasure
class
be declared abstract?
TwoPartMeasure
objects.
Length
and Weight
objects correspond to
real-world entities/concepts but TwoPartMeasure
objects do not.
Comparable
interface in this design?
TwoPartMeasure
and
HolidayAccount
related?
Comparable
.
HolidayAccount
extend TwoPartMeasure
?
HolidayAccount
"is a" TwoPartMeasure
.
They have no attributes in common and the only method they
have in common is compareTo()
(and, perhaps,
toString()
).
Copyright 2011