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

Java question. The following method was known to the ancient Greeks for computin

ID: 3743767 • Letter: J

Question

Java question. The following method was known to the ancient Greeks for computing square roots. Given a value x > 0, and a guess g for the square root, a better guess is (g + x/g)/2. Write a recursive helper method public static squareRootGuess(double x, double g). If g2 is approximately equal to x (that is, less than .0001 difference between x and g), return g, otherwise, return squareRootGuess with the better guess. Then write a method public static squareRoot(double x) that uses the helper method

I wrote the program but I got an error and not sure how to fix it. Please help

import java.util.Scanner;

/**

Greek's method to approximate the square root of a given number.

*/

public class SquareRootComputer

{

public static void main(String[] args)

{

Scanner in = new Scanner(System.in);

System.out.print("Enter a number: ");

double x = in.nextDouble();

System.out.println(" The square root is " + squareRoot(x));

}

/**

computes the square root of the specify number

@param x a non-negative integer

@return the square root of the specify number

*/

public static double squareRoot(double x)

{

// Complete this helper method

  

return squareRootGuess(double x, double g); //error here

}

/**

Uses the Greek's method to approximate the square toot of a

given number.

@param x a non-negative integer

@param g a guess

*/

private static double squareRootGuess(double x, double g)

{

//Complete this recursive method

final double value = 0.001;

if (Math.abs(x-(g*g)) < value)

{

return g;

}

else

g = (g + x/g)/2;

g = Math.round(g*100.00)/100.00;

return squareRootGuess(x,g);

}

}

Explanation / Answer

import java.util.Scanner;

/**

Greek's method to approximate the square root of a given number.

*/

public class SquareRootComputer
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a number: ");
        double x = in.nextDouble();
        System.out.print("Enter the guess: ");
        double g = in.nextDouble();
        SquareRootComputer sqc = new SquareRootComputer();
        System.out.println(" The square root is " + sqc.squareRoot(x,g));
    }


/**

computes the square root of the specify number

@param x a non-negative integer

@return the square root of the specify number

*/

    public static double squareRoot(double x, double g)
    {
    // Complete this helper method
        return squareRootGuess(x, g); //error here
    }

/**

Uses the Greek's method to approximate the square toot of a

given number.

@param x a non-negative integer

@param g a guess

*/

    private static double squareRootGuess(double x, double g)
    {

//Complete this recursive method
        double value = 0.001;
        if (Math.abs(x-(g*g)) <= value)
        {
            return g;
        }
        else
        {

            g = (g + x/g)/2;
            return squareRootGuess(x,g);
        }
    }
}

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