Build a simple digital stopwatch that keep
track of the time in tenths of a second. The stopwatch should contain
"Start", "Stop" and "Reset" buttons. To help guide you through this
project, we suggest that you download the provided program template
for this project and build your stopwatch program as follows:
Mini-project development process
- Construct a timer with an associated interval of 0.1 seconds
whose event handler increments a global integer. This integer will keep
track of the time in tenths of seconds. Test your timer by printing
the global integer to the console. To stop the timer and print
statements, use the reset button in upper left. Important: Do not
use floating point numbers to keep track of tenths of a second! While
it's certainly possible to get it working, the imprecision of floating
point can make your life miserable. Use an integer instead, i.e., 12
represents 1.2 seconds.
- Write the event handler function for the canvas that draws the
current time(simply as an integer, you should not worry about formating
it yet) in the middle of the canvas. Remember that you will need to
convert the current time into a string using
str before drawing it.
- Add "Start" and "Stop" buttons whose event handlers start and
stop the timer. Next, add a "Reset" button that stops the timer and
reset the current time to zero.
- Next, write a helper function
format(t) that returns a string of the form
A, B, C and
D are digits in the range 0-9. Test this function independent of your project. Note that your helper function
format should always draw leading zeros correctly. For example
Hint: Use integer division and remainder (modular arithmetic) to
extract various digits for the formatted time from the global integer
format(0) == 0:00.0
format(11) = 0:01.1
format(321) = 0:32.1
format(613) = 1:01.3
- Insert the
format function into your canvas event handler will complete the stopwatch.
- Finally, to turn your stopwatch into a test of reflexes, add to two
numerical counters that keep track of the number of times that you have
stopped the watch and how many times you manage to stop the watch on a
whole second. These counters should be drawn in the upper lefthand part
of the stopwatch canvas in the
x is the number of successful stops and
is number of total stops. My best effort at this simple game is around
a 25% success rate. Note that hitting the "Stop" button when the timer
is already stopped should not change your score.
- Modify "Reset" so as to set these number back to zero when clicked.
The output would look something like this.