Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program to compute the square root of a number. DO NOT USE any math libraries/methods in this program. You will be using the

Write a program to compute the square root of a number. DO NOT USE any math libraries/methods in this program. You will be using the Babylonian method (a.k.a. Herons method) to approximate the square root . To calculate the square root of x, the Babylonian method requires three inputs: x, an initial guess for the square root, and the error tolerance. It uses a repetitive calculation to get closer and closer to the actual value of the square root: after each iteration, the method checks if the absolute value of the difference between nextGuess and lastGuess is less than the error tolerance; if so, it stops and returns the value of nextGuess as the square root, otherwise if the difference between nextGuess and lastGuess is larger than the error tolerance, it repeats the calculation. Here is an example (x is 16, the initial guess is 6, and the error tolerance is 0.5): The first iteration computes nextGuess to be 6+16/6 = 4.3333. Next, check the difference between the lastGuess (6) and nextGuess (4.3333). The absolute value of the difference is 1.6667, which is greater than the error tolerance of 0.5. So, repeat the calculation. The second iteration computes nextGuess to be 4.3333+16/4.3333 = 4.0128. Check the difference between lastGuess (4.3333) and nextGuess (4.0128). The absolute value of the difference is 0.3205, which is less than the error tolerance of 0.5. So, the procedure stops and returns 4.0128 as the square root of 16. Your program needs three methods: main, squareRoot, and absoluteValue. The main method should get all three inputs from the user (x, initial guess, and error), run the squareRoot method, and output the approximate value of the square root (using exactly five decimal places, rounding if necessary). As always, you must validate the users inputs all three must be positive. If the user enters a value that is not positive, then your main method should prompt them to enter the value again (and repeat until they enter a valid input). As you cannot use any math libraries/methods, you will also need to write your own absoluteValue method. Only the main method should interact with the user (getting inputs and displaying results). The squareRoot and absoluteValue methods must not contain any input/output statements.

Provided Code:

import java.util.Scanner;

public class LA6a { /** * Error to show if value is not positive. */ static final String E_VALUE = "Value must be positive."; /** * Error to show if guess is not positive. */ static final String E_GUESS = "Guess must be positive."; /** * Error to show if error tolerance is not positive. */ static final String E_TOLERANCE = "Error tolerance must be positive."; /** * Computes the absolute value of an input * * @param value input value * @return absolute value of input */ public static double absoluteValue(double x, double initialGuess, double errTolerance) { return 0; } /** * Computes the square root of a value via * the Babylonian method: * * 0) lastGuess := initialGuess * 1) Loop * a) nextGuess := (lastGuess + value/lastGuess) / 2 * b) difference := | lastGuess - nextGuess | * c) lastGuess := nextGuess * d) if difference < errorTolerance, quit loop * 2) return nextGuess * * @param x value for which to take the square root * @param initialGuess initial guess as to the answer * @param err error tolerance * @return square root */ public static double squareRoot(double x, double initialGuess, double errTolerance) { return 0; }

/** * Program execution point: * inputs value, initial guess, tolerance, * outputs sqrt(value) via the Babylonian method * * @param args command-line arguments (ignored) */ public static void main(String[] args) { @SuppressWarnings("resource") final Scanner kbInput = new Scanner(System.in); double val; do { System.out.printf("Enter a value for which to take the square root: "); val = kbInput.nextDouble(); if (val <= 0) { System.out.printf("%s%n", E_VALUE); } } while (val <= 0); }

}

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions

Question

Create a table from the function y=3x+1; domain ={-1,0,1}

Answered: 1 week ago

Question

Name the R package used for implementing decision trees in R.

Answered: 1 week ago