8 Replies - 13148 Views - Last Post: 15 May 2011 - 10:46 AM Rate Topic: -----

#1 bsq935  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-October 08

Finding the square root with a While loop

Posted 24 November 2008 - 11:29 AM

I was all proud of myself for getting the square root to work, except that it doesn't find the square root for 1, so I must've done it wrong. Here's my assignment, just to make sure I interpreted it right...

Details

"To approximate the square root of a positive number n using Newton's method, you need to make an initial guess at the root and then refine this guess until it is "close enough." Your first initial approximation should be root = 1;. A sequence of approximations is generated by computing the average of root and n/root.

Use the constant:

private static final double EPSILON = .00001;

Your loop for findSquareRoot should behave like this:

make the initial guess for root
while ( EPSILON < absolute value of the difference between root squared and n )
calculate a new root
return root

Your class should have a constructor that takes the number you want to find the square root of. Implement the usual accessor method(s) and a findSquareRoot method that uses Newton's method described above to find and return the square root of the number. Add a method setNumber that takes a new number that replaces the number in the instance field. Supply a toString method that returns a string containing the number and the square root of the number. "

And here's what I have....

public class NewtonsSquareRoot{
  private static final double EPSILON = .00001;
  private int myNumber;
  private double root;
  private double guess;
  
  public NewtonsSquareRoot(int number){
	myNumber = number;
  }
  
  public int getNumber(){
	return myNumber;
  }
  
  public double findSquareRoot(){
	guess = 1;
	root = Math.sqrt(myNumber);
	
	while (EPSILON < Math.abs(Math.pow(root, 2) - myNumber))
	{
	  guess++;
	}
	return root;
  }
  
  public void setNumber(int number){
	myNumber = number;
  }
  
  public String toString(){
	String s = new String();
	s = "The square root of " + myNumber + " is " + root + ".";
   return s;
  }
}



Thanks in advance for helping :P

Is This A Good Question/Topic? 0
  • +

Replies To: Finding the square root with a While loop

#2 bsq935  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-October 08

Re: Finding the square root with a While loop

Posted 24 November 2008 - 01:55 PM

Oh wow just kidding. It works :)

But I do have another question...

In the test class, I'm supposed to "Use a while loop to generate the numbers 2 through 100. In the loop use the setNumber and toString methods to find and print the square roots of all the numbers from 2 to 100 inclusive."

I don't know what what the first step would look like. Is it like...

while ( x >= 2 && x <= 100)
?

This post has been edited by bsq935: 24 November 2008 - 01:56 PM

Was This Post Helpful? 0
  • +
  • -

#3 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

Re: Finding the square root with a While loop

Posted 24 November 2008 - 02:13 PM

Actually, your code doesnt work very well at all.

In your method findSquareRoot() you calculate the root with Math.sqrt(), enter a loop wich exits immediately, since Math.abs(Math.pow(root, 2) - myNumber = 0, and then return root.

The root is obviously the correct one, since Math.sqrt() was used, but im guessing thats not the way your teacher, and Newton for that matter, intended it to be done. =)

This post has been edited by LaFayette: 24 November 2008 - 02:18 PM

Was This Post Helpful? 0
  • +
  • -

#4 bsq935  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-October 08

Re: Finding the square root with a While loop

Posted 24 November 2008 - 02:32 PM

Thanks =/ I fail at programming.

I need a little more though. I took out the Math.sqrt() cuz I realized how silly that was, what else did I do wrong? I'm guessing it's the body of the While loop because I did the condition exactly the way he described it... Right?
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Finding the square root with a While loop

Posted 24 November 2008 - 02:37 PM

int x = 2;
while (x <= 100)
    //do your thing
    //increment x


Was This Post Helpful? 1
  • +
  • -

#6 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

Re: Finding the square root with a While loop

Posted 24 November 2008 - 03:05 PM

The body is okay if you change root to guess.
within the body you do a new guess with newtons method wich says that if you have a guess x0, you can always get a better guess x1 with:

x1 = x0 - (x0^2 - n)/(2*x0)

or in java

guess = guess - (Math.pow(guess,2) - n)/(2*guess);
Was This Post Helpful? 1
  • +
  • -

#7 bsq935  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-October 08

Re: Finding the square root with a While loop

Posted 24 November 2008 - 03:09 PM

Thanks so much guys, this class is a major challenge for me and I really appreciate the help.
Was This Post Helpful? 0
  • +
  • -

#8 ammaryasir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-May 11

Re: Finding the square root with a While loop

Posted 15 May 2011 - 10:34 AM

Rana Ammar Yasir Says you can find the square root of a number for integer in 3 lines
int x,y=1;
int z,n=36;
while(z<=n)
{
z = x*y;
x++;
y++;
}
S.o.p(+z);
Was This Post Helpful? 0
  • +
  • -

#9 ammaryasir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-May 11

Re: Finding the square root with a While loop

Posted 15 May 2011 - 10:46 AM

/*Finding the Square root of a number by Rana Ammar Yasir*/
public static void main(String[] args) {
int x = 1,y =1;
int n =36;int z = 1;
while(z<n)
{
z = x*y;
++x;
++y;
}
System.out.println(+x-1);
}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1