CSC 172 - Introduction to Algorithms and Data Structures
Dr. R. M. Siegfried
Assignment #5 - Writing An Immutable Class For Time
Due Friday, February 21, 2014
An immutable class is a class that has no mutators, so there is no way to
change private properties. The way in which these are written and are used is
a little bit different.
You are going to write a class called
Time that has three private properties:
- hours - the number of hours since
midnight
- minutes
- seconds
Both minutes and seconds have the usual meaning.
Your class will have 4 constructors:
- Time() - the default constructor
which sets the time to midnight (all three properties are 0)
- Time(int newHours) - minutes and
seconds are set to zero.
- Time(int newHours, int newMinutes) -
seconds are set to zero.
- Time(int newHours, int newMinutes,
int newSeconds) - all three properties are set to the values
given.
Additionally write the following methods:
- public String toString() - which
returns a String with the time in proper 12-hour format (with AM or PM)
- public void write() - which writes the
time in 12-hour format (you can use
toString() to do this.)
- public Time later(int extraHours) -
returns a Time object
extraHours hours later.
- public Time later(int extraHours,
int extraMinutes) - returns a Time
object extraHours hours and
extraMinutes minutes later.
- public Time later(int extraHours,
int extraMinutes, int extraSeconds) - returns a
Time object
extraHours hours,
extraMinutes minutes and
extraSeconds seconds later.
Write another class containing a main program that creates a
Time object, and returns the time 4 hours,
27 minutes and 15 seconds later.
[Back to the Assignment Index]