Site Navigation

Site Search

CS 110: Lab 8

Learning Objectives

After completion of this lab, you should be able to

Work collaboratively as a pair programming team

All labs in CS 110 will be done as pair programming teams. Your partner for today's lab is listed in the table below:

Hebeler 203
Grader: Vinh Tran; Assistant: Daniel Carpenter
Team 1
Aguilar Jr, Jaime
Hogan, Martin
Team 2
Amezcua Gutierrez, Edson
Ling, Nathan
Team 3
Baird, Owen
Nash, Chris
Team 4
Berman, Jake
Olivares, Scott
Team 5
Coudriet, Blake
Shearer, Stetson
Team 6
Erickson, Joel
Wagster, Nathan
Team 7
Goeke, Max
Smith, Stephanie
Team 8
Harris, Alexander
Awan, Samara
Team 9
Heflick, Liz
Bloom, Thomas
Team 10
McCauley, Rylee
Olden, Greg
Team 11
Millard, Ryan
Cuddington, Chris
Team 12
Barrett Wright, Matthew
Straub-Walden, Andy
Team 13
Millard, Mike
Williamson, Sarah
Team 14
Field, Sarah
Hastings, Jake
Team 15
Akana, Chris
Quayle, Weston
Fill in: Hansen, Mitchell

Note: Partners will change every week.

You may wish to review basic pair programming guidelines before you begin.

You should change roles every 10 to 15 minutes.

Use test-driven development for developing software programs

There are three common temperature scales in use today: Fahrenheit, Celsius, and Kelvin.

Converting between the scales is straightforward:

formulas (5K)

Imagine a set of temperature sensors (some based on Celsius, others based on Fahrenheit) out in the field recording temperatures that your would like to store and work with in a software program.

For consistency, the software program will convert and work with all temperatures in degrees Kelvin.

Use a calculator (Windows XP calculator works fine) to develop test cases for the following temperatures

Test Cases
Degrees in CelsiusDegrees in FahrenheitDegrees in Kelvin
100.0212.0373.15
 0.0 
  450.0
69.9999  

Write a Java class to represent one Temperature

Create the Java class Temperature in folder lab8 to represent a temperature reading in degrees Kelvin. The starting code for the class is given below:

   public class Temperature
   {
   // Instance variable
      private double degreesInKelvin; // degrees in Kelvin
   
   // Constructor method: initialize degrees in Kelvin to zero
      public Temperature()
      {
         // you figure out this code
      }
   
   // Convert and save degrees in Celsius in the Kelvin scale
      public void setDegreesFromCelsius(double celsius)
      {
         degreesInKelvin = celsius + 273.15;
      }
     
   // Convert and save degrees in Fahrenheit in the Kelvin scale
      public void setDegreesFromFahrenheit(double fahrenheit)
      {
         // you figure out this code
         // Tip: use floating point division (not integer division)
      }  
   
   // Getter method returns the degrees in Kelvin
      public double getDegreesInKelvin()
      {
         // you figure out this code
      }
   }

Create and test the class with a main method

  1. Create a new Java class, TemperatureDemo.java to test the correctness of your Temperature class. Use the test data you calculated at the start of lab for your test cases.

Note: this code is provided below. Test it and ensure that it works correctly with your Temperature class.

import java.util.Scanner;

public class TemperatureDemo
{
   public static void main(String args[])
   {
   // Declare variables
      double degrees;
      Temperature temperature1 = new Temperature();
      Temperature temperature2 = new Temperature();

   // Create a Scanner object to read from the keyboard
      Scanner keyboard = new Scanner (System.in);

   // Get the first temperature in Celsius from the user
      System.out.print("Enter a temperature in Celsius: ");
      degrees = keyboard.nextDouble();
      temperature1.setDegreesFromCelsius(degrees);

   // Display the first temperature in Kelvin
      System.out.println("Temperature 1 is " + temperature1.getDegreesInKelvin()
                          + " degrees Kelvin");

   // Get the second temperature in Fahrenheit from the user
      // You figure out this code

   // Display the second temperature in Kelvin
      // You figure out this code
   }
}
  1. Test your program by running it and verifying that the answers your program produces are correct

Sample session

Enter a temperature in Celsius: 101.2
Temperature 1 is 374.34999999999997 degrees Kelvin
Enter a temperature in Fahrenheit: 212.2
Temperature 2 is 373.26111111111106 degrees Kelvin

Use the DecimalFormat class to format double values

Use the DecimalFormat class to format double values with 3 digits to the right of the decimal point

  1. Add the import statement for java.text.DecimalFormat to the start of the TemperatureDemo class
  2. Create a DecimalFormat object, passing in the format string "#0.000" to specify formatting 3 digits to the right of the decimal point
  3. Use your formatting object to format the output to display with three digits to the right of the decimal point
Enter a temperature in Celsius: 101.2
Temperature 1 is 374.350 degrees Kelvin
Enter a temperature in Fahrenheit: 212.2
Temperature 2 is 373.261 degrees Kelvin

Compare floating point values

Two temperatures may be compared using the == operator on the values returned from getDegreesInKelvin()

if (temperature1.getDegreesInKelvin() == temperature2.getDegreesInKelvin())
{
   // do something
}

Modify your main method to display the message "The two temperatures are equal" if temperature1 (in Kelvin) is equal to temperature2 (in Kelvin). If the two temperatures are different, display the message "The two temperatures are different"

Enter a temperature in Celsius: 100.0
Temperature 1 is 373.150 degrees Kelvin
Enter a temperature in Fahrenheit: 212.0
Temperature 2 is 373.150 degrees Kelvin
The two temperatures are equal
Enter a temperature in Celsius: 100.1
Temperature 1 is 373.250 degrees Kelvin
Enter a temperature in Fahrenheit: 212.1
Temperature 2 is 373.206 degrees Kelvin
The two temperatures are different

Handle floating point approximation errors

Relational operators such as == with double values are often inaccurate due to floating point approximation errors.

For example, 0.000000001 is considered not equal to 0.000000000 when using == even though computers may be inaccurate at this degree of precision.

Modify your program so that two temperatures are considered as equal if they differ by less than or equal to a margin of error of ±0.001

Tip: the difference between two temperature may be calculated as

double difference;
difference = Math.abs(temperature1.getDegreesInKelvin() - temperature2.getDegreesInKelvin());
Enter a temperature in Celsius: 100.0001
Temperature 1 is 373.150 degrees Kelvin
Enter a temperature in Fahrenheit: 212.0001
Temperature 2 is 373.150 degrees Kelvin
The two temperatures are equal

To Receive Credit

You are allowed to drop your lowest lab grade.