Temperature Conversion

Fahrenheit to Celsius and vise versa

Page 1 of 1

14 Replies - 3036 Views - Last Post: 18 November 2009 - 10:04 PM Rate Topic: -----

#1 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Temperature Conversion

Post icon  Posted 17 November 2009 - 06:36 PM

Hello everyone,

I'm working on a program that suppose to let a user enter a number and converts it to Celsius or Fahrenheit when you click on a Celsius button or Fahrenheit button respectively. So far I have created the GUI but I'm not sure how to write the rest of the code. Help me please.
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;

public class FtoCConversionTest{
	public static void main(String[] args){
		EventQueue.invokeLater(new Runnable(){
			public void run(){
				ConvertorFrame frame = new ConvertorFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}
}

class ConvertorFrame extends JFrame
{
	public ConvertorFrame()
	{
		setTitle("Fehrenheit to Celsius converter");
		ConvertorPanel panel = new ConvertorPanel();
		add(panel);
		pack();
	}
}
	
class ConvertorPanel extends JPanel
{
	public ConvertorPanel()
	{
		
		setLayout(new BorderLayout());
	
		//add a text field and position it
		final JTextField textField = new JTextField();
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new GridLayout(2, 2));
		
		//add the text field label and position it
		northPanel.add(new JLabel("Type the number to convert below: ", SwingConstants.LEFT));
		northPanel.add(textField);
		//position the panel
		add(northPanel, BorderLayout.NORTH);
		
		//add JPanel
		panel = new JPanel();
		panel.setLayout(new GridLayout(1,2));
		
		//add buttons and position them
		addButton("Convert to Celsius", command);
		addButton("Convert to Fehrenheit", command );
		add(panel, BorderLayout.SOUTH);
	}
	
	private void addButton(String lable, ActionListener listener){
		
		JButton button = new JButton(lable);
		button.addActionListener(listener);
		panel.add(button);
	}
	private JPanel panel;
	private ActionListener command;
}	



Is This A Good Question/Topic? 0
  • +

Replies To: Temperature Conversion

#2 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: Temperature Conversion

Posted 17 November 2009 - 06:46 PM

Here's a whole tutorial on a Celsius Converter program.
I imagine to get from Celsius to Fahrenheit just reserve the formula. :^:
Java Celsius Converter Program
Was This Post Helpful? 0
  • +
  • -

#3 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Temperature Conversion

Posted 17 November 2009 - 06:54 PM

View Postjimdandy75, on 17 Nov, 2009 - 07:46 PM, said:

Here's a whole tutorial on a Celsius Converter program.
I imagine to get from Celsius to Fahrenheit just reserve the formula. :^:
Java Celsius Converter Program


I appreciate your suggestion, and I'm aware of the tutorial but I was hoping someone from here could offer me some hints or maybe snippets. Thanx again.
Was This Post Helpful? 0
  • +
  • -

#4 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: Temperature Conversion

Posted 17 November 2009 - 07:26 PM

View Postasdbabil, on 17 Nov, 2009 - 05:54 PM, said:

I appreciate your suggestion, and I'm aware of the tutorial but I was hoping someone from here could offer me some hints or maybe snippets. Thanx again.

Ok then, take the formula from the tutorial, and add it to your button Event Handler.
int tempFahr = (int)((Double.parseDouble(textField.getText()))
			* 1.8 + 32);

then find the formula to convert Fahrenheit to Celsius and do that on your other button?
Where exactly do you need the help, with event handlers?
Was This Post Helpful? 0
  • +
  • -

#5 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Temperature Conversion

Posted 17 November 2009 - 07:35 PM

View Postjimdandy75, on 17 Nov, 2009 - 08:26 PM, said:

View Postasdbabil, on 17 Nov, 2009 - 05:54 PM, said:

I appreciate your suggestion, and I'm aware of the tutorial but I was hoping someone from here could offer me some hints or maybe snippets. Thanx again.

Ok then, take the formula from the tutorial, and add it to your button Event Handler.
int tempFahr = (int)((Double.parseDouble(textField.getText()))
			* 1.8 + 32);

then find the formula to convert Fahrenheit to Celsius and do that on your other button?
Where exactly do you need the help, with event handlers?


Well, I'm familiar with the formulas but the problem I'm having is how to connect the two buttons to the formulas and then display the result
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Temperature Conversion

Posted 17 November 2009 - 08:48 PM

You make your listeners complicated
Here is how actionListener and actionPerformed work together

class ConverterFrame extends JFrame implements ActionListener {

	JButton toCelsius, toFar;

	ConvertFrame() {
	   toCelsius = new JButton("To Celsius");
	   toFar = new JButton("To farenheight");
	   add(toCelsius);
	   add(toFar);
	   toCelsius.addActionListener(this);
	   toFar.addActionListener(this):
	   ...

	 }

	 // called when a button is touch
	 public static void actionPerformed(ActionEvent e) {
		double value = .....get value from textField
		double translation;
		if(e.getSource() == toCelsius)
			  translation = value to celsius
		else
			   translation = value to farenheight

		 display translation in the JLabel
	 }


Was This Post Helpful? 1
  • +
  • -

#7 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: Temperature Conversion

Posted 17 November 2009 - 08:56 PM

pbl is right. this is what I came up with:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ConvertBoth {

	public static void main(String args[])
	{
		JFrame frame = new ConverterFrame();
		frame.setVisible(true);
	} 

} 

class ConverterFrame extends JFrame
{
	public  ConverterFrame()
	{
		setTitle("Fahrenheit-Celsius Converter");
		setLocationRelativeTo(null);
		setSize(510, 80);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel panel = new ConverterPanel();
		add(panel);

	} 

} 

class ConverterPanel extends JPanel implements ActionListener
{
	JTextField textField;
	JButton cel, far;
	JLabel converted;
	
	public ConverterPanel()
	{
		setLayout(new BorderLayout());

		JPanel displayPanel = new JPanel();
		displayPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

		textField = new JTextField("0", 10);
		displayPanel.add(textField); 
		
		JLabel celFar = new JLabel("# to Convert");
		displayPanel.add(celFar);
		
		cel = new JButton("-> Celsius");
		displayPanel.add(cel); 
		cel.addActionListener(this);
		
		far = new JButton("-> Fahrenheit");
		displayPanel.add(far); 
		far.addActionListener(this);
		
		converted = new JLabel("---------");
		displayPanel.add(converted);

		add(displayPanel, BorderLayout.CENTER);
		
		textField.requestFocus();
		textField.selectAll();
	}


	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		
		if(source == cel)
		{
			int tempCel = (int)(((Double.parseDouble(textField.getText()))
			- 32) * .56);
			converted.setText(tempCel + " Celsius");
			textField.requestFocus();
			textField.selectAll();		
		}
		
		else if(source == far)
		{
			int tempFahr = (int)((Double.parseDouble(textField.getText()))
			* 1.8 + 32);
			converted.setText(tempFahr + " Fahrenheit");
			textField.requestFocus();
			textField.selectAll();
		}
	}
}

sorry I still suck at layout manager. :blink:
Was This Post Helpful? 1
  • +
  • -

#8 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Temperature Conversion

Posted 17 November 2009 - 09:20 PM

View Postjimdandy75, on 17 Nov, 2009 - 09:56 PM, said:

pbl is right. this is what I came up with:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ConvertBoth {

	public static void main(String args[])
	{
		JFrame frame = new ConverterFrame();
		frame.setVisible(true);
	} 

} 

class ConverterFrame extends JFrame
{
	public  ConverterFrame()
	{
		setTitle("Fahrenheit-Celsius Converter");
		setLocationRelativeTo(null);
		setSize(510, 80);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel panel = new ConverterPanel();
		add(panel);

	} 

} 

class ConverterPanel extends JPanel implements ActionListener
{
	JTextField textField;
	JButton cel, far;
	JLabel converted;
	
	public ConverterPanel()
	{
		setLayout(new BorderLayout());

		JPanel displayPanel = new JPanel();
		displayPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

		textField = new JTextField("0", 10);
		displayPanel.add(textField); 
		
		JLabel celFar = new JLabel("# to Convert");
		displayPanel.add(celFar);
		
		cel = new JButton("-> Celsius");
		displayPanel.add(cel); 
		cel.addActionListener(this);
		
		far = new JButton("-> Fahrenheit");
		displayPanel.add(far); 
		far.addActionListener(this);
		
		converted = new JLabel("---------");
		displayPanel.add(converted);

		add(displayPanel, BorderLayout.CENTER);
		
		textField.requestFocus();
		textField.selectAll();
	}


	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		
		if(source == cel)
		{
			int tempCel = (int)(((Double.parseDouble(textField.getText()))
			- 32) * .56);
			converted.setText(tempCel + " Celsius");
			textField.requestFocus();
			textField.selectAll();		
		}
		
		else if(source == far)
		{
			int tempFahr = (int)((Double.parseDouble(textField.getText()))
			* 1.8 + 32);
			converted.setText(tempFahr + " Fahrenheit");
			textField.requestFocus();
			textField.selectAll();
		}
	}
}

sorry I still suck at layout manager. :blink:


I appreciate your time and effort; your code is helpful but it doesn't convert to Fahrenheit and the layout is different :( ....I apologize if I wasn't clear but the program should convert to both ways...if you compile and run my original code, you'll see the layout it has to be.

This post has been edited by asdbabil: 17 November 2009 - 09:38 PM

Was This Post Helpful? 0
  • +
  • -

#9 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Temperature Conversion

Posted 17 November 2009 - 09:26 PM

View Postpbl, on 17 Nov, 2009 - 09:48 PM, said:

You make your listeners complicated
Here is how actionListener and actionPerformed work together

class ConverterFrame extends JFrame implements ActionListener {

