Cmpt 225

Simon Fraser University
School of Computing Science

Cmpt 225 - Activity


Considering the incomplete Java class definition Temperature.java found in this handout, answer the following 10 questions.

For questions 1 to 6, write your answer in the space provided below the question.

1. Name one getter method you could add to the class Temperature.
 
 

2. Write a statement in Java that creates an array of 50 references to Temperature objects.
 
 

3. Is the class invariant provided appropriate when a Temperature object is instantiated using the default constructor? Briefly explain.
 
 
 
 

4. Why are the fields (variables) of the Temperature class private?
 
 
 

5. Name three fundamental (or basic) data types you find in the Java language.
 
 
 
 
 

6. You are to write a Temperature Converter application that transforms Celsius temperatures into Fahrenheit temperatures and you are to use the Temperature class given in this handout. Write an algorithm for the main method of this Temperature Converter class.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

For questions 7 to 10, write your answer directly on the Temperature class definition handout, in the appropriate space provided.
 

7. Write a class description for this Temperature class.

8. Write a POST condition for the method  public Temperature raise(double amount).

9. Fill in the missing parameters for one of the constructors of the Temperature class.

10. Add a PRE condition to the method

  public static boolean isValidTemperature(double theDegrees, Scale theScale)



/*
 * Temperature.java
 * Created on August 8, 2006
 * Class Description:_____________________________________________________
 *
 *                   _____________________________________________________
 *
 *                   _____________________________________________________
 *
 * Class Invariant: myScale is Celsius && myDegrees >= ABSOLUTE_ZERO_CELSIUS ||
 *                  myScale is Fahrenheit && myDegrees >= ABSOLUTE_ZERO_FAHRENHEIT
 */

import java.io.*;

public class Temperature {

   enum Scale {
       Fahrenheit, Celsius;
   }

   public final static double ABSOLUTE_ZERO_FAHRENHEIT = -459.67;
   public final static double ABSOLUTE_ZERO_CELSIUS    = -273.15;

   //--- Data Members ---
   private double myDegrees;       // >= ABSOLUTE_ZERO for myScale
   private Scale myScale;          // Fahrenheit or Celsius

   //--- Constructors ---
   // Postcondition: myDegrees == 0.0 && myScale is Celsius
   public Temperature() {
      myDegrees = 0.0;
      myScale = Scale.Celsius;
   }

   // Precondition: the scale is either Fahrenheit or Celsius
   //               and the degrees is a valid number of degrees for scale.
   // Postcondition: myDegrees == degrees && myScale is either Fahrenheit or Celsius,
   //                if temperature valid
   //                otherwise myDegrees == 0.0 && myScale is Celsius.
   public Temperature(_______________________, __________________________) {
      if (isValidTemperature(theDegrees, theScale)) {
         myDegrees = theDegrees;
         myScale = theScale;
      } else {
         System.out.println("Temperature(theDegrees, theScale): invalid args: " +
                 theDegrees + " " + theScale);
         System.out.println("Temperature is set to 0.0 " + theScale);
         myDegrees = 0.0;
         myScale = theScale;
      }
   }

   //--- Method Members ---

   // Description: Fahrenheit converter
   // Postcondition: Fahrenheit equivalent to myself is returned   //
   public Temperature inFahrenheit() {
      Temperature result = null;
      if (myScale == Scale.Fahrenheit) {
         result = new Temperature(myDegrees, myScale);
      } else if (myScale == Scale.Celsius) {
           result = new Temperature(myDegrees * 1.8 + 32.0, Scale.Fahrenheit);
      }
      return result;
   }

   // Description: Celsius converter
   // Postcondition: Celsius equivalent to myself is returned
   public Temperature inCelsius() {
      Temperature result = null;
      if (myScale == Scale.Celsius) {
         result = new Temperature(myDegrees, myScale);
      } else if (myScale == Scale.Fahrenheit) {
            result = new Temperature((myDegrees - 32.0) / 1.8, Scale.Celsius);
      }
      return result;
   }

   // Description: Raise by an amount in degrees of my scale.
   // Precondition: myDegrees + amount produces a valid magnitude
   //               for a Temperature whose scale is myScale.
   // Postcondition:
   //
   public Temperature raise(double amount) {
      double newDegrees = myDegrees + amount;
      if (!isValidTemperature(newDegrees, myScale))
         System.out.println("raise(double)" + newDegrees + " " + myScale +
                             " is not a valid temperature");
      return new Temperature(newDegrees, myScale);
   }

   // Description: Temperature validation utility.
   // Precondition: _____________________________________________________
   //
   //               _____________________________________________________
   // Postcondition: true if (theDegrees, theScale) represents a valid temperature,
   //                false otherwise.
   public static boolean isValidTemperature(double theDegrees, Scale theScale) {
      if (theScale ==  Scale.Celsius) {
         return theDegrees >= ABSOLUTE_ZERO_CELSIUS;
      } else if (theScale == Scale.Fahrenheit) {
         return theDegrees >= ABSOLUTE_ZERO_FAHRENHEIT;
      } else {
        return false;
      }
   }

   public static void main(String[] args) {
      // some code here
   }

}// end of Temperature class