Spring Semester 2003

Homework Assignment Three: Square Roots

Solve problem P6.12 in Big Java p. 278.

Here's the text of the problem:

The Heron method is a method for computing square roots that was known to the ancient Greeks. If `x` is a guess for the value `Math.sqrt(a)`, then the average of `x` and `a/x` is a better guess.

The problem asks to implement a class `RootApproximator` that starts with an initial guess of `1` and whose `nextGuess` method produces a sequence of increasingly better guesses. Supply a method `hasMoreGuesses` that returns `false` if two successive guesses are sufficiently close to each other. Then test your class like this:

```RootApproximator r = new RootApproximator(n);
while (r.hasMoreGuesses())
System.out.println(r.nextGuess()); ```
(Doesn't this look awfully similar to the way we use `StringTokenizer`s?

The above is your Homework Three.

Needless to say you are not allowed to make any use of `Math.sqrt(...)` in your program.

What follows (below) is FYI only, and not part of the homework.

Traditionally Homework Three was like that.

(However we have since posted a solution, see the link above).

Can you answer that last question though in the posted solution?

Last updated: Mar 2, 2003 by Adrian German for A201