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 LAajava Write a program to compute the square root of a number. DO NOT USE any math librariesmethods in this program. You will be using the Babylonian method aka 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 the initial guess is and the error tolerance is : The first iteration computes nextGuess to be Next, check the difference between the lastGuess and nextGuess The absolute value of the difference is which is greater than the error tolerance of So repeat the calculation. The second iteration computes nextGuess to be Check the difference between lastGuess and nextGuess The absolute value of the difference is which is less than the error tolerance of So the procedure stops and returns as the square root of 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 librariesmethods 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 inputoutput statements. Sample runs: Enter a value for which to take the square root: Enter an initial guess as to the answer: Enter an error tolerance for the calculation: sqrt ~ Enter a value for which to take the square root: Value must be positive. Enter a value for which to take the square root: Enter an initial guess as to the answer: Guess must be positive. Enter an initial guess as to the answer: Enter an error tolerance for the calculation: Error tolerance must be positive. Enter an error tolerance for the calculation: sqrt ~
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
