Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Temperature Class Write a Temperature class that will hold a temperature in Fahr

ID: 671564 • Letter: T

Question

Temperature Class Write a Temperature class that will hold a temperature in Fahrenheit, and provide meth- ods 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.

Explanation / Answer

package mani;


public class Temperature{
   double ftemp;
   public Temperature(double f){
       ftemp=f;
   }
   public void setFahrenheit(double f){
       this.ftemp=f;
   }
   public double getFahrenheit(){
       return ftemp;
   }
   public double getCelsius(){
       double cel=(5)*(ftemp-32);
       cel=cel/9;
       return cel;
   }
   public double getKelvin(){
       double K=((5/9)*(ftemp-32))+273;
       return K;
   }

}

public class Application {
   public static void main(String args[]) {
       Scanner scan=new Scanner(System.in);
       System.out.print("Enter the temperature in fahrenheit: ");
       double t=scan.nextDouble();
       Temperature f=new Temperature(t);
       System.out.println("Fahrenheit: "+f.getFahrenheit());
       System.out.println("Temperature in celsius: "+f.getCelsius());
       System.out.println("Temperature in Kelvin: "+f.getKelvin());
   }
  

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote