Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java: Write a temperature class using the demo below : Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit().

Java:

Write a temperature class using the demo below:

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.

import java.util.Scanner; public class TemperatureDemo { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Reading temperature for t1 System.out.print("Please enter a temperature degree: "); double temperature = input.nextDouble(); System.out.print("What is the scale (C/F/K)? "); char scale = input.next().charAt(0); Temperature t1 = new Temperature(temperature, scale); // Reading temperature for t2 System.out.print("Please enter a temperature degree: "); temperature = input.nextDouble(); System.out.print("What is the scale (C/F/K)? "); scale = input.next().charAt(0); Temperature t2 = new Temperature(temperature, scale); System.out.println(t1.toCelsius()); System.out.println(t2.toCelsius()); System.out.println(t1.add(t2)); System.out.println(t1.equals(t2)); System.out.println("T1: " + t1.toString()); System.out.println("T2: " + t2.toString()); } } 

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_2

Step: 3

blur-text-image_3

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

Distributed Relational Database Architecture Connectivity Guide

Authors: Teresa Hopper

4th Edition

0133983064, 978-0133983067

More Books

Students also viewed these Databases questions

Question

=+2 How can the effectiveness of global virtual teams be improved?

Answered: 1 week ago

Question

=+1 What are the major issues related to international T&D?

Answered: 1 week ago