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...
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; }
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.
Write a function in bash that takes an input temperature (Celsius or Fahrenheit) from the user...
Write a function in bash that takes an input temperature (Celsius or Fahrenheit) from the user converts that temperature Celsius to Fahrenheit, Fahrenheit to Celsius, Celsius to Kelvin and Fahrenheit to Kelvin
****USING BASH**** Write a function that takes an input temperature (Celsius or Fahrenheit) from the user...
****USING BASH**** Write a function that takes an input temperature (Celsius or Fahrenheit) from the user converts that temperature Celsius to Fahrenheit, Fahrenheit to Celsius, Celsius to Kelvin and Fahrenheit to Kelvin.
1. Writing a Random Temperature File Write a function that writes a series of random Fahrenheit...
1. Writing a Random Temperature File Write a function that writes a series of random Fahrenheit temperatures and their correspond- ing Celsius temperatures to a tab-delimited file. Use 32 to 212 as your temperature range. From the user, obtain the following: • The number of temperatures to randomly generate.• The name of the output file. A sample run is included below (you must follow the format provided below): Please enter the name of your file: Example.txt Please enter the number...
1. Write a C++ code segment to read 50 temperature values in Fahrenheit and to convert...
1. Write a C++ code segment to read 50 temperature values in Fahrenheit and to convert them to Celsius. You convert a Fahrenheit temperature to Celsius by using the following formula: Celsius = 5.0 / 9 * (Fahrenheit - 32). 2.A client has purchased 20 products in a store. Write a C++ code segment to read the unit price and the number of items of each product and to compute and print the total price of all these products.
1. Temperature in kelvins (K) is related to the temperature in degrees Fahrenheit (°F) by the...
1. Temperature in kelvins (K) is related to the temperature in degrees Fahrenheit (°F) by the equation Design a MATLAB program that will do the following a. Prompt the user to enter an input temperature in °F. b. Read the input temperature. c. Calculate the temperature in kelvins in a separate function d. Write out the results with two digits to the right of the decimal (Use the fprintf function). The results should go to the command window.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT