Please help me with my program
Hi I am new to Java and I am using jgrasp. My assignment is to convert celsius to fahrenheit and vice versa. I can't seem to get the right calculations, can somebody please see what is wrong?
Here is my driver:
Code:
import java.util.Scanner;
public class Driver
{
public static void main(String args[])
{
// Declare variables for user input
double degrees;
double Celsius;
String scale;
String endScale;
double getCelsius;
double getFahrenheit;
// Create a Scanner object to read from the keyboard
Scanner keyboard = new Scanner (System.in);
// Get the temperature and scale from user
System.out.print("Enter the temperature: ");
degrees = keyboard.nextDouble();
scale = keyboard.nextLine().trim();
// Declare and instantiate an object reference variable
Temperature outsideTemp = new Temperature();
// Set the outside temperature based on the user input
if (scale.equalsIgnoreCase("C"))
outsideTemp.setCelsius(degrees);
else
outsideTemp.setFahrenheit(degrees);
System.out.print("How would you like the temperature displayed (C or F)?");
endScale = keyboard.nextLine();
// Get the scale to display the temperature from the user
if (endScale.equalsIgnoreCase("C"))
System.out.print("The temperature is " + outsideTemp.getCelsius());
else
System.out.print("The temperature is " + outsideTemp.getFahrenheit());
// Display the temperature based on the user's desired scale
System.out.print(" degrees " + endScale);
}
}
Here is my class:
Code:
public class Temperature
{
// Instance variable
private double degreesKelvin; // degrees in Kelvin
double Celsius; // degrees in Celsius
double Fahrenheit;
// Constructor method: initialize degreesKelvin to zero
public Temperature()
{
degreesKelvin = 0;
}
// Convert and save degreesCelius in the Kelvin scale
public void setCelsius(double Celsius)
{
degreesKelvin = Celsius + 273.16;
}
// Convert degreesKelvin to Celsius and return the value
public double getCelsius()
{
Celsius = degreesKelvin - 273.16;
return Celsius;
}
// Convert and save degreesFahrenheit in the Kelvin scale
public void setFahrenheit(double Fahrenheit)
{
degreesKelvin = (( 5 / 9 ) * (Fahrenheit - 32)) + 273.16;
}
// Convert degreesKelvin to Fahrenheit and return the value
public double getFahrenheit()
{
Fahrenheit = ((degreesKelvin - 273.16) * (9 / 5 )) + 32;
return Fahrenheit;
}
}
Everything compiles, I just dont get the right answer.