Java\'s Math library includes a method for calculating the square root of a numb
ID: 3629405 • Letter: J
Question
Java's Math library includes a method for calculating the square root of a number. If this method weren't available, how would we compute a square root?
One option is to use the algorithm devised by Newton, which computes a series of estimates that get closer and closer to the actual square root. Newton's algorithm begins with some initial estimate of the square root, and it repeatedly generates a closer estimate by performing a simple computation. If x is the number whose square root we are calculating and estimate is the current estimate of the square root, Newton's algorithm uses
(estimate + x/estimate)/2
as its next estimate. It repeats this computation multiple times, and each time it gets a more accurate estimate of the square root.
Your task is to write a program that uses Newton's algorithm to estimate the square root of a positive integer. We have again prepared some starter code, which is found in the file SquareRootCalculator.java. The code that we have given you obtains two values from the user:
the integer x whose root the program will compute
the number of times n that the estimate should be improved
Here again, you do not need to understand how the program obtains the values of these variables. You simply need to complete the program so that it uses these variables to estimate the square root.
Your code should use x/2 as the original estimate, and it should improve the estimate n times by repeatedly applying the formula above. The program should output all of the estimates that it computes, including the original estimate. Use a for loop to perform the necessary repetitions.
Here is a sample run of the program, with user inputs shown in red:
input a positive integer: 20
number of times to improve the estimate: 5
estimates of the square root of 20:
10.0
6.0
4.666666666666667
4.476190476190476
4.472137791286727
4.472135954999956
Note that we end up with six estimates: the original one (x/2), and the five improvements that the user requested.
Begin by downloading the following file: SquareRootCalculator.java. Use the File->Save As (or equivalent) option in your browser to put this file in the folder/directory that you're using for your work on this assignment, and open SquareRootCalculator.java in DrJava. Add your name and email to the comments at the start of the file, and complete the program by filling in the remainder of the main method.
Explanation / Answer
please rate - thanks
import java.util.*;
public class SquareRootCalculator {
public static void main(String[] args)
{int x; // the number whose root we wish to find
int n; // the number of times to improve the guess
double estimate;
// Read the values from the user.
Scanner scan = new Scanner(System.in);
System.out.print("input a positive integer (x): ");
x = scan.nextInt();
System.out.print("number of times to improve the estimate: ");
n = scan.nextInt();
/* * The lines above read the necessary values from the user
* and store them in the variables declared above.
* Fill in the rest of the program below, using those
* variables to compute and print the values mentioned
* in the assignment. */
estimate=x/2.;
for(int i=0;i<n;i++)
estimate=(estimate + x/estimate)/2.;
System.out.println("Square Root of "+x+" = "+estimate);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.