Question: Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales
Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions.
In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and divide(double). These four methods all return a temperature equaled 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 three conversion methods all return a Temperature.
Include at least two constructors: a default constructor and an explicit constructor.
You must use a private helper method called set() that takes the parameters of the constructor and tests for appropriate values for each possible scale. The set method is a void method. This private set() method can be used to guarantee temperature values are in the proper range.
The add(), subtract(), multiply(), and divide() methods can call the constructor which calls the set() method.
The set method will check the degree value and if it is in the proper range a new Temperature will be made to be returned by the add() method, subtract() method, multiply() method and divide() method.
A switch statement should be used throughout this class when choosing between C, F, and K.
Absolute zero for Kelvin is 0, for Fahrenheit -459.67, and for Celsius -273.15. Your program must guarantee this absolute value is not violated.
For the equals() method consider changing the this temperature and the parameter temperature to the same scale and then testing the degree value for equality.
Here is the Demo
public class TemperatureDemoWithoutArrays { public static void main(String[] args) { 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 temp5 = new Temperature(212, 'F'); System.out.println("temp1 is " + temp1); System.out.println("temp5 is " + temp5); System.out.println("The sum of temp1 and temp5 should be a Fahrenheit value of 424f,"); Temperature sum = new Temperature(); sum = temp5.add(temp1); System.out.println("The sum of temp1 and temp5 is " + sum); System.out.println(" Temp2 is " + temp2 + " which is 50C."); System.out.println("temp2 to Celcius is " + temp2.toCelsius()+ "."); System.out.println(" Temp1 is " + temp1 + " and the Kalvin value is 373.15 " ); temp1 = temp1.toKelvin(); System.out.println("Temp1 to Kalvin is " + temp1); System.out.println(" The temperature 122F and 50C are the same."); Temperature tempAve = new Temperature(50.0, 'C'); System.out.println("temp2 is " + temp2); System.out.println("tempAve is " + tempAve); System.out.println("Are temp2 and tempAve the same?"); if (temp2.equals(tempAve)) { System.out.println("Yes, these two temperatures are equal."); } else { System.out.println("No, these two temperatures are not equal."); } tempAve = new Temperature(0.0, 'C'); 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("temp5 is " + temp5); System.out.println("The average of these 5 temperatures should be 50C"); 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.println(" Test the greaterThan() method."); System.out.println("The first temperature is temp4 and is " + temp4); System.out.println("The second temperature is temp2 and is " + temp2); System.out.println("Is temp4 greater than temp2?"); if (temp4.greaterThan(temp2)) { System.out.println("Yes, temp4 is greater then temp2."); } else { System.out.println("No, temp4 is not greater then temp2."); } } }
Thank You!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
