Question: Problem a ( LA 6 a . java ) Write a program to compute the square root of a number. DO NOT USE any math

Problem a (LA6a.java) 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 root1. 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/62=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.33332=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. Sample runs: Enter a value for which to take the square root: 16 Enter an initial guess as to the answer: 6 Enter an error tolerance for the calculation: .5 sqrt(16.00000) ~ 4.01282 Enter a value for which to take the square root: -12 Value must be positive. Enter a value for which to take the square root: 12 Enter an initial guess as to the answer: -1 Guess must be positive. Enter an initial guess as to the answer: 1 Enter an error tolerance for the calculation: -1 Error tolerance must be positive. Enter an error tolerance for the calculation: 1 sqrt(12.00000) ~ 3.52433

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!