Lab exercise #5: Inheritance and abstraction

Course links

External links

Part 1: Talliers

Often, at a large public event, some hireling of the promoter will stand at the entrance and count the number of attendees by repeatedly clicking a button on a small hand-held mechanism: a tallier. This mechanism has only three functions: a reset that sets its internal counter to 0, a tally that adds 1 to the internal counter when the button is clicked, and a report function that recovers the value of the internal counter, i.e., the number of tally operations performed since the last reset operation.

  1. In Eclipse, create a new package called tallying for the classes we develop in this lab.
  2. In this new package, create a Tallier class that supports these three functions, implementing each as a non-static method. How many fields, if any, will a Tallier need?
  3. Add a main method that creates two talliers and sends them messages demonstrating that they can count independently of one another.

Part 2: Deluxe talliers

The designers at UPCorp (formerly Useless Products, Inc.) have designed a new kind of tallier with an additional function: When it is touched back-to-back to any other UPCorp tallier, the contents of the internal counter of the other tallier are automatically added to its own internal counter. (For example, if one of these "deluxe talliers" reads 389, and it is touched to another tallier that reads 114, the deluxe tallier's internal counter will change to 503.)

  1. Write a DeluxeTallier class that extends Tallier and models this additional function in the form of a new collect method that takes the other tallier as an argument.
  2. What should happen when a DeluxeTallier is touched back-to-back to another DeluxeTallier?
  3. The testers at UPCorp found that what actually happens in the preceding case is that both of the deluxe talliers set their internal counters to the sum of their former values. Revise your collect method to ensure that this happens in your model whenever the argument to collect is a DeluxeTallier. You may find that you need to provide an additional method to make this work.
  4. Write a main method for the DeluxeTallier class that checks to make sure that the collect works as it is supposed to, both with non-deluxe talliers and with deluxe ones.

Part 3: Exotic talliers

For the new model year, the designers at UPCorp want to develop talliers that can not only report their internal counter readings as int values, but can convert the reading to a String and display it on a small LCD screen on the front of the tallier. All DeluxeTalliers will provide this function in some form, but the particular string that is displayed will be different in different markets: The USDeluxeTallier will use standard decimal numeration and Arabic numerals, the ChinaDeluxeTallier will use Han ideographs, and the AldebaranDeluxeTallier, for the UFO-pilot market, will use base-eight numeration (with Arabic numerals).

  1. Change DeluxeTallier to an abstract class with an abstract show method that takes no arguments and returns a String.
  2. Implement the USDeluxeTallier and AldebaranDeluxeTallier classes as extensions of DeluxeTallier, each providing its own overriding show method.
  3. Could this additional function also be implemented by having a separate Showable interface, containing just the abstract show method? What would be the advantages and disadvantages of such an approach?