Topical Information

This lab will help you practice with classes and libraries.

Background Information

In olden times — even before I was born! — shoppers at a grocery store would keep track of their purchases with a simple hand-held counter. A click to any of four buttons would spin the gears inside so that the digit associated with that button would increment. Typically the four digits were viewed as tens, ones, tenths, and hundredths to line up with monetary needs. I recall seeing one of these at my grand-parents' house once: it was shiny red plastic with four holes through which you could view the digits and there was a $ stamped in front of the first window and a . stamped between the first and last pair of windows. (It was a way to have a small child waste several hours clicking inanely at the little buttons and thus not annoying you for this or that...*grin*)

Program Information

Design and code a class to represent this 'little red counter' concept. Your Counter should be able to hold any valid 4-digit number from 0 to 9999. When the 'hundredths' button is clicked for a Counter object that represents 9999, it should wrap to 0 and remember that it has 'overflowed'.

Methods should be provided for:

Also allow the programmer using the class to detect an overflow condition and to reset an overflow condition. (They don't need to be able to set such a state, however.) In fact, you can treat this like cin's fail flag — don't tell 'em 'til they ask. (Programmers call this a polled or asynchronous state — as opposed to an interrupt/synchronous setup.)

But why restrict ourselves to mimicking a tired old plastic device? Let's make it more general by allowing the programmer using the class to change the maximum for the Counter. This can be done as a number of digits or as a specific value — at your discretion. (The last two [base ten] digits are always to be considered the 'cents'.)

Provide the ability to display the Counter's amount in either a monetary fashion (1234 displays as $12.34; or 509 displays as $5.09) or a raw fashion (1234 displays as 1234; or 509 displays as 0509). (As above, the last two [base ten] digits are always considered the 'cents' in the monetary display.)

You do not need to provide any means of direct input from the user. The button interface of the original device is a bit complicated for us to mimic within a generic Counter class. Allow for the application to provide such an interface — or whatever they wish — instead of us.

Place your counter ADT in a library.

To make sure the Counter works correctly, write a driver (test application). Here you can provide any sort of interface you like, but a simple one would be A10's, S1's, D0.1's, and F0.01's. (Note that ASDF is right there on the keyboard together... And if you don't believe in the power of ASDF, may I suggest you check out the And, no, the .org and the .net are not the same; although the .org may be useful...somehow...)

Thought Provoking Questions

  1. How can you separate the digits of your integer member for returning portions of it from the different accessors?

  2. How can you make sure you get appropriate decimal places when displaying 'real' numbers? How can you make sure you get the right number of digits when you are displaying a '4-digit' integer?

  3. When you detect overflow, do you continue incrementing the Counter?

  4. What data type does your Overflow method return?

  5. Normally, our output methods wouldn't print anything besides the Counter's value (not even an endl!). Why does this one (note the leading 0 for 509 and the $ during a monetary-style display)?

    Could you make it a little more configurable so the programmer could specify different monetary formats/units? What new data members would that require? What other accessors, mutators, and construction patterns would be necessary?

This assignment is (Level 3).


Total Level Possible

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