CSci 101: Homework 3


Objectives. The purpose of this assignment is to become familiar with classes and objects in Java. You will design and write a class with properties and methods, and then test that class using a driver program.

Problem Define a class called Time. An object of this class is used to the time of day, in hours and minutes (include seconds if you like, but seconds aren't necessary). Your times should allow for 24 hours in the day. You'll have to decide how the time of day is to be stored. For instance, you can have one private instance variable for hours and one for minutes. Or you could just store the number of minutes since midnight. In any case these are private instance variables, and you should design your program around the public methods.

There are several public methods to include. You may have as many constructors as you like but at least one that includes two parameters, the hour of the day and the minute of the hour, like

If you use 12 hour time, you probably want another parameter to indicate a.m. or p.m. Also include accessing methods get() and set(int h, int m).

The method

should convert the Time to a string. Use whatever standard output format you prefer, like "2:45 p.m." if you like 12 hour time with a.m. and p.m., or "14:45" if you prefer 24 hour time.

So far, this class is pretty much like any other class, but the remaining methods refer to time.

Include a meth add(int h, int m) to change the time by that many hours and minutes. For instance, if the time is 2:45, and you change the time by 3 hours and 20 minutes, then the new time should be 6:05. Allow the time change to be eiter positive or negative.

You can include more methods if you like, for instance a method to tell the difference between two times.

Discussion To design this class, you should start by deciding whether you want 12 hour time with a.m. and p.m., or 24 hour time. You could do both, of course, but one is enough.

The two hard methods to program are toString() and add(). Think about some of the difficulties these methods will give you, especially, add(). If you're near the end of the day, and add enough time, then the hour should end up small (the "wee hours of the morning"). But converting to a string will present some problems, too. For instance, if the minute is less than 10, a leading zero needs to be in the string. For instance, 2:05 has a 0 preceding the 5. Also, the number of hours has to be between 1 and 12 in a 12-hour system (there's no 0 hour).

After you've thought about that, you need to decide on how to store the time, one variable or two. You could even store the time as a double, measured in hours, and the fractional part will give you minutes (and seconds). That decision is yours, but what you decide will make the rest of the programming easy or difficult.

As you write the Time class, write a test program TimeDemo, too, to make sure things are working right. Be sure your final test program tests every one of the methods in Time.

Also, as you write your Time class and test program, include documentation. It's much better to write the documentation as you write the program than to wait until the end for the documentation. Write comments for variables and methods. Each variable should be accompanied by a comment saying what it is used for, and each method should be preceded by a comment section describing the purpose, preconditions and post-conditions of that method.

Sample Execution Since you will define your own test program, there is no sample output for this assignment. However, your final test program must have at least two Time objects declared, and must exercise each method in the Time class at least once, providing appropriate output to demonstrate that the class is functioning properly.