Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Won't display calculated fahrenheit and celsius when running.

1. ## Won't display calculated fahrenheit and celsius when running.

Please help, can't figure out why it wont display. I think it has something to do with the setter method for DegreesInCelsius and fahrenheit. on output the answers for temperature 1 fahrenheit and temperature 2 celsius is incorrect.

```<
public class Temperature
{
// Instance variable
private double degreesInKelvin; // degrees in Kelvin
private double degreesInCelsius;
private double degreesInFahrenheit;

// Constructor method: initialize degrees in Kelvin to zero
public Temperature()
{
degreesInKelvin = 0;
degreesInCelsius = 0;
degreesInFahrenheit = 0;
}

// Convert and save degrees in Celsius in the Kelvin scale
public void setDegreesFromCelsius(double celsius)
{
degreesInKelvin = celsius + 273.15;
}

// Convert and save degrees in Fahrenheit in the Kelvin scale
public void setDegreesFromFahrenheit(double fahrenheit)
{
degreesInKelvin = (5.0 / 9.0) * (fahrenheit - 32) + 273.15;
}

// Convert and save degrees in Celsius in the Kelvin scale
public void setDegreesToCelsius(double fahrenheit)
{
degreesInCelsius = (fahrenheit - 32) * (5 / 9);
}

// Convert and save degrees in Fahrenheit in the Kelvin scale
public void setDegreesToFahrenheit(double celsius)
{
degreesInFahrenheit = (celsius - 273.15) * (9/5) + 32;
}

// Getter method returns the degrees in Kelvin
public double getDegreesInKelvin()
{
return degreesInKelvin;
}

public double getDegreesInCelsius()
{
return degreesInCelsius;
}

public double getDegreesInFahrenheit()
{
return degreesInFahrenheit;
}
}>```

```<
import java.util.Scanner;
public class TemperatureDemo
{
public static void main(String args[])
{

final double MARGIN_OF_ERROR = 0.0001;
// Declare variables
double degrees;
Temperature temperature1 = new Temperature();
Temperature temperature2 = new Temperature();

// Create a Scanner object to read from the keyboard
Scanner keyboard = new Scanner (System.in);

// Get the first temperature in Celsius from the user
System.out.print("Enter a temperature in Celsius: ");
degrees = keyboard.nextDouble();
temperature1.setDegreesFromCelsius(degrees);

// Get the second temperature in Fahrenheit from the user
System.out.print("Enter a temperature in fahrenheit: ");
degrees = keyboard.nextDouble();
temperature2.setDegreesFromFahrenheit(degrees);

double difference = temperature1.getDegreesInKelvin() - temperature2.getDegreesInKelvin();

// Display the first temperature in Kelvin
System.out.println("\nTemperature 1 in Kelvin is " + temperature1.getDegreesInKelvin());
System.out.println("Temperature 1 in Ceslius is " + temperature1.getDegreesInCelsius());
System.out.println("Temperature 1 in Fahrenheit is " + temperature1.getDegreesInFahrenheit());

// Display the second temperature in Kelvin
System.out.println("\nTemperature 2 in Kelvin is " + temperature2.getDegreesInKelvin());
System.out.println("Temperature 2 in Ceslius is " + temperature2.getDegreesInCelsius());
System.out.println("Temperature 2 in Fahrenheit is " + temperature2.getDegreesInFahrenheit());

if (Math.abs(difference) <= MARGIN_OF_ERROR)
{
System.out.println("\nThe two temperatures are equivalent");
}
else
{
System.out.println("The two temperatures are not equivalent");
}
}
}>```

2. ## Re: Won't display calculated fahrenheit and celsius when running.

Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

Can you post what your program outputs and add comments to the output showing what is wrong with it and show what you want the output to look like?

3. ## Re: Won't display calculated fahrenheit and celsius when running.

I tried running your program.. and I got this result:
Ex.

Temperature 1 in Kelvin is 303.15
Temperature 1 in Ceslius is 0.0
Temperature 1 in Fahrenheit is 0.0

Temperature 2 in Kelvin is 272.0388888888889
Temperature 2 in Ceslius is 0.0
Temperature 2 in Fahrenheit is 0.0

so if I'm not mistake your problem is getting the value of celsius and farenheit. You need to equate the input before computing the kelvin and invoke the method setDegreesToCelsius and setDegreesToFarenheit next to the code setDegreesFromCelsius and setDegreesFromFarenheit.. You need to cast the equation (5/9) to double so that the result will not always be zero.. take a look at this modified code:

public void setDegreesFromCelsius(double celsius)
{
degreesInCelsius = celsius; //equate the value of your parameter to the instance var
degreesInKelvin = celsius + 273.15;

}

// Convert and save degrees in Fahrenheit in the Kelvin scale
public void setDegreesFromFahrenheit(double fahrenheit)
{
degreesInFahrenheit = fahrenheit; //equate the value of your parameter to the instance var
degreesInKelvin = (5.0 / 9.0) * (fahrenheit - 32) + 273.15;

}

// Convert and save degrees in Celsius in the Kelvin scale
public void setDegreesToCelsius(double fahrenheit)
{

degreesInCelsius = (fahrenheit - 32) * (double)5 / (double)9; //cast the digits so that it will not result to zero. by default the data type of 5 and 9 are integer.
}

// Convert and save degrees in Fahrenheit in the Kelvin scale
public void setDegreesToFahrenheit(double celsius)
{
degreesInFahrenheit = (celsius - 273.15) * (9/5) + 32;
}

modifying the codes, the output will be like this:

Enter a temperature in Celsius: 40
Enter a temperature in fahrenheit: 50

Temperature 1 in Kelvin is 313.15
Temperature 1 in Ceslius is 40.0
Temperature 1 in Fahrenheit is -201.14999999999998

Temperature 2 in Kelvin is 283.15
Temperature 2 in Ceslius is 10.0
Temperature 2 in Fahrenheit is 50.0
The two temperatures are not equivalent