10 Replies - 9373 Views - Last Post: 01 January 2012 - 09:46 PM Rate Topic: -----

#1 pcpeterchen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 22-December 11

Square Root Java Program

Posted 30 December 2011 - 09:06 PM

Hello all,
I'm having trouble with my while loop in the sqrt method. Any ideas on how to make it work?

thanks in advance!!


//Program requirements: user inputs a number and the program outputs a square root.
/*Program design:  Calculators work by using the Newton's Method or Babylonian method by tral and error.  
  You begin with variable root x and a starting x0.  
  Then the average of the over estimate and the underestimate will eual an approximation close to the square root.  
  If you repeat the proccess with the new approximation you get an even closer value.  
  If the process is repeated, eventually you will get within a certain accuracy.*/
// Calculators only have 8 decimall places, so I'll go to 8 points of accuracy.
//^^/> this is my own thinking, not real instructions.
import java.util.Scanner;
public class SquareRoot
{
  public static void main (String[] args)
  {
    Scanner scan = new Scanner(System.in);
    Sqrt ans = new Sqrt();
    System.out.println("Please enter your number.");
    double num = scan.nextDouble();
   System.out.println(ans.sqrt(num));
  }
}


import java.util.Scanner;
import java.lang.Math;
public class Sqrt
{
  public double sqrt(double num)
  {
    double x = 1;
    double y=1;
    while(Math.abs(x-y) < 0.00001)
    {
      y=x;
      x = 0.5*(x + num/ x);
    }
    return x;
  }
}import java.util.Scanner;
import java.lang.Math;
public class Sqrt
{
  public double sqrt(double num)
  {
    double x = 1;
    double y=1;
    while(Math.abs(x-y) < 0.00001)
    {
      y=x;
      x = 0.5*(x + num/ x);
    }
    return x;
  }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Square Root Java Program

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Square Root Java Program

Posted 30 December 2011 - 09:16 PM

You will have to input from the user before building your Sqrt object that actually expects a double as parameter

and NOT a good idea at all to have a method named sqrt in a class named Sqrt
it is very confusing

*Edited: thus my error

This post has been edited by pbl: 30 December 2011 - 09:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Square Root Java Program

Posted 30 December 2011 - 09:31 PM

public class SquareRoot
{
  public static void main (String[] args)
  {
    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter your number.");
    double num = scan.nextDouble();
    Sqrt ans = new Sqrt(num);
   System.out.println(ans.calculate());
  }
}

public class Sqrt
{
  double num;
  public Sqrt(double num)
  {
    this.num = num;
  }

  double calculate() }
    double x = 1;
    double y=1;
    while(Math.abs(x-y) < 0.00001)
    {
      y=x;
      x = 0.5*(x + num/ x);
    }
    return x;
  }
}

Was This Post Helpful? 0
  • +
  • -

#4 pcpeterchen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 22-December 11

Re: Square Root Java Program

Posted 31 December 2011 - 11:49 AM

thanks a bunch!!! ^____^
Was This Post Helpful? 0
  • +
  • -

#5 pcpeterchen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 22-December 11

Re: Square Root Java Program

Posted 31 December 2011 - 11:55 AM

but... it's still not outputting the correct answer!!!
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Square Root Java Program

Posted 31 December 2011 - 01:48 PM

Please post your updated code and be specific about how the program's results are incorrect or do not meet your expectations. I'm sure it's a simple fix, but without your code we'd be guessing at it.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Square Root Java Program

Posted 31 December 2011 - 02:42 PM

You only want to multiply the second term by 0.5, not the entire expression. Remember that:
x[i+1] = x[i] + f(x[i])/f'(x[i]).

I have a tutorial on the Newton method for more information.
Was This Post Helpful? 0
  • +
  • -

#8 pcpeterchen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 22-December 11

Re: Square Root Java Program

Posted 01 January 2012 - 08:41 PM

by the way... how do I delete an account on dream.in.code?
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Square Root Java Program

Posted 01 January 2012 - 08:47 PM

You can't. Just don't log in.
Was This Post Helpful? 0
  • +
  • -

#10 pcpeterchen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 22-December 11

Re: Square Root Java Program

Posted 01 January 2012 - 09:03 PM

k thanks!
Was This Post Helpful? 0
  • +
  • -

#11 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 247
  • Joined: 21-June 11

Re: Square Root Java Program

Posted 01 January 2012 - 09:46 PM

I'd hate to go off topic, but am I the only one who finds it amusing that someone is asking on Dream In Code how to delete their account right after having their question asked?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1