CMPS 144L
Lab Activity: Complete the Temperature Class

This lab activity is intended to reacquaint you with object-oriented programming in Java. Specifically, you are to download an incomplete version of the Temperature class and to complete it. The bodies of one constructor and five methods are missing, each one having been replaced by the comment STUB!. Before trying to develop your own code for the stubbed methods, study the code that is already there, as it gives you a model to mimic. In particular, take note of the methods that are provided and make use of them, as appropriate, when completing the stubbed methods.

Welcome to the Temperature Tester program.

Enter an initial temperature reading, which
should be a number optionally followed by C or F: -5C
Calling Temperature(-5.000000,true)

toString() yields      -5.0C
toString(true) yields  -5.0C
toString(false) yields 23.0F

Enter command (H for help): M +18F
Calling modifyBy(18.000000,false)

toString() yields      5.0C
toString(true) yields  5.0C
toString(false) yields 41.0F

Enter command (H for help): S 45
Calling setTo(45.000000)

toString() yields      45.0C
toString(true) yields  45.0C
toString(false) yields 113.0F

Enter command (H for help): D 68F
Computing difference between 45.0C (113.0F) and 20.0C (68.0F)
  difference(20.0C, true) yields 25.000000
  difference(20.0C, false) yields 45.000000
  difference(20.0C) yields 25.000000

toString() yields      45.0C
toString(true) yields  45.0C
toString(false) yields 113.0F

Enter command (H for help): Q

Goodbye.

You are not to change the signature1 of any of the public methods, or to add any public methods.

To test your work, you are expected to make use of jGrasp's Workbench feature and/or the TemperatureTester application program. An example of a user/program dialog with that application is shown to the right, with input entered by the user shown in boldface.

The application begins by inviting the user to enter an initial temperature value, which is specified by a numeral optionally followed by either C or F (to indicate Celsius or Fahrenheit, respectively). (The default is Celsius.)

Thereafter, the user is expected to enter commands, each of which indicates either that the temperature is to be

Each command begins with a one-letter code —M for Modify, S for Set, or D for Difference— followed by a numeral and then, optionally, either C or F (to indicate Celsius or Fahrenheit, respectively). The Q command (suggesting "Quit") causes the program to terminate. The H command (suggesting "Help") causes a few example commands to be displayed.

Not surprisingly, these commands are translated by the application into calls to methods in the Temperature class, namely modifyBy(), setTo(), and difference().

When you are confident that you have completed the class so that each of its methods fulfills its specification, use the Dropbox feature of Brightspace to submit the source code file (i.e., the .java file, not the .class file) into the "Lab #1" folder. Your lab instructor will assist you, if necessary.

One of your goals should be to complete the class without introducing any assignments to the instance variable. That is, the only method that should include a statement of the form celsiusDeg = X (where X is some expression) is the version of setTo() that was provided to you.

Another goal should be to re-use code wherever possible, rather than repeating code. For example, if two methods have similar purposes but one of them is specific to a particular temperature scale (e.g., Celsius) and the other method handles either scale, one of those methods should call the other one. (For a good example of this, see how the argument-less version of toString() calls the one-argument version. A similar relationship holds between the two versions of setTo().)

A third goal is to make use of the provided private methods. Calls to private methods would be appropriate in the degreesFahrenheit() method and at least one of the modifyBy() methods.


Footnote

[1] For the purposes of this lab, the signature of a method is given by its return type and the list of data types of its formal parameters.