Results 1 to 7 of 7
  1. #1
    2o2
    2o2 is offline Member
    Join Date
    Sep 2008
    Posts
    31
    Rep Power
    0

    Default SquareRoot Jframe semi working

    here is my JFrame that calculates the square root of a number using the average method. it works fine with non perfect squares, but does not work with perfect squares. for some reason if you put in a number like 9 it will come out with like 3.000001232 which is wrong! How can i fix this? Thanks!

    Java Code:
    //package swing;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class SquareRoot implements ActionListener {
    
    JFrame frame;
    JPanel panel;
    JTextField number;
    JLabel sqrt;
    JButton CalcSQRT;
    
    public SquareRoot() {
    	//Create and set up the window.
    	frame = new JFrame("Change Celsius to Fahrenheit");
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setSize(new Dimension(180, 80));
    
    	//Create and set up the panel.
    	panel = new JPanel(new GridLayout(3,3));
    	//Add the items.
    	addItems();
    
    	//Set the default button.
    	frame.getRootPane().setDefaultButton(CalcSQRT);
    
    	//Add the panel to the frame.
    	frame.getContentPane().add(panel, BorderLayout.CENTER);
    	//Display the window.
    	frame.pack();
    	frame.setVisible(true);
    }
    private void addItems() {
    	//Create items.
    	number = new JTextField("Number");
    	sqrt = new JLabel("");
    	CalcSQRT = new JButton("Square Root");
    
    	//Listen to events from the Convert button.
    	CalcSQRT.addActionListener(this);
    	//Add the items to the container.
    	panel.add(number);
    	panel.add(sqrt);
    	panel.add(CalcSQRT);
    
    
    }
    public void actionPerformed(ActionEvent event) {
    	double inputNumber = (Double.parseDouble(number.getText()));
    	double regGuess = (inputNumber / 2);
    	double sqrtAverage = 0;
    	double divide = 0;
    	double squareGuess = 0;
    	boolean h = true;
    
    	while(h == true)
    	{
    		squareGuess = regGuess*regGuess;
    		if(((squareGuess) == inputNumber) || ((squareGuess) > (inputNumber - .01) && (squareGuess) < (inputNumber + .01)))
    		{
    			sqrt.setText(regGuess + "");
    			break;
    		}
    		divide = inputNumber/regGuess;
    		sqrtAverage = ((divide + regGuess)/2);
    		regGuess = sqrtAverage;
    	}
    
    }
    	
    private static void createAndShowGUI() {
    	SquareRoot converter = new SquareRoot();
    }
    public static void main(String[] args) {
    	javax.swing.SwingUtilities.invokeLater(new Runnable() {
    		public void run() {
    			createAndShowGUI();
    		}
    	});
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    it will come out with like 3.000001232 which is wrong! How can i fix this?
    Welcome to the wild and woolly world of floating point numbers on digital computers. I'm going to have to find my links to articles that will help you, but the problem here you will find out is not the results you're getting but your acceptance of the results. If you hang in there, you'll see what I'm talking about...

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    ah, I see that you've also given your program an accuracy delta of 0.01. This is going to degrade your results somewhat. You may wish to make this delta smaller and see what happens.

    Also, try to get rid of "magic numbers" if you can as they will make your program difficult to debug. I'd change this:
    Java Code:
          if (((squareGuess) == inputNumber) || 
              ((squareGuess) > (inputNumber - .01) && (squareGuess) < (inputNumber + .01)))
    to this:
    Java Code:
          if (((squareGuess) == inputNumber) || 
              ((squareGuess) > (inputNumber - ACCURACY) && (squareGuess) < (inputNumber + ACCURACY)))
    and have a constant at the top of the class:
    Java Code:
      private static final double ACCURACY = 0.00001;  // this smaller number will give better results
    Last edited by Fubarable; 09-29-2008 at 01:37 AM.

  5. #5
    2o2
    2o2 is offline Member
    Join Date
    Sep 2008
    Posts
    31
    Rep Power
    0

    Default

    no, but the accuracy thing shouldn't matter for numbers with perfect squares right?

    i have a pretty similar program in c++ that does the same thing but works with perfect squares

    Java Code:
    #include "iostream"
    
    using namespace std;
    bool h = 1;
    double i = 0;
    double j = 0;
    double k = 0;
    double l = 0;
    int main()
    {
    b:
    	cout<<"Enter number to square root ['1' to exit]: ";
    	cin>>i;
    	j = (i/2);
    	if (i == 1)
    	{
    		h = 0;
    	}
    	if (i == 0)
    	{
    		cout<<"Incorrect Command"<<endl;
    		system("Pause");
    		h = 0;
    	}
    	while(h == 1)
    	{
    		double x = j*j;
    		if(((x) == i) || ((x) > (i - .0001) && (x) < (i + .0001)))
    		{
    			cout<<"The square root is: "<<j<<endl;
    			goto b;
    		}
    		l = i/j;
    		k = ((l + j)/2);
    		j = k;
    	}
    	return 0;
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes it should matter for perfect squares since you are approximating a solution here regardless of whether or not the number is a perfect square or not. You'll also notice that your C program uses a delta that is 2 orders of magnitude greater than the Java program. Why not vary the delta for both programs just to see what would happen.

  7. #7
    2o2
    2o2 is offline Member
    Join Date
    Sep 2008
    Posts
    31
    Rep Power
    0

    Default Fixed it!

    I fixed it, just rounded the answer to 4 decmal places and it works now!

Similar Threads

  1. how to use JFrame in JSP ??
    By priyanka sharma in forum JavaServer Pages (JSP) and JSTL
    Replies: 8
    Last Post: 04-06-2011, 04:51 AM
  2. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  3. jframe
    By amith in forum AWT / Swing
    Replies: 1
    Last Post: 05-15-2008, 10:03 AM
  4. Help with JFrame
    By Albert in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 04:44 AM
  5. Working With ANT
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 08:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •