2 Replies - 1991 Views - Last Post: 11 December 2010 - 12:36 PM Rate Topic: -----

#1 dracer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-December 10

Temperature conversion

Posted 11 December 2010 - 12:20 PM

TOPIC: TEMPERATURE CONVERSION

I need help with my temperature conversion code..It seems that I'm am having a problme in my action event section... I cannot figure out the problem.. Well the main problem is when i put in a number in the fahrenheit field or textbox and press the convert 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...



Heres the code:
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.");

      }


}


This post has been edited by macosxnerd101: 11 December 2010 - 04:40 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Temperature conversion

#2 dracer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-December 10

Re: Temperature conversion

Posted 11 December 2010 - 12:30 PM

what i put after // it was only a comment i thought about putting it in then i tooken it out
Was This Post Helpful? 0
  • +
  • -

#3 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • Posts: 972
  • Joined: 30-September 10

Re: Temperature conversion

Posted 11 December 2010 - 12:36 PM

Hello there,

Just glancing at your code, the reason you are getting those answers is that all the Fahrenheit and Celcius variables are equal to 0 when you do the claculations. For example, when you try and convert Celcius to Fahrenheit, the Celcius variable used in the calculation is equal to 0. Therefore, try setting them before you do the calculations and see how you get on then. Perhaps using something like the double parsing lines you have commented out.

In this part;

Celsius = 0;
Fahrenheit = 0; //Fahrenheit = 0...set to a value given by a user. Perhaps by parsing a value from a textbox?
Celsius = ((5.0 / 9.0) * (Fahrenheit - 30));  ...Fahrenheit = 0, therefore calculation will be equal to -16.6666 always




Set Fahrenheit to a value given by the user (perhaps from a textbox???), instead of setting it equal to 0. Do something similar with the other conversion calculation.

This post has been edited by CodingSup3rnatur@l-360: 11 December 2010 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1