JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The clas
ID: 3783918 • Letter: J
Question
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:
Then the output should look like this:
Explanation / Answer
//Temperature.java
public class Temperature
{
//private data members to store temperature and scale
public double temperature;
public char scale;
//default constructor
public Temperature()
{
}
//Constructor of Temperature class
public Temperature(double temperature, char scale)
{
this.temperature=temperature;
this.scale=scale;
}
//Methot to convert temp to kelvin
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;
}
//Methot to convert temp to Celsius
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;
}
//Methot to convert temp to Fahrenheit
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;
}
//Method to add temperatures in celsius
public Temperature add(Temperature n)
{
Temperature temp1 = this.toCelsius();
Temperature temp2 = n.toCelsius();
return new Temperature(temp1.temperature+temp2.temperature, 'C');
}
public Temperature subtract(Temperature n)
{
Temperature temp1 = this.toCelsius();
Temperature temp2 = n.toCelsius();
return new Temperature(temp1.temperature-temp2.temperature, 'C');
}
//Method to subtract temperatures in celsius
public Temperature multiply(Temperature n)
{
Temperature temp1 = this.toCelsius();
Temperature temp2 = n.toCelsius();
return new Temperature(temp1.temperature*temp2.temperature, 'C');
}
public Temperature divide(double d)
{
Temperature temp1 = this.toCelsius();
double new_temp=(double)(temp1.temperature/d);
return new Temperature(new_temp, 'C');
}
//Method to check equality
public boolean equals(Temperature n)
{
//first convert n temperature to corresponding class
//temperature
if(this.scale=='K')
n=n.toKelvin();
else if(this.scale=='C')
n=n.toCelsius();
else if(this.scale=='F')
n=n.toFahrenheit();
return this.temperature==n.temperature&&this.scale==n.scale;
}
//Rturns the string representation of Temperature
public String toString()
{
return temperature + "" + scale ;
}
}
-----------------------------------------------------------------
/**
* The java program TemperatureDemoWithoutArrays that
* tests the class Temperature and print the corresponding
* results.
* */
//TemperatureDemoWithoutArrays.java
public class TemperatureDemoWithoutArrays
{
public static void main(String[] args)
{
//Create instnace of Temperature class
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);
//Calling toKelvin method
temp1 = temp1.toKelvin();
System.out.println("Temp1 to Kalvin is " + temp1);
//Checking equals method
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);
//calling add method
tempAve = tempAve.add(temp1);
tempAve = tempAve.add(temp2);
tempAve = tempAve.add(temp3);
tempAve = tempAve.add(temp4);
//calling divide method since five temperatures are added to tempAve object
tempAve = tempAve.divide(5);
System.out.println("The average temperatrure is " + tempAve);
temp2=new Temperature(150, 'K');
System.out.print("Subtracting " + temp2 + " from " + temp4 +" gives " );
//calling substract
temp4 = temp4.subtract(temp2);
System.out.println(temp4);
}
}
-----------------------------------------------------------------
Sample output:
122.0F to Celcius is 50.0C
Temp1 is 100.0C
Temp1 to Kalvin is 373.15K
These two temperatures are equal
tempAve is 50.0C
temp1 is 373.15K
temp2 is 122.0F
temp3 is 32.0F
temp4 is 100.0C
The average temperatrure is 60.0C
Subtracting 150.0K from 100.0C gives 223.14999999999998C
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.