6.8 (Part 2) Temperature Class - Java

in #programming8 years ago

Starting out with Java: From control structures through objects
Chapter 6
Programming Challenges

  1. Temperature class

Write a Temperature class that will hold a temperature in Fahrenheit, and provide methods to get the temperature in Fahrenheit, Celsius, and Kelvin. The class should have the following field:

• ftemp–A double that holds a Fahrenheit temperature.

The class should have the following methods:

• Constructor–The constructor accepts a Fahrenheit temperature (as a double) and stores it in the ftemp field.
• setFahrenheit – The setFahrenheit method accepts a Fahrenheit temperature (as a double) and stores it in the ftemp field.
• getFahrenheit – Returns the value of the ftemp field, as a Fahrenheit temperature (no conversion required).
• getCelsius – Returns the value of the ftemp field converted to Celsius.
• getKelvin – Returns the value of the ftemp field converted to Kelvin.

Use the following formula to convert the Fahrenheit temperature to Celsius: Celsius = (5/9) x (Fahrenheit - 32)

Use the following formula to convert the Fahrenheit temperature to Kelvin: Kelvin = ((5/9) x (Fahrenheit - 32)) + 273

Demonstrate the Temperature class by writing a separate program that asks the user for a Fahrenheit temperature. The program should create an instance of the Temperature class, with the value entered by the user passed to the constructor. The program should then call the object’s methods to display the temperature in Celsius and Kelvin.

Gaddis, Tony (2015-05-29). Starting Out with Java: From Control Structures through Objects (Page 398 and 399). Pearson Education. Kindle Edition.

Happy Steeming Everyone! :)

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.028
BTC 58080.30
ETH 3102.16
USDT 1.00
SBD 2.40