Assignment: Counters and Timers

This assignment will give you a little practice in defining and working with classes. It uses no graphics.

A Counter class

Your first task is to define a class Counter for a counter that counts down. When it gets down to 0 and counts down again, it wraps back to a limit, minus 1. For example, if the limit is 60, the counter’s value is 0, and it counts down, its next value is 59.

The Counter class must support the following methods:

Define instance variables as appropriate, though I’d be surprised if you needed anything other than three.

To pad with zeros in the __str__ method, you can determine the length of the counter’s value as a string (by calling the str function on the counter’s value), and then concatenate as many zeros as necessary (the difference between min_digits and the length of this string).

There are a couple of ways to create a string of a given number of 0s; let’s say we want to create a string of n 0s. The first way is to use a loop. Start with an empty string, and repeatedly concatenate a 0 n times. The second way uses another cool feature of Python. If you want a list or string that is just a value repeated a number of times, you can use the * operator. For example, '0' * 4 gives the string 0000, because it says to repeat the string 0 four times. If the repeat count is zero or negative, you get an empty string (which is just what you want here). This operator works for lists, too, so that [0] * 4 gives the list [0, 0, 0, 0], though you won’t need to work with lists for this assignment.

A Counter driver

A driver is a piece of code that exists solely to exercise some other piece of code. A driver is typically not interesting in and of itself.

Write a driver for the Counter class. It should create two or more Counter objects and demonstrate that all the methods in the Counter class work properly by printing to the console.

A Timer class

Your next task is to define a class Timer for a 24-hour hours:minutes:seconds timer that counts down. It must have instance variables that reference Counter objects for the hours, minutes, and seconds. Your Timer class must support the following methods:

These methods should call the appropriate methods on the appropriate Counter objects.

Important note about the tick method of the Timer class

Remember that the tick method of Counter not only ticks the Counter down, but it also returns a boolean telling you whether the Counter wrapped. If you call the tick method of Counter on the seconds counter, and this call returns True, then you need to call the tick method on the minutes counter. And if you call the tick method of Counter on the minutes counter, and this call returns True, then you need to call the tick method on the hours counter. What about if you call the tick method of Counter on the hours counter, and this call returns True? It doesn’t matter; you don’t have to do anything when the hours counter wraps.

Because each call of the tick method of Counter does two things—decrements the Counter’s value and returns a boolean telling you whether the Counter wrapped—you need exactly one call of tick for each of the three Counter objects each time you tick the Timer. I was able to write the body of the tick method of Timer in three lines, each containing a call of tick on a Counter (and possibly some other actions as well).

A Timer driver

Finally, write a driver for the Timer class. It should start a timer at 01:30:00 and count it down to 00:00:00, printing to the console all the times between and including 01:30:00 and 00:00:00.

What to turn in

  1. counter.py, containing your Counter class
  2. timer.py, containing your Timer class
  3. The .py file containing Python code for your driver for Counter
  4. The .py file containing Python code for your driver for Timer
  5. A text files (in any standard text format) containing the console output from your Counter driver
  6. A text files (in any standard text format) containing the console output from your Timer driver.

You do not need to use file operations to create this file. Please select the lines of output in PyCharm output window using your mouse and copy it into a file.

Honor Code

The consequences of violating the Honor Code can be severe. Please always keep in mind the word and spirit of the Honor Code.