[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

Precision Error in Java

Hey,

Can someone help me with this one error I just don't get.  Why would I have a precision error on this.  My equation might be set up wrong.  Can you give me a tip please.

Code I have so far:

//      File Name:                        FahrenheitConverter.java
//      Programmer:                  Nikkita Kialand Miles
//      This program demonstrates the use of JButton, JTextField and JLabel
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FahrenheitConverter implements ActionListener
{      //      start class FahrenheitConverter
      JFrame converterFrame;
      JPanel converterPanel;
      JTextField tempFahrenheit;
      JLabel fahrenheitLabel, celsiusLabel;
      JButton convertTemp;

      //      Constructor
      public FahrenheitConverter( )
      {      //      start constructor
            //      Create the fame and container
            converterFrame = new JFrame("Convert Fahrenheit to Celsius");
            converterFrame.setSize(40, 40);
            converterPanel = new JPanel();
            converterPanel.setLayout(new GridLayout(2, 2));

            //      add widgets
            addWidgets( );

            //      add the panel to the frame
            converterFrame.getContentPane().add(converterPanel, BorderLayout.CENTER);

            //      Exit when the window is closed.
            converterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


            //      show converter.
            converterFrame.pack( );
            converterFrame.setVisible(true);
                        
      }      //      end constructor

      //      Create and add the widgets for converter
      private void addWidgets( )
      {      //      start addWidgets( )
            //      create widgets
            tempFahrenheit = new JTextField(2);
            fahrenheitLabel = new JLabel("Fahrenheit", SwingConstants.LEFT);
            convertTemp = new JButton("Convert...");
            celsiusLabel = new JLabel("Celsius", SwingConstants.LEFT);

            //      Listen to events from Convert button.
            convertTemp.addActionListener(this);

            //      add widgets to container.
            converterPanel.add(tempFahrenheit);
            converterPanel.add(fahrenheitLabel);
            converterPanel.add(convertTemp);
            converterPanel.add(celsiusLabel);

            fahrenheitLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
            celsiusLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
      }      //      end addWidgets( )
            //      Implementation of ActionListener interface.
      public void actionPerformed(ActionEvent event)
      {      //      start actionPerformed( )
            //      Parse degree Fahrenheit as a double and convert to Celsius
            int tempCels = (int)((Double.parseDouble(tempFahrenheit.getText( ))) - 32) * (.6) ;
            celsiusLabel.setText(tempCels  +  "Celsius");

      }      //      end actionPerformed( )
            
//      main method
  // main method
    public static void main(String[] args) {
      // Set the look and feel.
      try {
          UIManager.setLookAndFeel(
            UIManager.getCrossPlatformLookAndFeelClassName());
      } catch(Exception e) {}

      FahrenheitConverter converter = new FahrenheitConverter();
    }
}            
      
0
NikkitaKMiles
Asked:
NikkitaKMiles
  • 6
  • 6
  • 4
2 Solutions
 
aozarovCommented:
change
 int tempCels = (int)((Double.parseDouble(tempFahrenheit.getText( ))) - 32) * (.6) ;

to
 int tempCels = (int)( ( Double.parseDouble(tempFahrenheit.getText( ) - 32 )  * 0.6) ;

0
 
aozarovCommented:
Missing one bracket:
int tempCels = (int)( ( Double.parseDouble(tempFahrenheit.getText( )) - 32 )  * 0.6) ;

0
 
CEHJCommented:
Try

int tempCels = (int)((Double.parseDouble(tempFahrenheit.getText( )) - 32) * 0.6) ;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
aozarovCommented:
NikkitaKMiles, I provided you the same solution and before CEHJ.
Any reason you ignored it?
0
 
NikkitaKMilesAuthor Commented:
Sorry aozarov,

I got three or four solutions.  I just clicked on CEHJ's first.  I did not to ignore your solution I just saw his first.  Is there still a problem?  It's not personal, I just saw his solution first, tried it and it worked.  
0
 
aozarovCommented:
That is OK no harm done :-)
0
 
NikkitaKMilesAuthor Commented:
Thank you aozarov.

I will be more conscious about the order of the solutions I get next time.

Nikkita :-)
0
 
CEHJCommented:
NikkitaKMiles you should reopen and reassign the points to aozarov
0
 
NikkitaKMilesAuthor Commented:
This is what I am going to do as soon as I find out how to open a question.

I will assign 250 to aozarov and 250 to CEHJ.  In all fairness, I did not use aozarov's solution, but since everyone has something to say about how I assigned these points I will reassign this one time only.  I did not know there was a rule regarding assigning points.  I just wanted a simple answer and I received one.  Sorry for the confusion but once I give points they are assigned.  I will not do this again.  Now can we close this matter?

Nikkita
0
 
aozarovCommented:
Nikkita,
Don't bother!
That is fine. really :-)
0
 
CEHJCommented:
You don't need to do anything. I'll get the question reopened for you.

>> I did not know there was a rule regarding assigning points.

I would have thought it should be obvious, even if only in respect of 'natural justice' that the first person who submits an unequivocal answer should get all the points especially when the other person has merely duplicated the answer (if unintentionally in this case), adding absolutely nothing

0
 
NikkitaKMilesAuthor Commented:
Your "natural justice" does not explain my intentions.  As I stated earlier just reopen the question and I will take it from there. I don't need to be scolded for asking a programming question and making an error.  IT WAS UNINTENTIONAL!  

Nikkita
0
 
CEHJCommented:
>>IT WAS UNINTENTIONAL!  

I'm aware of that ;-)
0
 
CEHJCommented:
Thanks ;-)
0
 
CEHJCommented:
Oh well! Thanks ;-)
0
 
aozarovCommented:
Thanks and sorry for the trouble. :-)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 6
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now