• Post Reply Bookmark Topic Watch Topic
  • New Topic

Babylonin algorithm for square root with loop until >0.01  RSS feed

 
Rebecca Berry
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone! New to Java and have a homework question. I have created a program for square root and it appears to be working correctly. My professor has now requested the program keep looping until: "((old guess-guess)/old guess) is >0.01.  He would like the output to look as follows(using the number 15 as an example):
This program estimates square roots:
Enter an integer to estimate the square root of:
15
Current guess: 4.75
Current guess: 3.9539473684210527
Current guess: 3.8738122865399776
Current guess 3.872983434898095

The estimated square root of 15 is:  3.87

Any help on how to create this output would be great.   Thanks in advance!
Here is my code thus far:

 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just print guess inside the loop, just before the exit test.  And surely your exit test it "<0.01", not ">0.01".
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and welcome to the Ranch
 
Rebecca Berry
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help all! I am still having a little trouble however.  It is indeed >0.01. And according to the homework instructions the out put should appear exactly as I listed it in my initial question.  The number 15 is just used as an example when running.  When I run the program I currently have, and use the number 15 when doing so, it is only printing 1 current guess.  Here is an update of my code:

 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got an AND operator && with nothing to its right? Line 27.

Why have you got all your code in the main method? I see Fred has said the same about your dice class. You should have several methods, not just one. A BabylonianRoot class which takes a number as its field and calculates the root would be much more object‑oriented.
You also have some style problems. Your variable names don't make their meanings obvious. It is not obvious that sr might mean square root.
You are declaring several variables on the same line. Don't.
You are saying enter integer and reading a double in line 17.
Your if should not be all on one line. Look at our style suggestions.
I know people will disagree with me, but you should be able to write that loop so as to avoid while (true). How about do...while (...); The bit about over 99% and under 101% can go after while. You can refine that method by changing 0.99 to 0.999, 0.9999 etc.

How does that technique differ from Newton's approximation?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!