Just started a Java class and the first HW assingment was issued.
I have read the chapter a million times and cant figure out what the heck am I doing wrong. The assingment is supposed change Celcius temperature to Fahrenheit using a worker class.

import javax.swing.*;

public class ConvertTemperature {

   public static void main(String[] args) {
     // Get the  Celcius Temperature
     String getCelcius = JOptionPane.showInputDialog("Enter Celcius");
     double celcius = Double.parseDouble(getCelcius);
     // Create a CelciusToFahrenheit object that stores the celcius temperature.
     CelciusToFahrenheit you = new CelciusToFahrenheit(celcius);

     // Display Farenheight temperature.
     JOptionPane.showMessageDialog( null, you.surroundCelciusToFahreheit());

 class CelciusToFahrenheit {

   private double fahrenheit;

   public CelciusToFahrenheit (double celcius ) {
      double fahrenheit;
      fahrenheit = 1.8 * celcius + 32;

   public double surroundConvertTemperature() {
      return fahrenheit;


Someone PLEASE help! before I crack........ :cry:

Edited by pyTony: fixed formating

11 Years
Discussion Span
Last Post by splifficus

You should really use code brackets when pasting code, it makes it a lot easier to read.

I had to make two changes to make that work, the first was changing the line...

//  The function surroundCelciusToFahreheit doesn't even exist.
 JOptionPane.showMessageDialog( null, you.surroundCelciusToFahreheit());


JOptionPane.showMessageDialog( null, you.surroundConvertTemperature());

The next one isn't so obvious. You had declared a local variable by the same name of a member variable in the CelciusToFahrenheit class constructor. Don't do that, because anything you do with the variable declared locally will only change the local variable, and not the data member. So just remove the following line from that constructor...

double fahrenheit;

And after that it should work fine.



That did the trick! Thanks for the tip dude, it saved me a lot of headaches... :cheesy:

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.