My project is to write a Temperature class in Java. In addition to converting be
ID: 3634973 • 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. I am having trouble with my read() method. Here is what I have.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 void read()
{
Temperature[] array = new Temperature[5];
for (int i = 0; i < array.length; i++)
{
array[i] = new Temperature();
}
}
}
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)
{
double doubleAverage = 0;
for(int i = 1; i < array.length; i++)
{
doubleAverage = array[i].toKelvin().temperature;
}
return new Temperature((doubleAverage/array.length), 'K');
}
}
Explanation / Answer
class Temperature
{
public double temp;
public char temp_scale;
//Empty constructor
public Temperature(){}
// Constructor
public Temperature(double temp, char temp_scale)
{
this.temp=temp;
this.temp_scale=temp_scale;
}
// Method to convert the given temperature into kelvin
public Temperature toKelvin()
{
double conv_Temp;
if(temp_scale == 'C')
{
conv_Temp=(temp+273.15);
Temperature temp = new Temperature(conv_Temp,
'K');
return temp;
}
if(temp_scale == 'F')
{
conv_Temp=(double)((temp-32)*5.0/9.0+273.15);
Temperature temp = new Temperature(conv_Temp,
'K');
return temp;
}
if(temp < 0)
{
System.out.println("K cannot be < 0");
System.exit(0);
}
return this;
}
// Method to convert the given temperature into Celsius
public Temperature toCelsius()
{
double conv_Temp;
if(temp_scale == 'K')
{
conv_Temp=temp-273.15;
Temperature temp = new Temperature(conv_Temp,
'C');
return temp;
}
if(temp_scale == 'F')
{
conv_Temp=(double)((temp-32)*5.0/9.0);
Temperature temp = new Temperature(conv_Temp,
'C');
return temp;
}
if(temp < -273.15)
{
System.out.println("C cannot be < -273.15");
System.exit(0);
}
return this;
}
// Method to convert the given temperature into
Fahrenheit
public Temperature toFahrenheit()
{
double conv_Temp;
if(temp_scale == 'K')
{
conv_Temp=(double)(temp -273.15)*9.0/5.0+32;
Temperature temp = new Temperature(conv_Temp,
'F');
return temp;
}
if(temp_scale == 'C')
{
conv_Temp=(double)(temp*9.0/5.0 +32);
Temperature temp = new Temperature(conv_Temp,
'F');
return temp;
}
if(temp < -459.67)
{
System.out.println("F cannot be < -459.67");
System.exit(0);
}
return this;
}
// Method to add the temperature values
public Temperature add(Temperature n)
{
Temperature temp1 = this.toKelvin();
Temperature temp2 = n.toKelvin();
return new Temperature(temp1.temp+temp2.temp, 'K');
}
// Method to subtract the temperature values
public Temperature subtract(Temperature n)
{
Temperature temp1 = this.toKelvin();
Temperature temp2 = n.toKelvin();
return new Temperature(temp1.temp-temp2.temp, 'K');
}
// Method to multiply the temperature values
public Temperature multiply(Temperature n)
{
Temperature temp1 = this.toKelvin();
Temperature temp2 = n.toKelvin();
return new Temperature(temp1.temp*temp2.temp, 'K');
}
// Method to divide the temperature values
public Temperature divide(double d)
{
Temperature temp1 = this.toKelvin();
double new_temp=(double)(temp1.temp/d);
return new Temperature(new_temp, 'K');
}
// Method to compare the temperature values
public boolean equals(Temperature n)
{
return this.temp==n.temp&&this.temp_scale ==n.temp_scale;
}
// Method to convert the temperature values into a string
public String toString()
{
return "" + temp + " " + temp_scale ;
}
// Method to read the temperature values
public void read()
{
Temperature[] array = new Temperature[5];
for (int i = 0; i < array.length; i++)
{
array[i] = new Temperature();
}
}
}
// class to test the temperature
public class TemperatureDemoWithoutArrays
{
public static final int ARRAY_SIZE = 5;
public static void main(String[] args)
{
int x;
Temperature temp1 = new Temperature(100.0,
'C');
Temperature temp2 = new Temperature(122, 'F');
Temperature temp3 = new Temperature(32.0,
'F');
Temperature temp4 = new Temperature(100.0,
'C');
Temperature tempAve = new Temperature(50.0,
'C');
System.out.println(temp2 + " to Celcius is " +
temp2.toCelsius());
System.out.println("Temp1 is " + temp1);
temp1 = temp1.toKelvin();
System.out.println("Temp1 to Kalvin is " +
temp1);
if (temp2.equals(tempAve))
{
System.out.println("These two
temperatures are equal");
}
else
{
System.out.println("These two
temperature are not equal");
}
System.out.println("tempAve is " + tempAve);
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(5);
System.out.println("The average temperature is
" + tempAve);
System.out.print("Subtracting " + temp2 + "
from " + temp4 +" gives " );
temp4 = temp4.subtract(temp2);
System.out.println(temp4);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.