Embed presentation
Download to read offline
![Write a Java class to represent a temperature. The class has a single data field: temperatureC
The class has a single constructor that accepts an initial temperature provided as a double
argument If this argument it
Solution
import java.util.Scanner;
public class Temperature {
double setC;
Temperature (double setC){
this.setC = setC;
}
double getC()
{
return setC;
}
public double getF()
{
return ((double)9.0/5.0 * (setC + 32)); // Fahrenheit calculation
}
public double getK()
{
return ((double)setC + 273.15);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Please enter the initial temperature: ");
double setC = sc.nextDouble();](https://image.slidesharecdn.com/writeajavaclasstorepresentatemperature-230707194602-40bd7325/85/Write-a-Java-class-to-represent-a-temperature-The-class-has-a-single-pdf-1-320.jpg)

The document describes a Java class named 'temperature' that represents a temperature in Celsius. It includes methods to convert this temperature to Fahrenheit and Kelvin, and it has a main method that prompts the user for an initial temperature input. The document also demonstrates how to display the temperature in Celsius, Fahrenheit, and Kelvin based on user input.
![Write a Java class to represent a temperature. The class has a single data field: temperatureC
The class has a single constructor that accepts an initial temperature provided as a double
argument If this argument it
Solution
import java.util.Scanner;
public class Temperature {
double setC;
Temperature (double setC){
this.setC = setC;
}
double getC()
{
return setC;
}
public double getF()
{
return ((double)9.0/5.0 * (setC + 32)); // Fahrenheit calculation
}
public double getK()
{
return ((double)setC + 273.15);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Please enter the initial temperature: ");
double setC = sc.nextDouble();](https://image.slidesharecdn.com/writeajavaclasstorepresentatemperature-230707194602-40bd7325/85/Write-a-Java-class-to-represent-a-temperature-The-class-has-a-single-pdf-1-320.jpg)
