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(), toFarhrenheit(), 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{" + "temperature=" + temperature + "scale=" + scale + '}';
}
public void read()
{
System.out.println(this);
}
}
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 );
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)
{ Static method written by you.
}
public static Temperature getAverage(Temperature[] array)
{ Static method written byyou
}
}
I'm having problems with this part:
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)
{ Static method written by you.
}
public static Temperature getAverage(Temperature[] array)
{ Static method written by you
}
I pretty much have the whole assignment complete, but I am having trouble with this last part. This is what I have so far and I'm not sure where to go from here. Any help would be greatly appreciated!
Temperature tempArray [] = new Temperature [ARRAY_SIZE];
{
for (int i = 0; i < tempArray.length; i++)
{
tempArray[i]= new Temperature();
}