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

Activity #1

You are given the Temperature class, which supports the same operations as the similar class that you worked with in a previous lab. However, in this version of the class all temperature (and temperature change) values are expressed in terms of Celsius degrees.

Its child class, Temperature2, aims to improve things by providing operations that allow temperatures (and changes thereof) to be expressed in Fahrenheit degrees.

You are to complete (and submit) the source code of the Temperature2 class, which has a constructor and three methods that are "stubs" and thus are in need of modification.

It should go without saying that you are expected to test your work. A decent way to do that would be to use jGrasp's Workbench feature. Create an instance of Temperature2, apply the various methods to it, and see what values are returned by the observers and how the mutators effect the object's state.


Activity #2

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 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();

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 #3

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. One could reasonably consider this to be an unacceptable situation! 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. (Or else have it simply do nothing in such a circumstance.) Submit your updated version of BoundedCounter.