Write a Temperature class that will hold a temperature in Fa

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

Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

########## Temperature.java ###########

public class Temperature {

  

   // instance variable

   private double ftemp;

  

   // constructor

   public Temperature(double f){

       ftemp = f;

   }

  

   public void setFahrenheit(double f){

       ftemp = f;

   }

  

   public double getFahrenheit(){

       return ftemp;

   }

  

   public double getCelsius(){

       return (5.0/9.0)*(ftemp - 32);

   }

  

   public double getKelvin(){

       return ((5.0/9.0) * (ftemp - 32)) + 273;

   }

}

############# TemperatureTest.java #############

public class TemperatureTest {

  

   public static void main(String[] args) {

      

       Temperature temp = new Temperature(-40);

      

       System.out.println(\"Temperature in fahrenheit: \"+temp.getFahrenheit());

       System.out.println(\"Temperature in calcius: \"+temp.getCelsius());

       System.out.println(\"Temperature in kelvin: \"+temp.getKelvin());

   }

}

/*

Sample run:

Temperature in fahrenheit: -40.0

Temperature in calcius: -40.0

Temperature in kelvin: 233.0

*/

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 sh
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 sh

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site