Ch 2 Lab: The Temperature Class
Authors:
Ralph Morelli & Bronzell Dinkins
Trinity College, Hartford, CT
For use with Chapter 2
For Instructors
This is an ambitious lab for Chapter 2. It asks students to use the
Riddle class, discussed in Chapters 1, as a model in
building the Temperature class. They are asked to draw a UML
diagram of the Temperature class and to translated that into
a written specification. At this point (Chapter 2), we haven't really explained how methods
and parameters work, so they have to be willing to take these parts on
faith.
An optional section of the lab walks through using
command-line input with the BufferedReader object. No attempt
is made to explain how I/O works, so this too must be taken on
faith. As in preceding labs, this lab emphasizes the stepwise
refinement coding techniques.
Objectives
The objectives of this lab are:
- To give practice designing a simple Java class to convert temperatures from degrees Fahrenheit to degrees Celsius.
- To convert the design into a working Java program
- To compile, run and test the Java program
Before Lab
Read
Chapter 2 of Java, Java, Java, 3E. Read through this document
and bring your textbook to lab.
Problem Statement
Design and implement a class for a
Temperature object
that is capable of reporting its temperature in either Fahrenheit or
Celsius. This class should have one instance variable called
degrees, which contains the Fahrenheit temperature,
and two public methods, one called
getFahrenheit(),
which returns the temperature in Fahrenheit, and one called
getCelsius(), which returns the temperature in
Celsius. This method has to convert the stored temperature to Celsius
before returning it. An expression for converting Fahrenheit to
Celsius is (5 *(F -32) /9), where F is the temperature in Fahrenheit.
Test your class definition by implementing a main() method that creates Temperature
instances and displays their temperature values.
Your program should produce something like the following output in
the Java console:
The Fahrenheit temperature of thermometer1 is 20.0 degrees.
The Celsius temperature of thermometer1 is -6.67 degrees.
The Fahrenheit temperature of thermometer2 is 98.6 degrees.
The Celsius temperatureof thermometer2 is 37.0 degrees.
Problem Decomposition
This problem can be divided into one class, the
Temperature class, which will implement the
temperature conversion. It will contain a
main() method in which
Temperature instances will be created and
used.
Design
Implementation
Have your UML diagram and Design Specification checked by the Lab
Instructor or TA before beginning the implementation phase.
- Step 5. Use the stepwise refinement
approach that we practiced last time to code and test the
Temperature class. Here's a sequence of
appropriate steps, after each of which you should compile and run
your program.
- Code the initial comment block (purpose of your program, your
name, lab section, etc) and the basic class definition.
- Code the declarations for the class's instance variables as
well as the constructor method.
- Code the definition for each method, one at a time.
- Code a stub version of the main() method.
- Add code to the main method to create and use
one Temperature object. Write one access method at a
time, as well as a method call in main() that tests the method.
Here is where you would use method calls embedded within println()
expressions to display the temperature values. For example, here
are several statements that will output thermometer1's
temperature:
System. out. print(" The
Fahrenheit temperature of thermometer1 is ");
System. out.
print( thermometer1.
getFahrenheit());
System. out. println( "
degrees.");
- Add code to the main method to create and use a
second Temperature object.
Optional
NOTE: This section is required if you complete all other lab activites
before 3:30 PM.
If time and energy permits, you may want to try this optional exercise, which
makes use of Java's input class, BufferedReader.
-
Before a user can enter tempurature values from the
console, you must set the CodeWarrior Java MacOS Setting
targetRedirect stdin.

Make sure that "Redirect stdin" is set to the "Message Window" so
that Code Warrrior will take input from the keyboard as you type
it.
Incorporate the following code into your
main() method that allows the user to input
a value for a temperature.
The following statements must be incorporated into your program
(IN THE CORRECT LOCATIONS):
import java.io.*; // The IO classes must be imported
...
public static void main(String argv[]) throws IOException // Main must be revised
...
// The following code should be added to the main() method
BufferedReader input = new BufferedReader( new InputStreamReader (System.in) );
String inputString;
double temp;
// The following code is used to input a double value and store it
// in the temp variable
System.out.println("Input a temperature in Fahrenheit degrees: " );
inputString = input.readLine();
temp = Double.valueOf(inputString.trim()).doubleValue();
// The following statement could be used to create a Temperature
//object using the input value.
Temperature thermometer1 = new Temperature(temp);
Lab Checkout
Have your work checked by the lab instructor before you
leave the lab. Make sure to include your name and the lab section
in files.
You're done. Great work!