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 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. 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.
Write a program read in data from the standard input stream (cin) representing temperature in Fahrenheit....
Write a program read in data from the standard input stream (cin) representing temperature in Fahrenheit. The program will then convert the values into Kelvin (using 5/9 (Fº-32) to convert to Celsius and a difference of 273.15 between Celsius and Kelvin) and print out the new total value. Convert temperature in Fahrenheit to Kelvin : Enter temperature in Fahrenheit: 80.33 The temperature in Kelvin is: 300 Write a second program which then does the reverse conversion (using a difference of...
Write the class RecursiveProbs, with the methods listed below. Write all the methods using recursion, not...
Write the class RecursiveProbs, with the methods listed below. Write all the methods using recursion, not loops. You may use JDK String methods like substring() and length(), but do not use the JDK methods to avoid coding the algorithms assigned. For example, don't use String.reverse(). public boolean recursiveContains(char c, String s) returns true if the String contains the char, otherwise returns false. Here is the code for this method, which you should use as an example to see how to...
Add code (see below for details) to the methods "set" and "get" in the following class,...
Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index] =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT