Site Navigation

Site Search

CS 110: Program 5

Student Learning Objectives

Temperature class

Re-do your Lab 8's Temperature class to include

  1. mutator methods to set a Temperature object's degreesInKelvin from Celsius and from Fahrenheit scales
    • public void setDegreesFromCelsius(double celsius)
    • public void setDegreesFromFahrenheit(double fahrenheit)
  2. accessor methods to return the temperature object's degreesInKelvin in the Celsius, Fahrenheit, and Kelvin scale
    • public double getDegreesInCelsius()
    • public double getDegreesInFahrenheit()
    • public double getDegreesInKelvin()
  3. Add a Boolean method to compare two Temperature objects for equality plus or minus the margin of error and return true or false
    • public Boolean isNearlyEqual(Temperature t2)
    • Remember when comparing doubles to allow for a margin of error because of approximation errors.
      • Use a named constant to set the margin of error at 0.001
public class Temperature
{
// Declare constant value
   private final double MARGIN_OF_ERROR = 0.001;
 
// Instance variable
   private double degreesInKelvin;
   
// Constructor method: initialize degrees in Kelvin to zero
   public Temperature()
   {
      degreesInKelvin = 0;
   }
       
// Convert and save degrees in Celsius in the Kelvin scale
   public void setDegreesFromCelsius(double celsius)
   {
      // you figure out this code
   }
     
// 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)
   }
   
// Convert and return degreesInKelvin in the Celsius scale
   public double getDegreesInCelsius()
   {
      // you figure out this code
   }
         
// Convert and return degreesInKelvin in the Fahrenheit scale
   public double getDegreesInFahrenheit()
   {
       // you figure out this code
   }
   
// Return degreesInKelvin in the Kelvin scale
   public double getDegreesInKelvin()
   {
      // you figure out this code
   }
   
// Return true if the two degrees are equal, plus or minus the margin of error
   public boolean isNearlyEqual(Temperature t2)
   {
      // you figure out this code
   }
}

Tip: to compare the two temperatures in method isNearlyEqual, you can use the method getDegreesinKelvin

Since the isNearlyEqual method's parameter is named t2, to get its temperature inside method isNearlyEqual, use

t2.getDegreesInKelvin()

Inside method isNearlyEqual, to get the temperature of the first object, use

getDegreesInKelvin()

and then figure our the difference between these two degrees in Kelvin.

TemperatureDemo main method

Write a TemperatureDemo class with a main method to test your Temperature class.

Your TemperatureDemo main method should prompt the user for two temperatures (Celsius and Fahrenheit), display the equivalent temperatures in all three scalses, and then report if they are nearly equal or different.

import java.util.Scanner;
   
public class TemperatureDemo
{
   public static void main(String[] args)
   {      
   // Declare variable for user input
      double degrees;
         
   // Declare and create two Temperature objects
      Temperature temp1 = new Temperature();
      Temperature temp2 = new Temperature();
         
   // Create a Scanner object to read from the keyboard
      Scanner keyboard = new Scanner(System.in);
     
   // Get the first temperature in degrees Celsius
      System.out.print("Enter a temperature in Celsius: ");
      degrees = keyboard.nextDouble();
      temp1.setDegreesFromCelsius(degrees);

   // Get the second temperature in Fahrenheit
      // You figure out this code
     
   // Display the first temperature in all three scales
      // You figure out this code
     
   // Display the second temperature in all three scales
      // You figure out this code
     
   // Display the result of the isNearlyEqual method
      // You figure out this code
         
   }
}

Sample Session


Enter a temperature in Celsius: 100.0
Enter a temperature in Fahrenheit: 212.0001

Temperature 1 in Kelvin is         373.15
Temperature 1 in Celsius is        100.0
Temperature 1 in Fahrenheit is     212.0

Temperature 2 in Kelvin is         373.1500555555555
Temperature 2 in Celsius is        100.00005555555555
Temperature 2 in Fahrenheit is     212.0001

The two temperatures are nearly equal

Another Sample Session

Enter a temperature in Celsius: 1.666
Enter a temperature in Fahrenheit: 100.756

Temperature 1 in Kelvin is         274.816
Temperature 1 in Celsius is        1.6659999999999968
Temperature 1 in Fahrenheit is     34.998799999999996

Temperature 2 in Kelvin is         311.34777777777776
Temperature 2 in Celsius is        38.19777777777779
Temperature 2 in Fahrenheit is     100.75600000000001

The two temperatures are different

What to turn in

Grading Criteria

20 points maximum