import java.util.Scanner;
/*******************************************************************************/
/**
 * This is simple implementation of Newton's method for finding the square
 * root of a number.  We iterate
 *
 *       newApprox = (oldApprox + x/oldApprox)/2.0
 *
 * until the difference between the next and the previous approximation to the
 * root is smaller than some predetermine tolerance 'epsilon'.  For square roots
 * we can start with the value of the approximate root equal to the original
 * number.
 *
 * @author Duncan Buell
 * 4 February 2008
**/

public class MySquareRoot
{

// Note that there is no constructor.  For this particular purpose we can allow
// Java to supply 'the default constructor' since we don't use anything except
// this one method for root finding.  Again, please ignore some of this and
// concentrate on the loop control.  That's the important concept here.

/*******************************************************************************/
/**
 * Java method to find roots by Newton iteration.
**/
  public double findRoot(double x, double epsilon)
  {
    double newApprox,oldApprox;

    oldApprox = Double.POSITIVE_INFINITY;
    newApprox = x;

    while(epsilon < (Math.abs(oldApprox-newApprox)))
    {
      oldApprox = newApprox;
      newApprox = (oldApprox + x/oldApprox)/2.0;
      System.out.println("new approximation is " + newApprox);
    }

    return newApprox;
  }
} // public class MySquareRoot

