Assignment 8: First brush with classes

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.

1. Setup

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).

2. Writing a class

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.

3. Using the class

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:

4. Turn in

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.


Thomas VanDrunen
Last modified: Tue Mar 1 18:14:26 CST 2005