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();
Temperature temp = new Temperature (setC);
System.out.println("The current temperature in Celsius is: " +temp.getC());
System.out.println("The current temperature in Fahrenheit is: " +temp.getF());
System.out.println("The current temperature in Kelvin is: " +temp.getK());
}
}
example:
Please enter the initial temperature: 23 The current temperature in Celsius is: 23.0 The current
temperature in Fahrenheit is: 99.0 The current temperature in Kelvin is: 296.15

Write a Java class to represent a temperature. The class has a single.pdf

  • 1.
    Write a Javaclass 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();
  • 2.
    Temperature temp =new Temperature (setC); System.out.println("The current temperature in Celsius is: " +temp.getC()); System.out.println("The current temperature in Fahrenheit is: " +temp.getF()); System.out.println("The current temperature in Kelvin is: " +temp.getK()); } } example: Please enter the initial temperature: 23 The current temperature in Celsius is: 23.0 The current temperature in Fahrenheit is: 99.0 The current temperature in Kelvin is: 296.15