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

Please solve this using methods that are stricting from the beginner CSS 1/Java

ID: 3902441 • Letter: P

Question

Please solve this using methods that are stricting from the beginner CSS 1/Java 1/ Programming 1 courses. Thank you!

Part 3 A More Sophisticated Object Write a class called Temperature that has two instance variables: a temp value (a floating-point number-data type double) and a character for the scale, either?for Celsius or 'F' for Fahrenheit. The class should have a constructor that sets each instance variable (assume zero degrees if no temp value is specified and Celsius if no scale is specified), and it should include the following methods: Two accessor methods: one to return the degrees Celsius and the other to return the degrees Fahrenheit use the following formulas to write the two methods: DegreesC-5(degreesF -32)/9 DegreesF-(9(degreesC)/5)+32 Three mutator methods: one to set the value, one to set the scale (F or C), and one to set both; A comparison method: it should return -1 if the invoking temperature is less than the argument temperature, O if they are equal and 1 if the invoking temperature is greater than the argument one; A toString method: returns a String representing the calling Temperature object. The numeric value ofthe temperature should be rounded to the nearest tenth of a degree. For example, System.out.printin(new Temperature(98.164555, F)); //should print: 98.2F Then write a driver program called TempTester that tests all the methods from the Temperature class. Example tests: 0.0 degrees C- 32.0 degrees F,-40.0 degrees C-40.0 degrees F, and 100.0 degrees C-212.0 degrees F.

Explanation / Answer

import java.io.*;
import java.util.*;

class Temperature {
   
    private double temp;
    private char scale;

    public Temperature(double t, char s){
        temp = t;
        scale = s;
    }
    public Temperature(double t){
        temp = t;
        scale = 'C';
    }
    public Temperature(char s){
        temp = 0;
        scale = s;
    }
    public void setScale(char a){
        scale = a;
    }
    public void setValue(double a){
        temp = a;
    }
    public double degreeCelsius(){
       if (scale == 'C')
          return temp;
       else {
          return 5.0 *(temp - 32.0)/9.0;
       }
    }
   
    public double degreeFarenheit(){
       if (scale == 'F')
          return temp;
       else {
          return (9.0 *(temp/5.0)) + 32.0;
       }
    }
   
    public int compare(Temperature t){
        int res;
        if (scale == 'C'){
           if (temp > t.degreeCelsius()){
              res = 1;
           }
           else if (temp < t.degreeCelsius()){
              res = -1;
           }
           else {
              res = 0;
           }
        }
        else {
           if (temp > t.degreeFarenheit()){
              res = 1;
           }
           else if (temp < t.degreeFarenheit()){
              res = -1;
           }
           else {
              res = 0;
           }
        }
        return res;
    }

    public String toString(){
        return Double.toString(temp) + " " + scale;
    }
   
}

public class TempTester{
    public static void main(String[] args){
        
          Temperature t1 = new Temperature(0,'C');
          Temperature t2 = new Temperature(32,'F');
          if (t1.compare(t2) == 0)
             System.out.println("Same");
          else
             System.out.println("Different");
          t1 = new Temperature(-40,'C');
          t2 = new Temperature(-40,'F');
          if (t1.compare(t2) == 0)
             System.out.println("Same");
          else
             System.out.println("Different");
          t1 = new Temperature(100,'C');
          t2 = new Temperature(212,'F');
          if (t1.compare(t2) == 0)
             System.out.println("Same");
          else
             System.out.println("Different");
       
    }
}