The goal of this assignment is to become acquainted with the syntax of defining classes and objects.
Recall the example from class where we wrote a program
called Time.java
.
It used an array to model the various parts to the concept
of the time of an event.
The code presented in class is available in the class
public directory.
Your task will be to rewrite this program to use
a time class instead of an array.
As in previous assignments, move into your cs241
directory.
Make a directory for this assignment, and change into it.
> cd cs241 > mkdir assign8 > cd assign8
For this part, copy the file used in class last week from the public directory,
but rename it TimeDriver.java
:
> cp /homeemp/tvandrun/pub/241/Time.java TimeDriver.java
By using cp
in this way, giving the locationa and name
of the file you want to copy as well as a new name (in place of the dot
we've used before) makes the cp
command rename
the file as well as copy it.
Open TimeDriver.java
in xemacs and change the
documentation and opening line to reflect the new name
(as well as updating the date and the author tags).
Open a new file in xemacs. This can be done by pressing the "Open" button in the button bar and then typing the name of the new file; or, if you want it in a separate window so you can look at both at the same time, go to the "File" menu and select "open in new frame."
Call your new file Time.java
, because this will
be a class to model the type Time
.
In this new file, write a class with appropriate instance variables, so that this class will have components for year, month, date, AM/PM, hour, and minute. Make this a real improvement over the array--- instead of making them all integers, month should be a string and AM/PM should be boolean.
Compile your file Time.java
to make sure the syntax is
correct.
Now make changes to TimeDriver.java
so that it uses
your driver class instead of the old array method.
This will mean updating the main method as well as
before
, enterTime
, and toString
.
Hints:
before
will be completely different, since you can't just
loop through the members of the Time
object.
before
?
This will be much easier if you write a helper method to convert
months (as strings) to integers; that is, a method like
int monthToNumber(String month)
, which, if you give
it the String "January" will return 1. Or, you can use the method I've
provided, MonthCompare.monthToNumber(String month)
,
for partial credit (one point off).
Create the script file as before (cat, rm, compile, and run)--
both Time.java
and TimeDriver.java
.
> a2ps -P sp (the name of the script file)
(This will print "two up", meaning two pages shown next to each other
on one pice of paper. If you use a2ps
on a Java file, it
will format it nicely like in the handouts I've given in class.
The command lpr
works similarly except it does no formatting
and doesn't print two up by default.)
Then turn in the hard copy before you leave for break.