	JButton toCelsius, toFar;

	ConvertFrame() {
	   toCelsius = new JButton("To Celsius");
	   toFar = new JButton("To farenheight");
	   add(toCelsius);
	   add(toFar);
	   toCelsius.addActionListener(this);
	   toFar.addActionListener(this):
	   ...

	 }

	 // called when a button is touch
	 public static void actionPerformed(ActionEvent e) {
		double value = .....get value from textField
		double translation;
		if(e.getSource() == toCelsius)
			  translation = value to celsius
		else
			   translation = value to farenheight

		 display translation in the JLabel
	 }



I wish I knew the easier way :)....I don't understand what goes where the dots are!
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Temperature Conversion

Posted 17 November 2009 - 09:28 PM

Double.parseDouble(textField.getText());
Was This Post Helpful? 0
  • +
  • -

#11 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Temperature Conversion

Posted 17 November 2009 - 09:49 PM

View Postpbl, on 17 Nov, 2009 - 10:28 PM, said:

Double.parseDouble(textField.getText());


Is your code a replacement for my ConvertorFrame and ConvertorFrame?
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Temperature Conversion

Posted 17 November 2009 - 09:58 PM

View Postasdbabil, on 17 Nov, 2009 - 08:49 PM, said:

View Postpbl, on 17 Nov, 2009 - 10:28 PM, said:

Double.parseDouble(textField.getText());


Is your code a replacement for my ConvertorFrame and ConvertorFrame?

It's a kind of a merge of your 2 classes Just showing the Buttons and their ActionListener and how it works
the import part was

public void actionPerformed(ActionEvent e) {
	if(e.getSource() == buttonWhatever)   // to know which button was clicked


I did not test that code ... just wrote it like that... didn't took care of the layout manager or if the buttons were inside a panl inside the frame
Was This Post Helpful? 1
  • +
  • -

#13 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: Temperature Conversion

Posted 18 November 2009 - 06:48 AM

View Postasdbabil, on 17 Nov, 2009 - 08:20 PM, said:

I appreciate your time and effort; your code is helpful but it doesn't convert to Fahrenheit and the layout is different :( ....I apologize if I wasn't clear but the program should convert to both ways...if you compile and run my original code, you'll see the layout it has to be.

Mine does convert both ways, but it's converting what's in the text box. If you convert to Celsius you have to assume what's in the box is Fahrenheit and vice versa. 0 Fahrenheit = -17 Celsius, and 0 Celsius = 32 Fahrenheit. I believe my formulas are correct. Now if I had to do it as an assignment or something, which I don't, because I'm not in school, I would definitely change the layout and make it more clear on how it works. It's more, just a quick example for you to work off of. :D
Was This Post Helpful? 1
  • +
  • -

#14 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Temperature Conversion

Posted 18 November 2009 - 09:55 PM

View Postjimdandy75, on 18 Nov, 2009 - 07:48 AM, said:

View Postasdbabil, on 17 Nov, 2009 - 08:20 PM, said:

I appreciate your time and effort; your code is helpful but it doesn't convert to Fahrenheit and the layout is different :( ....I apologize if I wasn't clear but the program should convert to both ways...if you compile and run my original code, you'll see the layout it has to be.

Mine does convert both ways, but it's converting what's in the text box. If you convert to Celsius you have to assume what's in the box is Fahrenheit and vice versa. 0 Fahrenheit = -17 Celsius, and 0 Celsius = 32 Fahrenheit. I believe my formulas are correct. Now if I had to do it as an assignment or something, which I don't, because I'm not in school, I would definitely change the layout and make it more clear on how it works. It's more, just a quick example for you to work off of. :D


Hi,
Just wanna let you know that your code was very helpful; I was able to fix mine by comparing it to yours. I appreciate your time and effort in helping me :)
this is my code after I fixed it:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FtoCConversionTest{
	public static void main(String[] args){
		EventQueue.invokeLater(new Runnable(){
			public void run(){
				ConvertorFrame frame = new ConvertorFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}
}

class ConvertorFrame extends JFrame{
	
	public ConvertorFrame(){
		setTitle("Fehrenheit to Celsius Calculator");
		ConvertorPanel panel = new ConvertorPanel();
		add(panel);
		pack();
	}
}
	
class ConvertorPanel extends JPanel{
	
	public ConvertorPanel(){
		
		setLayout(new BorderLayout());
		//add the panel
		panel = new JPanel();
		
		//add JTextField and position it
		textField = new JTextField();
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new GridLayout(2,1));
		//add the text field label and position it
		northPanel.add(new JLabel("Type the number to convert below: ", SwingConstants.LEFT));
		northPanel.add(textField);
		add(northPanel, BorderLayout.NORTH);
		
		//set the panel and position it
		panel.setLayout(new GridLayout(1,2));
		//add buttons and position them
		cButton = new JButton("Convert to Celsius");
		panel.add(cButton);
		fButton = new JButton("Convert to Fahrenheit");
		panel.add(fButton);
		add(panel, BorderLayout.SOUTH);
		
		Converter converter = new Converter();
		cButton.addActionListener(converter);
		fButton.addActionListener(converter);
		
	}
	private class Converter implements ActionListener{
		
		public void actionPerformed (ActionEvent event){
			
			if (event.getSource() == cButton){
				int c = (int)(((Double.parseDouble(textField.getText()))- 32) * 0.56);
				textField.setText(c + "");
				textField.requestFocus();
				textField.selectAll();
			}
			else if(event.getSource() == fButton){
				int f = (int)(((Double.parseDouble(textField.getText()))* 1.8) + 32);
				textField.setText(f + "");
				textField.requestFocus();
				textField.selectAll();
			}
		}
	}
	private JButton cButton, fButton;
	private JTextField textField;
	private JPanel panel;
}	


Was This Post Helpful? 0
  • +
  • -

#15 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: Temperature Conversion

Posted 18 November 2009 - 10:04 PM

View Postasdbabil, on 18 Nov, 2009 - 08:55 PM, said:

Hi,
Just wanna let you know that your code was very helpful; I was able to fix mine by comparing it to yours. I appreciate your time and effort in helping me :)

Excellent! Glad I could help :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1