4. Temperature Class Write a Temperature class that will hold a temperature in F
ID: 3793107 • Letter: 4
Question
4. 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) _ (Fahrenheit _ 32)
Use the following formula to convert the Fahrenheit temperature 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 Celsius and Kelvin.
USING BLUE J PROVIDE TWO SOURCE CODE SHOWING RELATIONSHIP OF THE CODE AND OUTPUT. ONE CODE WITH TEMPERATURE AND OTHER WITH TEMPERATUREDEMO. IN TOTAL, YOU SHOULD SUBMIT 6 SOURCE CODE EACH HAS TWO SOURCE CODE. BELOW IS THE SAMPLE OF HOW SHOULD LOOK
public class HelloWorldDemo
{
public static void main(String [] args) {
HelloWorld myMessage = new HelloWorld();
System.out.println(myMessage.getMessage());
}
}
public class HelloWorld
{
private final String MSG="Hello World";
public String getMessage(){
return MSG;
}
}
Explanation / Answer
TemperatureDemo.java
import java.util.Scanner;
public class TemperatureDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the temparature in Fahrenheit: ");
double ftemp = scan.nextDouble();
Temperature t = new Temperature(ftemp);
System.out.println("Celcius Temperature is "+t.getCelcius());
System.out.println("Kevin Temperature is "+t.getKelvin());
}
}
Temperature.java
public class Temperature {
private double ftemp;
public Temperature(double f){
ftemp = f;
}
public void setFahrenheit (double f){
ftemp = f;
}
public double getFahrenheit(){
return ftemp;
}
public double getCelcius(){
return ((ftemp - 32) * 5)/9;
}
public double getKelvin(){
return getCelcius() + 273.15;
}
}
Output:
Enter the temparature in Fahrenheit:
36
Celcius Temperature is 2.2222222222222223
Kevin Temperature is 275.3722222222222
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.