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:

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.

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.

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!