CMPS 144L Spring 2020
Lab #3: Inheritance, Polymorphism, Dynamic Method Invocation

Activity #1

Enter an initial temperature reading, which
must be a number followed by C or F: 25.5F
Calling Temperature2(25.500000,false)
Current temperature is -3.611111C (25.500000F)

Enter command (H for help): H
Examples of commands:
  S -14.5F (to set temperature to -14.5 degrees Fahrenheit)
  C 93C (to change temperature by 93 degrees Celsius)
  Q (to quit)
Current temperature is -3.611111C (25.500000F)

Enter command (H for help): C -8.4C
Calling changeBy(-8.400000)
Current temperature is -12.011111C (10.380000F)

Enter command (H for help): C 12F
Calling changeByFah(12.000000)
Current temperature is -5.344444C (22.380000F)

Enter command (H for help): S 23F
Calling setToFah(23.000000)
Current temperature is -5.000000C (23.000000F)

Enter command (H for help): S 19.8C
Calling setTo(19.800000)
Current temperature is 19.800000C (67.640000F)

Enter command (H for help): Q

Goodbye.
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 thereto) 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.

For testing your work, it is recommended that you use the Temp2Tester application. A sample dialog between it and a user is shown to the right. An alternative means for testing would be to use jGrasp's Workbench feature.


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() method should throw the same kind of exception.

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

throw new RuntimeException();

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 RuntimeException("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.)


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 [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. (Or else have it simply do nothing in such a circumstance.)

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.