My project is to write a Temperature class. 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.

public class Temperature {
	   
    public double temperature;
    public char scale; 
    
    public Temperature(double temperature, char scale)
    {
        this.temperature=temperature;
        this.scale=scale;
    }
   
    public Temperature() 
    {
	}

	public Temperature toKelvin()
    {
        double converted_Temp;
         
        
        if(scale == 'C')
        {
            converted_Temp=(temperature+273.15);
            Temperature temp = new Temperature(converted_Temp, 'K');
            return temp;
        }
       
        if(scale == 'F')
        {
            converted_Temp=(double)((temperature-32)*5.0/9.0+273.15);
            Temperature temp = new Temperature(converted_Temp, 'K');
            return temp;
        }
        if(temperature < 0) 
        { 
              System.out.println("K cannot be < 0"); 
              System.exit(0); 
        } 
       
        return this;
    }
   
    public Temperature toCelsius()
    	{
        double converted_Temp;
 
        if(scale == 'K')
        {
            converted_Temp=temperature-273.15;
            Temperature temp = new Temperature(converted_Temp, 'C');
            return temp;
        }
    
        if(scale == 'F')
        {
            converted_Temp=(double)((temperature-32)*5.0/9.0);
            Temperature temp = new Temperature(converted_Temp, 'C');
            return temp;
        }
        if(temperature < -273.15) 
        { 
              System.out.println("C cannot be < -273.15"); 
              System.exit(0); 
        } 
       
        return this;
    }
   
    public Temperature toFahrenheit()
    {
        double converted_Temp;
 
        if(scale == 'K')
        {
            converted_Temp=(double)(temperature -273.15)*9.0/5.0+32;
            Temperature temp = new Temperature(converted_Temp, 'F');
            return temp;
        }

        if(scale == 'C')
        {
            converted_Temp=(double)(temperature*9.0/5.0 +32);
            Temperature temp = new Temperature(converted_Temp, 'F');
            return temp;
        }
        if(temperature < -459.67) 
        { 
              System.out.println("F cannot be < -459.67"); 
              System.exit(0); 
        } 
        return this;
    }
   

    public Temperature add(Temperature n)
    {
        Temperature temp1 = this.toKelvin();
        Temperature temp2 = n.toKelvin();
       
        return new Temperature(temp1.temperature+temp2.temperature, 'K');
    }
   
    public Temperature subtract(Temperature n)
    {
        Temperature temp1 = this.toKelvin();
        Temperature temp2 = n.toKelvin();
       
        return new Temperature(temp1.temperature-temp2.temperature, 'K');
    }
   
    public Temperature multiply(Temperature n)
    {
        Temperature temp1 = this.toKelvin();
        Temperature temp2 = n.toKelvin();
       
        return new Temperature(temp1.temperature*temp2.temperature, 'K');
    }
   
    public Temperature divide(double d)
    {
        Temperature temp1 = this.toKelvin();
        double new_temp=(double)(temp1.temperature/d);
       
        return new Temperature(new_temp, 'K');
    }
   

    public boolean equals(Temperature n)
    {
        return this.temperature==n.temperature&&this.scale==n.scale;
    }

    public String toString() 
    {
        return "" + temperature + " " + scale  ;
    }
   
    public String read()
    {
    return this.toString();
    }
}
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.equals(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 );
                 Temperature tempArray [] = new Temperature [ARRAY_SIZE];
                 readTemperatures(tempArray);// must write this method 
                 tempAve = getAverage(tempArray); // must write this method 
                 System.out.println("the average of the array of temperatures is       " + tempAve ); 
                 
         }  


public static void readTemperatures(Temperature[] array)
{ 
	for (int i = 0; i < array.length; i++)
	{
		array[i] = new Temperature();
		array[i].read();
		System.out.println("array[i] " + array[i]); 
	}
}
public static Temperature getAverage(Temperature[] array) 
{ 
	Temperature sum = array[0];
	for(int i = 1; i < array.length; i++) 
        {
		sum = sum.add(array[i]);
        }
	Temperature tempAve = new Temperature(sum.temperature/array.length, 'K');
	return tempAve;
	
}
}

This is the output I get when I run the program:
Temp1 is 120.0 C
Temp1 to Kelvin is 310.92777777777775 K
These two temperature are not equal
Temp1 is 310.92777777777775 K
Temp2 is 100.0 F
Temp3 is 50.0 C
Temp4 is 232.0 K
the average temperature is 362.5388888888889 K
array 0.0
array 0.0
array 0.0
array 0.0
array 0.0
the average of the array of temperatures is 0.0 K

I think my read() method in my Temperature class is wrong. I am also unsure about my two static methods at the end of my TemperatureDemo class. Can someone please help me fix my read() method. Thanks a lot.

Sorry about that, I forgot I had made that previous thread. If I have any more questions regarding this program I will post them in the other thread. I will mark this thread as solved.

This question has already been answered. Start a new discussion instead.