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

Write in Java A common way of calculating the square root is by successive appro

ID: 653866 • Letter: W

Question

Write in Java

A common way of calculating the square root is by successive approximations. To calculate sqrt(x):

Make a guess for the square root, called guess0

Improve the guess using the formula: guessn = 1?2 * (guessn-1 + x ? guessn-1 )

If guessn2 = x, then we have found the square root

For example, to calculate sqrt(2) (assuming we care only about the first 5 decimal places):

We guess that sqrt(2) = 1

12 = 1 ? 2, so we improve the guess by:

1?2 (1 + 2?1) = 1.5

1.52 = 2.25 ? 2, so we improve the guess by:

1?2 (1.5 + 2?1.5) = 1.41667

1.416672 = 2.00700 ? 2, so we improve the guess by:

1?2 (1.41667 + 2?1.41667) = 1.41422

1.414222 = 2, so we have found the square root

Objective:

Create a class SquareRoot with the following methods:

public static double sqrtIter(double guess, double x)

Approximates the square root of x by improving the guess (recursively).
At each stage of the approximation, sqrtIter should print out the current guess.

public static double sqrt(double x)

which finds the square root of x by calling sqrtIter using an initial guess of 1

SquareRoot should also have a main method that asks the user for a number, and then prints the result of sqrt using that number.

Since you will be comparing doubles, you should use a TOLERANCE of 0.00001

Example Dialog:

Explanation / Answer

class squrerootapprox{

public static double sqrt(double x)

{

return sqrtIter(1.0, x);

}

public static double sqrtIter(double guess, double x)

{

System.out.println("the guess is "+ guess );

double c = a-b;

if (Math.abs(((guess-(return sqrltr(( ( (guess* guess) +x) /2*guess) ,x) ) -1.0) )<= 0.000001)

return guess;

}

public static void main(String []args){

System.out.println("enter the number whose sdquare root has to be found out " );

Scanner s=new scanner(System.in);

double y= s.nextDouble();

System.out.println("the square root of number " + y + "    is " + sqrt( y));

    }

}

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