Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java question. The following method was known to the ancient Greeks for computing square roots. Given a value x > 0, and a guess g

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. The error is at line return squareRootGuess(x,g);

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(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

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);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions