NEED HELP WITH TEMPERATURE
Code Java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Temperature2 extends JApplet implements ActionListener
{
private JTextField fahrenheitField, celsiusField;
private JLabel messageLabel;
private JButton fahrButton, celsButton, resetButton;
private double Celsius;
private double Fahrenheit;
public void init()
{
Container contentPane = getContentPane();
JLabel fahrenheitLabel, celsiusLabel, instructionsLabel;
JPanel fahrenheitPanel, celsiusPanel, resetPanel;
fahrenheitPanel = new JPanel();
celsiusPanel = new JPanel();
resetPanel= new JPanel();
instructionsLabel = new JLabel(" Temperature Converter Chart");
fahrButton = new JButton("convert to fahrenheit");
fahrButton.addActionListener(this);
fahrenheitPanel.add(fahrButton);
celsButton = new JButton("convert to celsius");
celsButton.addActionListener(this);
celsiusPanel.add(celsButton);
resetButton = new JButton("Reset");
resetButton.addActionListener(this);
resetPanel.add(resetButton);
fahrenheitLabel = new JLabel("Temperature in F:");
fahrenheitField = new JTextField(9);
fahrenheitPanel.add(fahrenheitLabel);
fahrenheitPanel.add(fahrenheitField);
celsiusLabel = new JLabel("Temperature in C:");
celsiusField = new JTextField(9);
celsiusPanel.add(celsiusLabel);
celsiusPanel.add(celsiusField);
contentPane.setLayout(new GridLayout(6, 1));
contentPane.add(instructionsLabel);
contentPane.add(fahrenheitPanel);
contentPane.add(celsiusPanel);
contentPane.add(fahrButton);
contentPane.add(celsButton);
contentPane.add(resetButton);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("convert to celsius"))
{
Celsius = 0;
Fahrenheit = 0;
Celsius = ((5.0 / 9.0) * (Fahrenheit - 30));
// double Celsius = Double.parseDouble(celsiusField.getText());
celsiusField.setText(Double.toString(Celsius));
System.out.println("Temperature in Celcius is:" + Celsius);
}
else if (e.getActionCommand().equals("Reset"))
{
Celsius = 0;
Fahrenheit = 0;
celsiusField.setText("0.0");
fahrenheitField.setText("0.0");
}
else if (e.getActionCommand().equals("convert to fahrenheit"))
{
Celsius = 0;
Fahrenheit = 0;
Fahrenheit = (Celsius * (9.0 / 5.0) + 32);
/**
double Celsius = Double.parseDouble(fahrenheitField.getText());
*/
fahrenheitField.setText(Double.toString(Fahrenheit));
System.out.println("Temperature in Fahrenheit is:" + Fahrenheit);
}
else
celsiusField.setText("Error in convertor code.");
}
}
Re: NEED HELP WITH TEMPERATURE
In the future please don't ask your same question in multiple categories. I've removed your other topic. What specifically are you having problems with?
If you're getting an error message, please post it in full.
Re: NEED HELP WITH TEMPERATURE
the problem is that there is no errors...it seems the conversion is not working properly..when i put in a number in the fahrenheit field or textbox and press the conver to celsius button the answer is always -16.666 and if i do the same and put a number in the celisus field or textbox the output for fahrenheit is always 32...
this is the problem
Re: NEED HELP WITH TEMPERATURE
Take a look at your actionPerformed method.
When the user presses the convert to fahrenheit or convert to celsius buttons, you're initializing the values to 0 rather than to the values in the other textbox.
0F = -16.666C and 0C = 32F.
Change these to parse the text from textbox into a number, then convert that number to the other temperature scale.
Re: NEED HELP WITH TEMPERATURE
do you mean like this..
Code :
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("convert to celsius"))
{
[COLOR="Red"] 0Celsius = 32Fahrenheit;
0Fahrenheit = -16.666Celsius;
Celsius = ((5.0 / 9.0) * (Fahrenheit - 30));[/COLOR]
// [COLOR="Red"]0Celsius = Double.parseDouble(celsiusField.getText());[/COLOR]
celsiusField.setText(Double.toString(Celsius));
System.out.println("Temperature in Celcius is:" + Celsius);
}
i'm clueless
Re: NEED HELP WITH TEMPERATURE
No, I was simply showing you where you were getting those numbers from. That's not valid Java code :P
Code Java:
if (e.getActionCommand().equals("convert to celsius"))
{
Fahrenheit = Double.parseDouble(fahrenheit.getText());
Celsius = ((5.0 / 9.0) * (Fahrenheit - 30));
celsiusField.setText(Double.toString(Celsius));
//System.out.println("Temperature in Celcius is:" + Celsius); this isn't useful in a GUI app
}
Your handler for converting Celsius to Fahrenheit will be similar to this, but you'll need to change the code around to convert celsius to fahrenheit, rather than convert fahrenheit to celsius.