Let's consider designing a Thermometer class.
- Class Constants
The table above gives absolute zero and the freezing and boiling points of
water using the different scales.
These data are useful constants to let users look up at any time.
Our thermometer class should declare them to be public class constants.
Next we note that the conversion equations given above use some constants
(32, 1.8 and 273.15) multiple times.
This is a sure sign that they are useful constants.
Two of them were already discussed above.
The 1.8 is the ratio between the size of Fahrenheit and Celsius degrees.
It takes 1.8 Fahrenheit degrees to make 1 Celsius degree.
It is a useful for our conversions, but is not of general
interest to the public.
It should be declared as a private class constant.
- Converting Between Temperature Scales
Look at the thermometer in the image below.
It really serves two purposes:
- It tells us what the current temperature is in either Fahrenheit or Celsius.
(It is about 63.5 °F = 17.5 °C)
- It can also be used to convert between Fahrenheit and Celsius and back.
We simply read across from one scale to the other.
For example: 40 °C = 104 °F.
We want to design a Thermometer class that can also serve both
functions.
The thermometer in the image can only convert between Celsius and Fahrenheit.
First, we note that the conversions are the same regardless of the current
temperature.
This is a sure sign that our conversion methods can be class methods.
When we define the methods we will use the static modifier.
To do the conversions, our Thermometer class
should have 2 class methods.
The method prototypes are:
- public static double celsiusToFahrenheit(double celsius)
- public static double fahrenheitToCelsius(double fahrenheit)
- Instance Variables and Methods
We also want to instantiate Thermometer objects that will keep track of
the current temperature.
Thermometers are pretty simple objects.
They keep track of the current temperature. That is about it.
Our Thermometer class will have only one data member,
currentTemperature, which we will
store as the current Celsius temperature.
It will have a number of different methods.
These methods are all instance methods since they refer to the
currentTemperature instance variable.
- It will have two constructors: a default constructor that sets the
currentTemperature to 0.0 and an alternate constructor that takes the initial
Celsius temperature as a parameter.
(Note that it is not possible in Java to have an additional constructor that
takes a Fahrenheit temperature as the initial value. Why?)
- It will have two "setter" methods:
- void setTemperatureUsingCelsius (double degreesC) which simply
sets the current temperature.
- void setTemperatureUsingFahrenheit (double degreesF) which
converts degreesF to Celsius and sets the current temperature.
- It will have two "getter" methods:
- double getCelsius () which simply returns the current temperature.
- double getFahrenheit () which converts the current temperature
to Fahrenheit and returns it.