CS 14 - Lab 3
In this lab you are going to implement a Time class that keeps track of
the time in either regular AM/PM notation or 24-hour (military) notation.
The Interface
The interface of a class consists of its public methods. These
are the operations that a users of the class are allowed to perform. The
Time class should support the following public methods:
Time( int hour = 0, int minute = 0, int second = 0, char AMorPM = 'M' );
void addHours( int hours );
void addMinutes( int minutes );
void addSeconds( int seconds );
void add( int hours, int minutes, int seconds );
void subtractHours( int hours );
void subtractMinutes( int minutes );
void subtractSeconds( int seconds );
void subtract( int hours, int minutes, int seconds );
int getHour();
int getMinute();
int getSecond();
char *getAMPMTime();
char *getMilitaryTime();
The Constructor
There is a constructor that allows the user to create a Time object
by passing a time in either AM/PM or military format. If the last parameter is
'A', 'a', 'P' or 'p', the constructor should assume the other three parameters
specify a time in AM/PM format. If the last parameter is anything else, it
should assume the other three parameters specify a time in military (i.e.
24-hour) format.
Note that the all of the parameters have default values. If a Time
object is created with no parameters, then, it's time is set to midnight.
What happens if a Time object is created with 3 integer parameters
(but no AMorPM parameter)?
The constructor should validate the integer parameters. If the minutes or
seconds is less than 0 or greater than 59, there is an error. If an AM/PM
time is being specified and the hour is less than 1 or greater than 12, there
is an error. If a military time is being specified and the hour is less than 0
or greater than 23, there is an error.
In the event of an error, the constructor should simply set the time to
midnight.
The add and subtract Methods
These methods are fairly straight-forward; they are used to add or subtract
hours, minutes, and/or seconds to or from a Time object. Note that if a
negative integer is passed to one of the add methods, it is the same as doing a
subtract. (Likewise, passing a negative integer to a subtract method is the
same as calling an add method.) Therefore, the subtract methods are redundant,
but are provided for the class user's convenience.
If adding to or subtracting from a Time object pushes the time past
midnight (in either direction), the time should simply be set to midnight.
For example, if the time is 11:00 PM and 3 hours are added, the new time is
past midnight, so just set the time to midnight. Likewise, if the time is
1:00 AM and 2 hours are subtracted, the time should be set to midnight.
The get Methods
The first three get methods are used to return the hour, minute, or second of
the Time object individually. The latter two get methods are used to
return a time formatted in either AM/PM or military format.
getAMPMTime should return a string formatted like: HH:MM:SS AP
HH = the hour (1-12)
MM = the minutes (0-59)
SS = the seconds (0-59)
AP = "AM" or "PM"
getMilitaryTime should return a string formatted like: HH:MM:SS AP
HH = the hour (0-23)
MM = the minutes (0-59)
SS = the seconds (0-59)
Representing the Time
So far we have discussed the Time interface, but not how to actually
represent the time itself. One obvious way would be to have 3 integer
variables; one each for the hour, minutes, and seconds. The problem with this
is that adding and subtracting can become complicated. Therefore, you should
represent the time using one integer as the number of seconds since midnight.
Of course, this variable should be private.
Note that there are 60 seconds in a minute, 60 seconds in an hour, and 24 hours
in a day. This means there are 86,400 seconds in a day. 12:00 AM (or 00:00
in military time) is second 0; 11:59 PM (or 23:59) is 86,359. When adding or
subtracting hours and/or minutes, make sure and convert them to seconds first.
The main() Function
Write a main() function to test your Time class. The program should
allocate Time objects using both AM/PM and military time formats and
call each of the add, subtract, and get methods.
Program Structure
Create a file called CS14Time.h and put your class definition in it.
Place the actual class methods in a file called CS14Time.cpp.
Finally, place your main program in a file called main.cpp. Note
that you will need to include CS14Time.h in the .cpp files using
the command:
#include "CS14Time.h"
Grading
When you have finished the program, your TA will grade it. To receive full
credit, your TA will check all of the following:
- The program works correctly
- You created the 3 files as specified
- The program is properly commented. Each function should have a comment
at the beginning explaining what it does, what each of its parameters
are for, and the return value (if any). In addition, the code within
each function should have comments as appropriate to explain how it
works.
- The program is well formatted (i.e. style). For example, you should
be consistent with your indenting in the program.