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

My project is to write a Temperature class in Java. In addition to converting be

ID: 3629201 • Letter: M

Question

My project is to write a Temperature class in Java. In addition to converting between Celsius and Fahrenheit also include Kelvin. The class has read(), add(Temperature), subtract(Temperature), multiply(Temperature), divide(double), equals(Temperature), toKelvin(), toFahrenheit(), toCelsius(), and toString() methods. Methods add, subtract, multiply, and divide all return a Temperature. ALL METHODS MUST BE USED TO SOLVE THIS PROBLEM!

This is the demo class:
public class TemperatureDemo
{
public static final int ARRAY_SIZE = 5;
public static void main(String[] args)
{
int x;
Temperature temp1 = new Temperature(120.0, “C”);
Temperature temp2 = new Temperature(100, “F”);
Temperature temp3 = new Temperature(50.0, “C”);
Temperature temp4 = new Temperature(232.0, “K”);
Temperature tempAve = new Temperature(0.0, “C”);
// create array
// fill array with empty temperatures
// this will be discussed in class
System.out.println("Temp1 is " + temp1);
temp1 = temp2.toKelvin();
System.out.println("Temp1 to Kelvin is " + temp1);
if (temp1.equal(temp3))
{
System.out.println("These two temperatures are equal");
}
else
{
System.out.println("These two temperature are not equal");
}
System.out.println("Temp1 is " + temp1);
System.out.println("Temp2 is " + temp2);
System.out.println("Temp3 is " + temp3);
System.out.println("Temp4 is " + temp4);
tempAve = tempAve.add(temp1);
tempAve = tempAve.add(temp2);
tempAve = tempAve.add(temp3);
tempAve = tempAve.add(temp4);
tempAve = tempAve.divide(4);
System.out.println("the average temperature is " + tempAve );

Explanation / Answer

If you prefere to use String for the variable unit, use equal instead of ==.

package temperature;

public class Temperature {
   
    private int temperature;
    private char unit; // possible value 'K' 'C' 'F'
   
    //Constructor
    public Temperature(int temperature, char unit){
        this.temperature=temperature;
        this.unit=unit;
    }
   
    //Covert
    //I cast long to int to make my IDE happy!
    //You could log a message in case the conversion is not effective, in this case i return the same obj.
    public Temperature toKelvin(){
        int converted_Temp;
        //for Celsius
        if(unit == 'C'){
            converted_Temp=temperature+273;
            Temperature temp = new Temperature(converted_Temp, 'K');
            return temp;
        }
        //for Fahrenheit
        if(unit == 'F'){
            converted_Temp=(int) Math.round((temperature+459.67)*5.0/9);
            Temperature temp = new Temperature(converted_Temp, 'K');
            return temp;
        }
       
        return this;
    }
   
    public Temperature toCelsius(){
        int converted_Temp;
        //for Kelvin
        if(unit == 'K'){
            converted_Temp=temperature-273;
            Temperature temp = new Temperature(converted_Temp, 'C');
            return temp;
        }
        //for Fahrenheit
        if(unit == 'F'){
            converted_Temp=(int) Math.round((temperature-32)*5.0/9);
            Temperature temp = new Temperature(converted_Temp, 'C');
            return temp;
        }
       
        return this;
    }
   
    public Temperature toFahrenheit(){
        int converted_Temp;
        //for Kelvin
        if(unit == 'K'){
            converted_Temp=(int) Math.round(temperature*9.0/5 -459.67);
            Temperature temp = new Temperature(converted_Temp, 'F');
            return temp;
        }
        //for Celsius
        if(unit == 'C'){
            converted_Temp=(int)Math.round(temperature*9.0/5 +32);
            Temperature temp = new Temperature(converted_Temp, 'F');
            return temp;
        }
       
        return this;
    }
   
    //Arthmetic
    public Temperature add(Temperature obj){
        Temperature temp1 = this.toKelvin();
        Temperature temp2 = obj.toKelvin();
       
        return new Temperature(temp1.temperature+temp2.temperature, 'K');
    }
   
    public Temperature subtract(Temperature obj){
        Temperature temp1 = this.toKelvin();
        Temperature temp2 = obj.toKelvin();
       
        return new Temperature(temp1.temperature-temp2.temperature, 'K');
    }
   
    public Temperature multiply(Temperature obj){
        Temperature temp1 = this.toKelvin();
        Temperature temp2 = obj.toKelvin();
       
        return new Temperature(temp1.temperature*temp2.temperature, 'K');
    }
   
    public Temperature divide(double d){
        Temperature temp1 = this.toKelvin();
        int new_temp=(int)Math.round(temp1.temperature/d);
       
        return new Temperature(new_temp, 'K');
    }
   
    //equals method
    public boolean equals(Temperature obj){
        return this.temperature==obj.temperature&&this.unit==obj.unit;
    }

    @Override
    public String toString() {
        return "Temperature{" + "temperature=" + temperature + "unit=" + unit + '}';
    }
   
    // I guess that read output the temperature.
    public void read(){
        System.out.println(this);
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote