56.9k views
3 votes
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 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

sample run:

Enter·a·Fahrenheit·temperature:20↵

The·temperature·in·Fahrenheit·is·20.0↵

The·temperature·in·Celsius·is·-6.666666666666667↵

The·temperature·in·Kelvin·is·266.3333333333333↵

User Antonieta
by
6.0k points

1 Answer

1 vote

Answer:

import java.util.Scanner;

//The Temperature Class Begins Here

public class Temperature {

private double ftemp;

//The constructor

public Temperature(double ftemp) {

this.ftemp = ftemp;

}

//Get farenheit Method

public double getFtemp() {

return ftemp;

}

//Set farenheit method

public void setFtemp(double ftemp) {

this.ftemp = ftemp;

}

// Get Celcius Method

public double getCelcius(double ftemp){

//double celcius = (double) (5/9) * (ftemp - 32);

double celcius = (ftemp-32)* (double)5/9;

return celcius;

}

// Get Kelvin Method

public double getKelving(double ftemp){

double Kelvin;

Kelvin = (ftemp-32)*(double)5/9 + 273.15;

return Kelvin;

}

}

//The Temperature Class Ends Here

//The Temperature Test Class with a main method begins Here

class TemperatureTest{

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

//Prompt User for value in fahrenheit

System.out.println("Enter·a·Fahrenheit·temperature:");

double ftem = in.nextDouble();

//Making the instance of the Temperature class

Temperature temp = new Temperature(ftem);

//Outputing Results

System.out.println("The·temperature·in·Fahrenheit·i: "+ftem);

System.out.println("The·temperature·in·Celcius·is: "+ temp.getCelcius(ftem));

System.out.println("The·temperature·in·Kelvin·is: " + temp.getKelving(ftem));

}

}

Step-by-step explanation:

This is implemented in Java programming Language

Explanations are provided as comments within the code

User Bobthecow
by
5.0k points