Topical Information

This lab will help you practice with classes.

Program Information

From time to time in programming, it comes in handy to be able to determine how long it takes/took to perform a certain action(s). (See, for instance, the sort timing lab.)

You are to write a class which will allow an easy interface to the computer's timing facilities. When you first create a Timer object, it should be set to a 'stop' state. When the programmer wants to use it to time something, they can call the start() method before the 'something' executes and then call the stop() method afterward the 'something' finishes. To determine how long the 'something' took, they'll call the elapsed() method. This elapsed time will be reported in (whole) seconds.

Unfortunately, this describes extremely simple operation and may not suit all programmers' needs. To allow more general usage of the class, we'll also allow the programmer to start the timer at the object's declaration. We'll also allow the programmer to get the elapsed time whether the timer is still running or not. Finally, we'll provide an output() method so the programmer can directly print the currently elapsed time without having to retrieve it separately.

In addition, allow the programmer to subtract() and add() two Timer objects to receive a Timer object that is the difference/total of the original two Timers' currently elapsed times. (The resulting object should not be running.)

Then we have to boobie-proof the class. Make sure that things work correctly if the programmer tries to get/print the elapsed time of a Timer object which has never been started. Make sure nothing goes wrong if the programmer tries to stop a Timer object that isn't currently running.

We won't need a reset() method since the programmer can simply call start() again to start over...

Finally, place your Timer class in a library and write a test application that makes sure it is working properly. The easiest way would be to print the current system time, start the Timer, and ask the tester to wait for a while before hitting Enter. Then, when they do hit Enter, stop the Timer and print both the Timer's elapsed seconds and the system time. Then the tester can check the elapsed time for themselves. Place this interaction in a yes/no loop so the tester can repeat the testing easily by simply saying 'yes' or 'no' to your question of whether or not to continue. (Of course, IGWOS that your loop should allow the tester to enter either y/n or the full words yes/no. But don't worry about them typing 'yellow' or 'Naugahyde' or whatever.)

Thought Provoking Questions

  1. How many data members do you have? What data types are they? Where is this data type declared? (Don't forget to #include any standard libraries you need!)
  2. How many constructors do you have? What is the purpose of each (i.e. in what situation would 'the programmer' use each; or in what situation would each be used by the compiler on behalf of the programmer)?
  3. Is it okay that the mutators for the run state of the Timer are not named in the standard way? (i.e. They don't start with set_.)
  4. Is it okay that the accessor for the elapsed seconds of the Timer is not named in the standard way? (i.e. It doesn't start with get_.)
  5. Does your output() method print anything besides the elapsed seconds (even an endl)? Why should it not?
  6. Does your add() method change the value of the calling object? Should it? (Hint: Does the compiler change x when you have 'x + y' in your program? What about y?) Does/Should your add() method change the other Timer object? Does this extend to subtraction(), too?
  7. Why is an input method not a good/reasonable idea for the Timer class?
  8. Why don't you have a mutator for the elapsed time?
  9. The proposed driver/test 'app' doesn't really test all of your class' functionality, does it? Indicate specifically which methods are never tested and make suggestions to remedy these deficiencies. (Note: some methods will be explicitly tested and others will be implicitly tested. Be careful!) (Note: you need not correct the deficiencies in your program unless you choose the option below.)

This assignment is (Level 3).


Total Level Possible

If you did all above options, this lab could be worth as much as (Level 7+).