Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

There are several techniques for implementing the sqrt method in Math class. On

ID: 3725108 • Letter: T

Question

There are several techniques for implementing the sqrt method in Math class. On such technique is known as the It approximates the square root of a number, n, by repeatedly performing calculation using the following formula: nextGuess-(lastGuess+n/ lastGuess)2 When nextGuess and lastGuess are almost identical, nextGuess is the approximated square root. The initial guess can be any positive value (for example 1). This value will be the starting value for lastGuess. If the difference between nextGuess and lastGuess is less than a very small number such as 0.0001, you can claim that nextGuess is the approximated square root of n. If not, nextGuess becomes lastGuess and the approximation process continues. Implement and test the following method that returns the square root of n: public static double sqrt(long n)

Explanation / Answer

Solution:-

double n = input.nextDouble();

double nextguess = n / 2;

double pctDiff = Double.MAX_VALUE;

double lastGuess = nextguess;

    while (Math.abs(pctDiff) >= 0.0001)

    {                       

        double r = n /nextguess;

        nextguess = (nextguess + r)/ 2;

        pctDiff = ((nextguess-lastGuess)/lastGuess);

        lastGuess = nextguess;

        System.out.println(nextguess);

    }

================================================================================

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote