prev next

Activity 2

Now that you have the class stub for the TimeCounter class, it is time to start filling in the code for each of the methods.  Start out by filling in the constructors, the getTime()/setTime() methods, the reset() method, and the tick() method.

To help you verify that your methods are correct, we have provided you with a simple Java application called TimeConverter.  To compile our  TimeConverter application, type the following commands:

javac TimeConverter.java TimeCounter.java

If the compiler reports any errors check the code you just wrote.  Note that chances are almost 100% that the errors reported by the compiler are from the work you did in this activity, since you have already verified that the declaration syntax for the TimeCounter class was correct.  Developing a class step by step makes it easier to find and correct errors since you have a real good idea where they occur.  Once the TimeConverter program compiles, run it by typing the following command:

java TimeConverter

You should see the TimeConverter window appear on your screen.  Type in a few numbers into the TimeConverter and try pressing the increment and reset buttons.  Nothing should happen, which is okay, since you have not written the getTSeconds(), getSeconds(), getMinutes(), and getHours() methods yet.

As you can see the TimeConverter allows you to type in a number representing time in 10ths of a second and displays the corresponding time in the form HH:MM:SS.T (where HH=hours, MM=minutes, SS==seconds, and T==10ths of a second).  For example, if you type in the number 600 in the input box, the time 00:01:00.0 will appear since there are 60 seconds in a minute and 10 10ths of a second in a second (i.e. 60 times 10 is 600).  The TimeConverter also allows you to increment and reset the time it is currently displaying. If you type 135684 in the input box, the time 3:46:08.4 will appear.

You have to prepare your test data to make sure that your program is correct.

In case you have not figured it out already, the TimeConverter uses the TimeCounter class to do all of the work.  When you type in a time in 10ths of a second, the TimeConverter invokes the setTime() method in the TimeCounter to store the time you type in.  To display the time, the TimeConverter invokes the getTSeconds(), getSeconds(), getMinutes(), and getHours() methods provided by the TimeCounter class to get the values it needs to display.  The inc and reset buttons simply invoke the tick() and reset() methods.

Complete the rest of the methods in the TimeCounter class one method at a time.  After you have finished a method, compile the program and try it out.  Enter numbers in the TimeConverter to verify that your expressions are working correctly.


prev next