PDA

View Full Version : Java swing temperature converter



matmatmat
April 7th, 2009, 05:34 PM
I've created a temperature converter with netbeans:


/*
* temperature_converter.java
*
* Created on 07 April 2009, 17:09
*/



/**
*
* @author matio
*/
public class temperature_converter extends javax.swing.JFrame {

/** Creates new form temperature_converter */
public temperature_converter() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

ctof = new javax.swing.JRadioButton();
ftoc = new javax.swing.JRadioButton();
tempTextField = new javax.swing.JTextField();
tempLabel = new javax.swing.JLabel();
convertButton = new javax.swing.JButton();
convertedTempLabel = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

ctof.setText("Celcius to Fahrenheit");

ftoc.setText("Fahrenheit to Celcius");

tempLabel.setText("Temperature");

convertButton.setText("Convert");
convertButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
convertButtonActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(ctof)
.addComponent(ftoc)
.addGroup(layout.createSequentialGroup()
.addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(tempLabel))
.addGroup(layout.createSequentialGroup()
.addComponent(convertButton)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(convertedTempLabel)))
.addContainerGap(315, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(tempLabel)
.addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(convertButton)
.addComponent(convertedTempLabel))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 27, Short.MAX_VALUE)
.addComponent(ctof)
.addGap(18, 18, 18)
.addComponent(ftoc))
);

pack();
}// </editor-fold>

private void convertButtonActionPerformed(java.awt.event.Action Event evt) {
if (ctof.isSelected()){
//Parse degrees Celsius as a double and convert to Fahrenheit.
int tempFahr = (int)((Double.parseDouble(tempTextField.getText()) )
* 1.8 + 32);
convertedTempLabel.setText(tempFahr + " Fahrenheit");

}
else{
int tempCelc = (int)((Double.parseDouble(tempTextField.getText()) )
- 32 * 5/9);
convertedTempLabel.setText(tempCelc + " Celcius");
}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new temperature_converter().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton convertButton;
private javax.swing.JLabel convertedTempLabel;
private javax.swing.JRadioButton ctof;
private javax.swing.JRadioButton ftoc;
private javax.swing.JLabel tempLabel;
private javax.swing.JTextField tempTextField;
// End of variables declaration

}

when i run it and choose to convert 212 Fahrenheit to Celsius it says it is 195 when it should be 100.
What's wrong with the math bit ?

ajackson
April 7th, 2009, 05:48 PM
Goold old order of precedence (BODMAS or whatever the acronym is)

You basically have F - 32 * 5/9

It does the 5/9 first, then times that by 32, then subtracts that from F.

You need it rephrased as (F - 32) * 5/9

That forced the F - 32 to happen and then the times and divide.

matmatmat
April 7th, 2009, 06:07 PM
Thanks

Tony Flury
April 7th, 2009, 09:52 PM
BODMAS -
Brackets
Of (i.e. percentages)
Divide
Multiply
Add
Subtract

Krupski
April 7th, 2009, 11:00 PM
BODMAS -
Brackets
Of (i.e. percentages)
Divide
Multiply
Add
Subtract

I prefer "PPAE" (put parentheses around everything). :)

Firestom
April 7th, 2009, 11:24 PM
I'm used to using PEDMAS (Parenthèses, Exposants, Division, Multiplication, Addition, soustraction) but that won't work for english people. Here is a list of a full operator precdence in Java: http://www.uni-bonn.de/~manfear/javaoperators.php