import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class CelsiusTemperature extends JFrame implements ActionListener{
	private double readingInCelsius;
	private JButton button;
	private JTextField field;
	private JTextArea area;
	private JLabel label;

	public CelsiusTemperature(){
		setSize(500,200);
		setLocation(250,150);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		label = new JLabel("Celsius");
		field = new JTextField(20);
		button = new JButton("Convert");
		area = new JTextArea(10,20);

		Container container;
		container = getContentPane();
		container.setLayout(new FlowLayout());

		container.add(label);
		container.add(field);
		container.add(button);
		container.add(area);

		field.addActionListener(this);
		button.addActionListener(this);

   	public CelsiusTemperature(double celsius) {
    	readingInCelsius = celsius;
   	}

 	public double convertToFahrenheit() {
      	return (9.0/5.0)*readingInCelsius + 32.0;
  	}
}

     public void actionPerformed(ActionEvent event) {

			if (event.getSource() == button){
			int f = (int)(((Double.parseDouble(field.getText()))* 1.8) + 32);
            	        area.setText(field.getText() + " Celsius" + "=" + f + " Fahrenheit");
			}

     public static void main(String args[]) {
     CelsiusTemperature ct = new CelsiusTemperature();
	 ct.setVisible(true);
	}
}

I duno how to connect the two method = public CelsiusTemperature(double celsius) and public double convertToFahrenheit() into actionPerformed method. Does anyone can help me out?

Member Avatar for Javam

hmm, well first you need paramater of celsius in convertToFahrenheit() function.
Like: convertToFahrenheit(double celsius),
then,
:

if (event.getSource() == button){

int f = (int)(((Double.parseDouble(field.getText()))* 1.8) + 32);
double fahrenheitTemp = convertToFahrenheit(f);

area.setText(field.getText() + " Celsius" + "=" + fahrenheitTemp + " Fahrenheit");

}

Is this helpingful?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.