473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help simplify conversion code for fahrenhiet and Celcius

10
Does anyone know a way that i can simplify this code?

Here is my code. Basically it allows me to enter an integer and I can prompt the user to press one of two buttons which then converts the integer into Fahrenhiet or Celcius.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class FtoCConversionTest{
  6.     public static void main(String[] args){
  7.         EventQueue.invokeLater(new Runnable(){
  8.             public void run(){
  9.                 ConvertorFrame frame = new ConvertorFrame();
  10.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11.                 frame.setVisible(true);
  12.             }
  13.         });
  14.     }
  15. }
  16.  
  17. class ConvertorFrame extends JFrame{
  18.  
  19.     public ConvertorFrame(){
  20.         setTitle("Fahrenheit to Celsius Calculator");
  21.         ConvertorPanel panel = new ConvertorPanel();
  22.         add(panel);
  23.         pack();
  24.     }
  25. }
  26.  
  27. class ConvertorPanel extends JPanel{
  28.  
  29.     public ConvertorPanel(){
  30.  
  31.         setLayout(new BorderLayout());
  32.         //add the panel
  33.         panel = new JPanel();
  34.  
  35.         //add JTextField and position it
  36.         textField = new JTextField();
  37.         JPanel northPanel = new JPanel();
  38.         northPanel.setLayout(new GridLayout(2,1));
  39.         //add the text field label and position it
  40.         northPanel.add(new JLabel("Type the number to convert below: ", SwingConstants.LEFT));
  41.         northPanel.add(textField);
  42.         add(northPanel, BorderLayout.NORTH);
  43.  
  44.         //set the panel and position it
  45.         panel.setLayout(new GridLayout(1,2));
  46.         //add buttons and position them
  47.         cButton = new JButton("Convert to Celsius");
  48.         panel.add(cButton);
  49.         fButton = new JButton("Convert to Fahrenheit");
  50.         panel.add(fButton);
  51.         add(panel, BorderLayout.SOUTH);
  52.  
  53.         Converter  converter = new Converter();
  54.         cButton.addActionListener(converter);
  55.         fButton.addActionListener(converter);
  56.  
  57.     }
  58.     private class Converter implements ActionListener{
  59.  
  60.         public void actionPerformed (ActionEvent event){
  61.  
  62.             if (event.getSource() == cButton){
  63.                 int c = (int)(((Double.parseDouble(textField.getText()))- 32) * 0.56);
  64.                 textField.setText(c + " °C");
  65.                 textField.requestFocus();
  66.                 textField.selectAll();
  67.             }
  68.             else if(event.getSource() == fButton){
  69.                 int f = (int)(((Double.parseDouble(textField.getText()))* 1.8) + 32);
  70.                 textField.setText(f + " ºF");
  71.                 textField.requestFocus();
  72.                 textField.selectAll();
  73.             }
  74.         }
  75.     }
  76.     private JButton cButton, fButton;
  77.     private JTextField textField;
  78.     private JPanel panel;
  79. }
thanks in advance
Feb 17 '10 #1
1 1405
Curtis Rutland
3,256 Expert 2GB
It looks pretty simplified to me.
Feb 17 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: weasel | last post by:
Why is the Farenheit to Celsius part not working properly? Instead of showing a similar range of what the farenheit is listing, the celsius portion is showing half the range of farenheit. print...
6
by: Brian | last post by:
Help.. We cannot get this script to work..can someone take a look at it and make suggestions? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Convert...
4
by: nyy | last post by:
Hello everybody on this group. I have this program that is supposed to display a temp. conversion from fahrenheit to celsius, can anybody kindly can tell me what is wrong? Thanks. <!DOCTYPE html...
3
by: Paul M | last post by:
hi, i have an xml file that holds a URL string for an image location. how do i write the XSL code to display this image? thanks, PM
4
by: Paul M | last post by:
Larry, i've got an xml file which has multiple cities as the top level item. When i read it in using XMLReader, and define the variables, as below, it finds the correct number of cities in the...
4
by: Gary | last post by:
Hi, I have a temperature conversion program down pat, but I was told to add an average, meaning, i need to get the average temperature for as many times as it was entered. i do not know where to...
0
by: c#2006user | last post by:
Hi all Im converting code from vb.net to c# an im stuck again! :O) anyway ive included both vb and c# versions and if anyone could see where im going wrong it would much appreaciated. i've tried...
5
by: bratiskovci | last post by:
Hello, I am trying to write a few programs using python but don't really know where to start...I am completely CONFUSED. My first program deals with conversion between fahrenheit and celcius...
21
by: AsheeG87 | last post by:
Hey Everyone~ I'm still a C++ Rookie so please bear with me on this. I'm doing a temperature conversion program with prototype functions. Basicly, I was wondering if some of you would take a look...
2
by: silhutte75 | last post by:
Writing program to convert celcius to fahrenheit and vice versa. Have eliminated all errors but have four warnings and and stuck. Please help help help. snippet of code: printf("2. Please...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.