Question

In: Computer Science

Write a Temperature class that will hold a temperature in Fahrenheit, and provide methods to get...

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 hold 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)

In order to convert the Fahrenheit temperature to Kelvin, you must first convert it to Celsius, and then add 273. Hint: Use the getCelsius() method inside of the getKelvin() method!

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.

Solutions

Expert Solution

Answer: Hey dear student finds the solution of your query, if you have any doubt feel free to ask. Thanks!!

Copy to code: This code has Tempreature class and a double variable,constructor,setter and getters.In main, create object of the Temperature class and call all methods of the class,print both tempreatures.

import java.io.*;
class Temperature //Temperature class
{
   double ftemp; //data member of the class
  
   public Temperature(double tempF) //constructor
   {
       ftemp = tempF;
   }
   public void setFahrenheit(double f) //method to set setFahrenheit
   {
       ftemp = f;
   }
   public double getFahrenheit() //method to return value from ftemp
   {
       return ftemp;
   }
   public double getCelsius() //method to return value as Celsius
   {
       double celsius;
       celsius =(ftemp-32) ;
       celsius = celsius*0.555;
       return celsius;
   }
   public double getKelvin() //method to return value in Kelvin
   {
       double kelvin;
       kelvin = getCelsius(); //call getCelsius()
       kelvin = kelvin+273;
       return kelvin;
   }
}
class Main
{
   public static void main(String[]args)
   {
       double c,k;
       Temperature t = new Temperature(68.52);//create object of the Temperature class
       t.setFahrenheit(78.2);//call setFahrenheit()
       c = t.getCelsius();//call getCelsius()
       k = t.getKelvin();//cll getKelvin()
       System.out.println("Temperature in Celcius: "+c);//print both Temperatures
       System.out.println("Temperature in Kelvin: "+k);
   }
}
  

Screenshots of the code:

Snapshot ofthe output:


Related Solutions

For My Programming Lab - Java: Write a Temperature class that will hold a temperature in...
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...
**********Java language************ 1-      (Conversions between Celsius and Fahrenheit) Write a class that contains the following two methods:...
**********Java language************ 1-      (Conversions between Celsius and Fahrenheit) Write a class that contains the following two methods: /** Convert from Celsius to Fahrenheit */ public static double celsiusToFahrenheit(double celsius) /** Convert from Fahrenheit to Celsius */ public static double fahrenheitToCelsius(double fahrenheit) The formula for the conversion is: Celsius = (5.0 / 9) * (Fahrenheit – 32) Fahrenheit = (9.0 / 5) * Celsius + 32 Program output should be like: If you want to convert from Fahrenheit To Celsius press 0...
Write a fragment of MIPS code to convert temperature in Kelvin to Fahrenheit
Write a fragment of MIPS code to convert temperature in Kelvin to Fahrenheit
Define a class to represent a Temperature. Your class should contain Instance variable int fahrenheit A...
Define a class to represent a Temperature. Your class should contain Instance variable int fahrenheit A parameterized constructor; Member method to calculate the equivalent temperature in Celsius unit toCelsius() with decimal value. double toCelsius() Conversion formula: celsius= (fahrenheit - 32) * 5/9 Method definition to override the equals() method boolean equals(Object obj)
Write a program that asks the user for a Fahrenheit temperature and calls a function name...
Write a program that asks the user for a Fahrenheit temperature and calls a function name Celsius that returns the temperature in Celsius of the Fahrenheit temperature sent to it. Your function will look similar to this: double celsius(double f) { double c; : : return c; }
In C Write a program that prompts the user to enter a Fahrenheit temperature calculate the...
In C Write a program that prompts the user to enter a Fahrenheit temperature calculate the corresponding temperature in Celsius and print it prompt the user if they want to do another temperature conversion if the user enters y, repeat 1), 2) and 3) if the user enters n, then print Bye and exit the program if the user enters any other character, ask the user to enter y or n only Note : c = (5.0/9.0)*(f-32.0) Sample output Enter...
System.out.println("Enter a temperature in Fahrenheit: ");          double fahrenheit = input.nextDouble();       double...
System.out.println("Enter a temperature in Fahrenheit: ");          double fahrenheit = input.nextDouble();       double celsius = (5.0 / 9) * (fahrenheit - 32);    System.out.println("Fahrenheit " + fahrenheit + " is " + celsius + " in Celsius"); the above code i would like to print to 2 decimal places for celsius please   this is in JAVA
Write in java Q 4.Centigrade and Fahrenheit are two scales to measure temperature. Write a program...
Write in java Q 4.Centigrade and Fahrenheit are two scales to measure temperature. Write a program that that prompts the user to enter Centigrate and then print the Fahrenheit. Use following formula for conversion. Fahrenheit = Centigrade*1.8 +   32 ; Q. 7 Write a program to calculate the bill of a customer. The program will -           Prompt the employee to enter the monthly plan fees. -           Prompt the employee to enter the rate per additional minute. -          Print the bill...
Write a program that displays a temperature conversion table for degrees Celsius and degrees Fahrenheit. The...
Write a program that displays a temperature conversion table for degrees Celsius and degrees Fahrenheit. The table should include rows for all temperatures between 0 and 100 degrees Celsius that are multiples of 10 degrees Celsius. Include appropriate headings on your columns. The formula for converting between degrees Celsius and degrees Fahrenheit can be found on the internet. Python 3
Instructions Write a program in C# that converts a temperature given in Fahrenheit to Celsius. Allow...
Instructions Write a program in C# that converts a temperature given in Fahrenheit to Celsius. Allow the user to enter values for the original Fahrenheit value. Display the original temperature and the formatted converted value. Use appropriate value returning methods for entering (input), calculating, and outputting results.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT