i want to get correct output:
Output:
Enter temperature scale: F
Enter number of degrees: 212
The temperature in C is -17.777777777777 78
(My output of temperature in C should be 100)
Any help and response will be appreciate.
Regards!
Code:
public class Temperature
{
private String myScale; //valid values are "F" or "C"
private double myDegrees;
double celsius;
//default constructor
public Temperature()
{
myScale = "";
myDegrees = 0.0;
}
//constructor with specified degrees and scale
public Temperature(double degrees, String scale)
{
myDegrees = degrees;
myScale = scale;
}
//a method to convert Fahrenheit to Celsius
public Temperature toCelsius()
{
Temperature tempCelsius;
celsius = ((myDegrees - 32) * 5) / 9;
tempCelsius = new Temperature(celsius, "C");
return tempCelsius;
}
// a method to display the temperature
public void displayTemperature()
{
System.out.println("The temperature in C is " + celsius);
}
}
import java.util.Scanner;
public class TemperatureDemo
{
public static void main (String [] args)
{
Temperature tempCelsius;
Scanner input = new Scanner(System.in);
String scale = ""; double degree;
System.out.print("Enter temperature scale: ");
scale = input.next();
System.out.print("Enter number of degrees: ");
degree = input.nextDouble();
//Create a Temperature object based on user input
Temperature Answer = new Temperature();
//call toCelsius() method
Answer.toCelsius();
//call displayTemperature() method
Answer.displayTemperature();
}
}
Enter temperature scale: F
Enter number of degrees: 212
The temperature in C is -17.777777777777 78
(My output of temperature in C should be 100)
Any help and response will be appreciate.
Regards!
Comment