Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN JAVA Write a Temperature class that represents temperatures in degrees in bot

ID: 3838319 • Letter: I

Question

IN JAVA

Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit. Use a floating-point number for the temperature and a character for the scale: either ‘C’ for Celsius or ‘F’ for fahrenheit. The class should have:

Four constructors: one for the number of degrees, one for the scale, one for both the degrees and scale, and a default constructor. For each of these constructors, assume zero degrees if no value is specified and celsius if no scale is given.

Two accessor methods: one to return the temperature in degrees Celsius, the other to return it in degrees Fahrenheit. Use the formulas from programming project 5 of chapter 3 and round to the nearest tenth of a degree.

Degrees_C = 5(Degrees_F 32)/9

Degrees_F = (9(Degrees_C)/5) + 32)

Three set methods: one to set the number of degrees, one to set the scale and one to set both.

Three comparison methods: one to test whether two temperatures are equal, one to test whether one temperature is greater than the other, and one to test whether one temperature is less than the other.

Additionally write a driver a program that tests all the methods. Be sure to invoke each of the constructors, to include at least one true and one false case for each comparison method, and to test at least the following three temperature pairs for equality. 0.0 degrees C and 32.0 degrees F, -40.0 degrees C and -40.0 degrees F, and 100.0 degrees C and 212.0 Degrees F.


Make sure in the Class file to appropriately comment your code with javadoc comments for every method. (Pre and post condition comments).

Explanation / Answer

Temperature.java

package Chegg;

public class Temperature {

   private float degree;
   private char scale;

   public Temperature() {

   }

   public Temperature(float degree) {
       this.degree = degree;
       scale = Character.MIN_VALUE;
   }

   public Temperature(char scale) {
       degree = 0;
       this.scale = scale;
   }

   public Temperature(float degree, char scale) {
       this.degree = degree;
       this.scale = scale;
   }

   /**
   * Returns celsius
   *
   * @param degree
   * @return
   */
   public float degreeCelsius(float degree) {
       return 5 * (degree - 32) / 9;
   }

   /**
   * Returns farenheit
   *
   * @param degree
   * @return
   */
   public float degreeFarenheit(float degree) {
       return (9 * (degree) / 5) + 32;
   }

   public void setDegree(float degree) {
       this.degree = degree;
   }

   public void setScale(char scale) {
       this.scale = scale;
   }

   public void setBoth(float degree, char scale) {
       this.degree = degree;
       this.scale = scale;
   }

   /**
   * Comparison equal method
   *
   * @param degree1
   * @param degree2
   * @return
   */
   public boolean isEqual(float degree1, float degree2) {
       degree2 = degreeCelsius(degree2);
       return degree1 == degree2;
   }

   /**
   * Comparison greater method
   *
   * @param degree1
   * @param degree2
   * @return
   */
   public boolean isGreater(float degree1, float degree2) {
       degree2 = degreeCelsius(degree2);
       return degree1 > degree2;
   }

   /**
   * Comparison lower method
   *
   * @param degree1
   * @param degree2
   * @return
   */
   public boolean isLower(float degree1, float degree2) {
       degree2 = degreeCelsius(degree2);
       return degree1 < degree2;
   }

}

Temperaturetester.java

package Chegg;

public class TemperatureTester {

   public static void main(String[] args) {
       Temperature temperature = new Temperature();
       Temperature temperature2 = new Temperature(32);
       Temperature temperature3 = new Temperature('C');
       Temperature temperature4 = new Temperature(32, 'C');

       System.out.println(temperature.isEqual(0.0f, 32.0f));
       System.out.println(temperature.isGreater(0.0f, 32.0f));
       System.out.println(temperature.isLower(0.0f, 32.0f));

       System.out.println(temperature.isEqual(-40f, -40f));
       System.out.println(temperature.isGreater(-40f, -40f));
       System.out.println(temperature.isLower(-40f, -40f));

       System.out.println(temperature.isEqual(100f, 212f));
       System.out.println(temperature.isGreater(100f, 212f));
       System.out.println(temperature.isLower(100f, 212f));
   }
}