Trouble with fahrenheit to celsius conversion.
Hi there, first post on this board.
I'm just starting out with learning java, and i'm currently fiddling with the standard tutorial.
I did the one about the celsius -> fahrenheit converter, and thought i'd make it on better by adding the other way round, but it just won't work
Any suggestions are warmly welcomed.
Here's the source code from netbeans IDE:
Code :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Test;
/**
*
* @author Shitney Lee
*/
public class TestAppGUI extends javax.swing.JFrame {
/**
* Creates new form TestAppGUI
*/
public TestAppGUI() {
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() {
tempTextField1 = new javax.swing.JTextField();
convertButton1 = new javax.swing.JButton();
celsiusLabel1 = new javax.swing.JLabel();
fahrenheitLabel1 = new javax.swing.JLabel();
fahrenheitLabel2 = new javax.swing.JLabel();
celsiusLabel2 = new javax.swing.JLabel();
convertButton2 = new javax.swing.JButton();
tempTextField2 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Temperature Converter");
tempTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tempTextField1ActionPerformed(evt);
}
});
convertButton1.setText("Convert!");
convertButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
convertButton1ActionPerformed(evt);
}
});
celsiusLabel1.setText("Celsius");
fahrenheitLabel1.setText("Fahrenheit");
fahrenheitLabel2.setText("Fahrenheit");
celsiusLabel2.setText("Celsius");
convertButton2.setText("jButton1");
convertButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
convertButton2ActionPerformed(evt);
}
});
tempTextField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tempTextField2ActionPerformed(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.GroupLayout.Alignment.LEADING)
.addComponent(tempTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(convertButton1)
.addComponent(convertButton2)
.addComponent(tempTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(celsiusLabel2)
.addComponent(fahrenheitLabel2)
.addComponent(celsiusLabel1)
.addComponent(fahrenheitLabel1))
.addContainerGap(160, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {convertButton1, tempTextField1});
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {convertButton2, tempTextField2});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tempTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(celsiusLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(convertButton1)
.addComponent(fahrenheitLabel1))
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(fahrenheitLabel2)
.addComponent(tempTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(celsiusLabel2)
.addComponent(convertButton2))
.addContainerGap(74, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void tempTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void convertButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int tempFahr1;
tempFahr1 = (int)((Double.parseDouble(tempTextField1.getText()))
* 1.8 + 32);
fahrenheitLabel1.setText(tempFahr1 + " Fahrenheit");
}
private void tempTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void convertButton2ActionPerformed(java.awt.event.ActionEvent evt) {
int tempCels2;
tempCels2 = (int)((Double.parseDouble(tempTextField2.getText()))
- 32 * 5.0 / 9.0) ;
celsiusLabel2.setText(tempCels2 + " Celsius");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TestAppGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TestAppGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TestAppGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TestAppGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestAppGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel celsiusLabel1;
private javax.swing.JLabel celsiusLabel2;
private javax.swing.JButton convertButton1;
private javax.swing.JButton convertButton2;
private javax.swing.JLabel fahrenheitLabel1;
private javax.swing.JLabel fahrenheitLabel2;
private javax.swing.JTextField tempTextField1;
private javax.swing.JTextField tempTextField2;
// End of variables declaration
}
Re: Trouble with fahrenheit to celsius conversion.
Why doesn't it work? It works, your algorithm is just wrong.
This should work:
Code java:
private void convertButton2ActionPerformed(java.awt.event.ActionEvent evt) {
int tempCels2;
tempCels2 = (int) ((Double.parseDouble(tempTextField2.getText()) - 32) / 1.8);
celsiusLabel2.setText(tempCels2 + " Celsius");
}
Re: Trouble with fahrenheit to celsius conversion.
Quote:
Originally Posted by
HaHom Tsa Pfnuma
...
I did the one about the celsius -> fahrenheit converter, and thought i'd make it on better by adding the other way round
That's a Great Idea! Start with something that works and make changes to make sure you understand what's going on. I like that approach.
Quote:
Originally Posted by HaHom Tsa Pfnuma
won't work...suggestions
Inspect the statements that control the displayed values. The program does the hard part (getting user input, calculating a result based on that value, and then displaying the result). All you have to do is to make sure the calculations are correct. That's just a matter of math.
Anyhow...
So, the program converts Celsius to Fahrenheit correctly, right? Here's the formula.
DegreesF = DegreesC * 9.0/5.0 + 32.0
Compare it with the program statement that does the conversion. So, far, so good, right?
Now, solve (algebraically) for the other direction:
DegreesC = (DegreesF - 32.0) * 5.0/9.0
Compare this with your corresponding program statement.
Cheers!
Z
Re: Trouble with fahrenheit to celsius conversion.
Aha, the missing parenthesisses (if that's how you say it in plural, anyway, heh)!!! I've got to look closer at what i'm doing.
Thanks a lot, especially for the detailed explanation. I really appreciate you taking your time to explain it properly instead of just providing the solution; also gives a nice feeling to actually solve it by myself.