CMPS 144L
Lab Activity: Stopping/Warning/Bounded Counters

Part A: Complete StoppingCounter and WarningCounter

You are given the Java classes Counter, ResetableCounter, BoundedCounter, and RollOverCounter, each of which is the child class of the one before it. Having read the relevant web page carefully, you should be very familiar with these classes already.

Using RollOverCounter as a model, develop the classes StoppingCounter and WarningCounter, and submit them.

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

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

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

throw new IllegalStateException();

In the case of an "illegal" increment, if you wanted to associate a relevant error message to the exception, you could say this:

throw new IllegalStateException("Cannot increment a WarningCounter at its max");

A slightly different error message would be appropriate for an "illegal" decrement.

Also provided is the Java application CounterTester, 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.)

Here is a sample dialog between a user and the CounterTester application:

Welcome to the Counter Tester program!

Enter the kind of Counter to test:
  (1) (plain) Counter
  (2) Resetable Counter
  (3) RollOver Counter
  (4) Stopping Counter
  (5) Warning Counter
> 3
Enter floor: 0
Enter ceiling: 4
Enter initial count value: 2
Initial counter value = 2
  Type i to increment, d to decrement, r to reset,
  s to setTo, p to print, q to quit, and h for this help
> p
(RollOverCounter) count value:2; floor:0; ceiling:4; initial value:2
> i
After incrementing, count is 3
> i
After incrementing, count is 4
> i
After incrementing, count is 0
> i
After incrementing, count is 1
> d
After decrementing, count is 0
> d
After decrementing, count is 4
> s
Set counter to: 3
After setting to 3, count is 3
> i
After incrementing, count is 4
> i
After incrementing, count is 0
> q
Goodbye.


Part B: Fixing BoundedCounter

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 [0..10] as its range of possible count values, will have a count value of 25. This is an unacceptable state of affairs! And the same thing could happen to an instance of any class that is a descendant of BoundedCounter.

To fix this, modify the BoundedCounter class so as to ensure that any 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 appropriate.

Test your work by using the CounterTester program to create a RollOverCounter and then trying to set it to a value outside whatever range you gave it.

Submit your updated version of BoundedCounter.