CASE STUDY
360
(^1) A listing of the Java libraries can be found in many reference books as well as on Sun’s Web site.
Notice that we made some assumptions when summarizing this class in a CRC card.
We assumed that the month, day, and year were kept—or at least returned—as
integers. If we implement the Dateclass ourselves, we might want to rethink an integer
representation for these fields. But before we spend any time writing this class
ourselves, we should look in the Java library. Surely, a date is such a common object
that a Dateclass must already exist.
Library Search^1 : Let’s begin by looking for a Date class in the Java Class Library. A Date
class sounds like it might be categorized as a utility, so we look there first. Success right
off the bat: The class Dateis a member of the java.utilpackage. A quick scan of the
methods shows that they include appropriate constructors, knowledge methods, and a
compareTomethod. However, the parameterized constructor and the knowledge
methods are marked deprecated. “Deprecated” means that they will not be supported
in future language releases. In the case of the Datemethods, they are deprecated in fa-
vor of the methods in the Calendarclass. So on we go to the Calendarclass specification.
The class Calendaris also part of the java.utilpackage. However, it is an abstract
class; that is, some of its methods are marked abstract. Does this mean that we have to
derive a class from Calendarand fill in the missing code? Maybe, but perhaps the pack-
age already offers a derived class. Yes,GregorianCalendaris listed as a subclass of
Calendar. Does the combination of the concrete methods in both of these classes give us
what we need? GregorianCalendarhas both a parameterized and a nonparameterized
constructor. It does not provide a comparison method per se, but there are Boolean
methods afterand before. A further examination of the documentation for these two
classes shows that they provide a wide variety of class constants and operations to set
and manipulate not only dates down to minutes and seconds, but also time zones.
In our problem, is it appropriate to use the class GregorianCalendaror should we build
a simple Dateclass ourselves? We use GregorianCalendarin this Case Study and ask you
to implement a new Date class in the Case Study Follow-Up exercises.
We have not yet mentioned the driver class that must read in the entries written in
Chapter 6, prompt for and read the birthday, and then write out the expanded entry.
Oops!We have forgotten that an entry as input has only three fields, but an entry that is
written out now has four fields. We must derive a new class from Entry that includes
the Birthdayfield. We can read each entry from the file and store the values into the de-
rived class. Because we plan to write the entries as objects, we need to overload the
writeToFilemethod. We had better summarize these observations in CRC cards.
T
E
A
M
F
L
Y
Team-Fly®