Sunday, January 24, 2016

Java Temperature Converter



//LAB ASSIGNMENT
//New project to allow user to convert temperature from celsius to fahrenheit
//and from fahrenheit to celsius. Provide a way to clear all.

package TempConverter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



/**
 *
 * @author hpaga
 */
public class TempConverterApp extends javax.swing.JFrame implements ActionListener {

    /**
     * Creates new form TempConverterApp
     */
    public TempConverterApp() {
        initComponents();
     
        //finally register the button with action Listener
        jbtnConvert.addActionListener(this);
        jbtnClear.addActionListener(this);
    }

// Variables declaration - do not modify                  
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton jbtnClear;
    private javax.swing.JButton jbtnConvert;
    private javax.swing.JLabel jlblFahrenheit;
    private javax.swing.JTextField jtxtCelTemp;
    // End of variables declaration                

    @Override
    public void actionPerformed(ActionEvent ae) {
         //Parse degrees Celsius as a double and convert to Fahrenheit.
        if (ae.getSource()==jbtnConvert){
         
            double Cel = Double.parseDouble(jtxtCelTemp.getText());
            double Ferh = Cel * 1.8 + 32;
            jlblFahrenheit.setText (Ferh + "'" + " Fahrenheit");
        }
        else if (ae.getSource() == jbtnClear){
            jtxtCelTemp.setText("");
            jlblFahrenheit.setText("Fahrenheit");
        }
         
    }
}

No comments:

Post a Comment