CMPS 144L Fall 2019
Lab #3: Inheritance, Polymorphism, Dynamic Method Invocation

For the first two activities of this lab, you are given the followng Java classes, each of which is a child class of the one before it.

Having carefully read the web page describing the Counter class hierarchy and listened intently to the relevant lectures, you should be very familiar with these classes already.


Activity #1: Develop StoppingCounter and WarningCounter

Using RollOverCounter as a model, develop the classes StoppingCounter and WarningCounter, and submit them (i.e., the corresponding .java source code files) to the Lab #3 folder.

A StoppingCounter object is to have the property that, when its count value is at its maximum, the increment() method has no effect. Similarly, when its count value is at its minimum, the decrement() method should has no effect.

A WarningCounter object is to have the property that, when its count value is at its maximum, the increment() method should throw a RuntimeException. Similarly, when its count value is at its minimum, the decrement() should throw the same kind of exception.

To throw such an exception, you can use this line of code:

throw new RuntimeException();

Slightly better, you can provide a meaningful error message, as in

throw new RuntimeException("Cannot increment when counter is at max!");

Also provided is the Java application CounterTester_F18, which should make it very convenient to test your work. (However, in order for it to create and test a StoppingCounter, you must "un-comment" a line of code in the main() method. The same applies to WarningCounter. In its current form, the program will display a message to this effect if the user tells the program to create an instance of either of those subclasses. Of course, when you un-comment the relevant line(s) of code, you should comment out (or delete) the lines of code that display the message.)


Activity #2: Make BoundedCounter Safer

Consider this code segment, found in a client of the Counter class family:

Counter rc = new RollOverCounter(0,10);
rc.setTo(25);

The result is that rc, which is supposed to be an instance of RollOverCounter having as its range of possible count values 0..10, will have a count value of 25. This is an unacceptable situation! And the same thing could happen to any instance of any class that is a descendant of BoundedCounter.

To fix this, modify the BoundedCounter class so as to ensure that an invocation of setTo() upon any instance of any of its descendant classes will not allow that object's count value to be set to a value outside its legal range. Specifically, have the setTo() method throw an IllegalArgumentException when an attempt is made to do so.

The "tester" program that you used for Activity 1 can be used for the same purpose here. Use it to create an instance of RollOverCounter (a concrete child class of BoundedCounter) and try to set its count value to something outside its range. (Also try to set its count value to something inside its range, to be sure that that works.)

When finished, submit BoundedCounter.java to the Lab #3 folder.


Activity #3: Develop MinMaxRecallCounter

Complete the development of the class MinMaxRecallCounter. An instance of this class has all the functionality of an instance of its parent class, Counter, plus it has

The MMR_Tester application can be used for testing purposes.

When finished, submit MinMaxRecallCounter.java to the Lab #3 folder.