In: Computer Science
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have three conversion methods: toCelcius(), toKelvin and toFahrenheit(). These methods will return a Temperature in those three scales equal to this temperature. Note that the value of this is not changed int these coversions. In addition to these conversion methods the class will have add(Temperature), subtract(Temperature), multiply(Temperature) and divide(Temperature). These four methods all return a temperature equalled to the respective operation. Note that the this value is not changed in these operations. Two boolean methods equals(Temperature), and greaterThan(Temperature) will return true if the this is greater than the parameter. Your class should include a read() method and a toString() method. Remember methods add, subtract, multiply, divide and the threee convesion methods all return a Temperature. Include a least two constructore: a default and a explicit. Use a private helper called set() that takes the parameters of the constructor and tests for appropiate values for each possible scale. This private set() method can be used to guarantee temperrature valuese are in the proper range. The subtract() and divide() methods can call the constructor to return a temperature in a legal range. A switch statement should be used throughtout this class when choosong between "C", "K", and "F". Absolute zero for Kelvin is 0, for Fahrenheit -459.67, and fro Calvin -273.15. Your program must guarantee this absolute value is not violated. For the equal() method consider changing the this temperature and the parameter temperature to the same scale and then testing the degree value for equality.
Driver:
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 temperatrure is " + tempAve); System.out.print("Subtracting " + temp2 + " from " + temp4 +" gives " ); temp4 = temp4.subtract(temp2); System.out.println(temp4); } }
Then the output should look like this:
// Temperature.java
class Temperature
{
public double temp;
public char temperatureScale;
//Empty constructor
public Temperature(){}
// Constructor
public Temperature(double temp, char temperatureScale)
{
this.temp=temp;
this.temperatureScale=temperatureScale;
}
// Method to convert the given temperature into kelvin
public Temperature toKelvin()
{
double conv_Temp;
switch(temperatureScale)
{
case 'C' :
conv_Temp=(temp+273.15);
Temperature t = new Temperature(conv_Temp, 'K');
return t;
case 'F' :
conv_Temp=(double)((temp-32)*5.0/9.0+273.15);
t = new Temperature(conv_Temp, 'K');
return t;
}
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;
switch(temperatureScale)
{
case 'K' :
conv_Temp=temp-273.15;
Temperature t = new Temperature(conv_Temp, 'C');
return t;
case 'F' :
conv_Temp=(double)((temp-32)*5.0/9.0);
t = new Temperature(conv_Temp, 'C');
return t;
}
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;
switch(temperatureScale)
{
case 'K' :
conv_Temp=(double)(temp -273.15)*9.0/5.0+32;
Temperature t = new Temperature(conv_Temp, 'F');
return t;
case 'C' :
conv_Temp=(double)(temp*9.0/5.0 +32);
t = new Temperature(conv_Temp, 'F');
return t;
}
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.temperatureScale==n.temperatureScale;
}
// Method to convert the temperature values into a string
public String toString()
{
return "" + temp + " " + temperatureScale ;
}
// 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();
}
}
}
// TemperatureDemoWithoutArrays.java
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);
}
}
/*
output:
122.0 F to Celcius is 50.0 C
Temp1 is 100.0 C
Temp1 to Kalvin is 373.15 K
These two temperature are not equal
tempAve is 50.0 C
temp1 is 373.15 K
temp2 is 122.0 F
temp3 is 32.0 F
temp4 is 100.0 C
The average temperature is 333.15 K
Subtracting 122.0 F from 100.0 C gives 50.0 K
*/