954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java novice PLEASE HELP!!!

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:

splifficus
Newbie Poster
2 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

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());


To.

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.

-Fredric

Daishi
Junior Poster in Training
80 posts since Aug 2005
Reputation Points: 10
Solved Threads: 2
 

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

splifficus
Newbie Poster
2 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You