Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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:

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven NoSQL Databases In A Week Get Up And Running With The Fundamentals And Functionalities Of Seven Of The Most Popular NoSQL Databases

Authors: Aaron Ploetz ,Devram Kandhare ,Sudarshan Kadambi ,Xun Wu

1st Edition

1787288862, 978-1787288867

More Books

Students also viewed these Databases questions