This week's giveaway is in the Java/Jakarta EE forum.We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line!See welcome thread
Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Babylonin algorithm for square root with loop until >0.01

Greenhorn
Posts: 12
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:

Bartender
Posts: 585
9
Just print guess inside the loop, just before the exit test.  And surely your exit test it "<0.01", not ">0.01".

Marshal
Posts: 57499
175
...and welcome to the Ranch

Rebecca Berry
Greenhorn
Posts: 12
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: 57499
175
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?

 It is sorta covered in the JavaRanch Style Guide.