For My Programming Lab - Java: Write a Temperature class that will hold a temper
ID: 3806330 • Letter: F
Question
For My Programming Lab - Java:
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 set Fahrenheit 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. Use the following formula to convert to Celsius:
Celsius = (5/9) * (Fahrenheit - 32)
• getKelvin: Returns the value of the ftemp field converted to Kelvin. Use the following formula to convert to Kelvin:
Kelvin = ((5/9) * (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 the following format (for example,
if the temperature in Fahrenheit was -40):
The temperature in Fahrenheit is -40.0
The temperature in Celsius is -40.0
The temperature in Kelvin is 233.0
Explanation / Answer
import java.util.*;
class Temperature
{
private double ftemp;
public Temperature(double ftemp) //constructor
{
this.ftemp = ftemp;
}
public void setFahrenheit(double ftemp) //set Fahrenheit Temperature
{
this.ftemp = ftemp;
}
public double getFahrenheit() //get temperature in Fahrenheit
{
return ftemp;
}
public double getCelsius() //get temperature in Celsius
{
double ctemp = (5.0/9.0) * (ftemp - 32.0);
return ctemp;
}
public double getKelvin() //get temperature in Kelvin
{
double ktemp = ((5.0/9.0) * (ftemp - 32.0)) + 273.0;
return ktemp;
}
}
class Test
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter temperature in Fahrenheit : ");
double ftemp = scan.nextDouble(); //input temperature in Fahrenheit
Temperature t = new Temperature(ftemp);
//display temperature in different units
System.out.println("The temperature in Fahrenheit is "+t.getFahrenheit());
System.out.println("The temperature in Celsius is "+t.getCelsius());
System.out.println("The temperature in Kelvin is "+t.getKelvin());
}
}
Output:
Enter temperature in Fahrenheit : -40
The temperature in Fahrenheit is -40.0
The temperature in Celsius is -40.0
The temperature in Kelvin is 233.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.