Java Program The ancients had a recursive method for calculating a square root:
ID: 3674218 • Letter: J
Question
Java Program
The ancients had a recursive method for calculating a square root: Suppose you want to find the square root of x, guess that the square root is x/2. Test your guess (what's the difference between x and guess*guess). If your guess isn't accurate enough, try again. Your new guess should be 0.5 * (last guess + X/last guess). Keep going until your guess is close enough. Your class should include a recursive method for calculating the square root using the Babylonian Method. Users of your class should be able to specify the number they'd like to take a square root of as well as how much error they will tolerate in the square root estimation. For testing purposes, you should print the output.Explanation / Answer
/*
Purpose: This program computes a number's square root using the Babylonian method.
General Design: Four methods make up the program. promptUser gets input from the user and runs the computations.
computeStartingNumber computes the number to use at the start of the algorithm. computeBabylonianRoot runs the
algorithm. computeError computes the error between the estimate and the actual square root.
*/
import java.util.Scanner;
import java.util.ArrayList;
public class SquareRoot {
public static void main(String[] args)
{
promptUser("y");
}
/*
Computes the estimate of the square root. It recursively calls itself until the error bounds are met.
@params square is the number of which to estimate the square root, estimateIterations is an array of the estimate
and the number of iterations, and error is the error bound entered by the user.
@return the array of the estimate and the number of iterations.
*/
public static double[] computeBabylonianRoot(int square, double[] estimateIterations, double error) {
double iterations = estimateIterations[1] + 1;
double[] newEstimate = {(estimateIterations[0] + (square / estimateIterations[0])) / 2.0, iterations};
double newError = computeError(square, newEstimate[0]);
if (newError > error && newError > 0) {
double[] answer = computeBabylonianRoot(square, newEstimate, error);
return answer;
} else if (newError < 0 && newError * -1 > error) {
double[] answer = computeBabylonianRoot(square, newEstimate, error);
return answer;
} else {
return newEstimate;
}
}
/*
Computes the error of the estimate and the actual square root.
@params square is the number of which to take the square root. estimate is the current estimate.
@return the error between the estimate and actual square root.
*/
public static double computeError(int square, double estimate) {
double actualRoot = Math.sqrt(square);
double newError = (estimate - actualRoot) / actualRoot;
return newError;
}
/*
Computes the starting number for the Babylonian method.
@param square is the number of which to take the square root.
@return the starting estimate
*/
public static int computeStartingNumber(int square) {
int length = Integer.toString(square).length();
int startingEstimate = 2;
if (length % 2 == 0) {
int iterations = (length - 2) / 2;
startingEstimate = 3 * startingEstimate;
for (int i = 1; i <= iterations; i++) {
startingEstimate = startingEstimate * 10;
}
} else {
int iterations = (length - 1) / 2;
for (int i = 1; i <= iterations; i++) {
startingEstimate = startingEstimate * 10;
}
}
return startingEstimate;
}
/*
Asks the user for the relative error they want.
@param none
@return the user-specified relative error
*/
public static double getRelativeError() {
Scanner secondIn = new Scanner(System.in);
System.out.println("Within what relative error?");
double relativeError;
if (!secondIn.hasNextDouble()) {
System.out.println("Error, starting over.");
relativeError = getRelativeError();
} else {
relativeError = secondIn.nextDouble();
if (relativeError <= 0) {
System.out.println("Error must be positive.");
relativeError = getRelativeError();
}
}
return relativeError;
}
/*
Gets user input and calls the algorithm. It recursively calls itself as long as the user inputs "y".
@param input is whether the user wants to run the algorithm or exit the program
@return void
*/
public static void promptUser(String input) {
if (input.equals("y")) {
Scanner in = new Scanner(System.in);
System.out.println("What number would you like to take the square root of?");
System.out.println("Only positive integers will be recorded.");
// Initializes ArrayList
ArrayList<Integer> startingIntegers = new ArrayList<Integer>();
// Populates ArrayList
int beginSquare = in.nextInt();
startingIntegers.add(beginSquare);
// Gets relative error from user
double relativeErrorInput = getRelativeError();
// Computes square root for elements in ArrayList
for (int square : startingIntegers) {
int startingEstimate = computeStartingNumber(square);
double[] inputArray = {startingEstimate, 0};
double[] test = computeBabylonianRoot(square, inputArray, relativeErrorInput);
System.out.println("For " + square + ", the answer is " + test[0]);
System.out.println("It took " + test[1] + " iterations.");
}
// Asks user if they want to run the program again
System.out.println(" Compute another? (y/n)");
// Instantiates a new Scanner
// This is ugly but I was having problems using the same variable in as above
Scanner newIn = new Scanner(System.in);
String newUserInput = newIn.next();
promptUser(newUserInput);
} else if (input.equals("n")) {
System.out.println("Goodbye!");
} else {
// If input is not y or n I just ended the program
// Could also do a while loop until they enter y or n
System.out.println("Error, shutting down.");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.