6-6: Exercises
Exercise 6-14: Stopwatch Deluxe
This exercise extends our Stopwatch project.
It offers two new features:
The Deluxe model has the same look & feel as the Original. In addition to the regular controls:
this model displays a Lap
button which records the time that has elapsed since it was last
pressed. (It is assumed that it was "pressed" at time 0 -
when Start was pressed.)
The Lap button also makes the Stats checkbox visible. It, in turn, controls the visibility of the stats group of controls.
The stats group displays:
To provide room for these new displays the Form itself is resized. In fact, the size of the Form must toggle between two sizes, depending on which button was last pressed, since Reset should return the display to its original appearance.
Examining Form properties reveals the Size property. Note that this is a compound property. It has several components that can set together or seperately. Expand it to see Width and Height.
While it's running, the form is referred to as "Me", so
Me.Height=400
would make the form 400 pixels from top to bottom.
The two values that will be used for Height should be stored in Global constants for easy access by your code, and easy maintenance should it become necessary to make adjustments to the sizes chosen.
The first lap time is simply the clock value, but after that it
won't be so easy. Tracking lap times imposes several new data
requirements on the design. It will be necessary, for example,
to store the clock value when the Lap button is pressed
so that this value can be subtracted from the clock the next
time Lap is pressed. Compounding this problem is the
fact that the "clock value" actually consists of three
components.
Statistics
The first time the Lap button is pressed the Lap Counter should be set to 1, and Fastest, Average and Slowest times are all the same, so populating the text boxes should be straightforward. However, subsequent laps will require significant processing.
It will be necessary to:
There are a number of ways to find the Fastest/Slowest lap times. Perhaps the easiest is to intitialise each of them to some value and then compare this to the current lap time. It is left to you to determine what the initial values should be.
The Average lap time calculation is a bit more complex. One way would be to loop through the Items in the list box to sum the times and then divide by the number of laps. Note, however, that these Items are strings!
An alternative would be to add the current time to an accumulator and divide the total by the number of laps.