Answered step by step
Verified Expert Solution
Question
1 Approved Answer
JAVA PLEASE! In this week's lab, you will write an interactive program, SquareRootFinder, that computes the square root of a number to the nearest ten
JAVA PLEASE!
In this week's lab, you will write an interactive program, SquareRootFinder, that computes the square root of a number to the nearest ten billionths place, that is, 10 decimal digits. Definition 1. The square root of a number n, denoted n, is a number s whose square is equal to n; that is, if s2=n the s is the square root of n. There is an elementary algorithm used to find the square root of a number that takes advantage of a mathematical principle commonly referred to as the pinching (a.k.a. squeezing or sandwich) theorem. The theorem simply means that given any closed continuous interval [a,b], there is a number p such that apb. We can easily show that such a number, p, exists. 2a+b, the midpoint between a and b, satisfies the inequality. How does this relate to our square root algorithm? The square root of any number lies in a continous interval between two numbers. We know that n is between 0 and 1 if n is less than 1 and between 1 and n if n is greater than or equal to 1. We would like to keep narrowing, "squeezing", the initial interval to obtain better and better approximations of n. We can terminate the algorithm when our approximation is within some predetermined tolerance (). For example to find 100, we first consider the initial interval [1,100]. We then square 50.5, the midpoint of the interval. 50.550.5>100, so we can obtain a better approximation by squeezing our interval to its lower half, [1,50.5]. Next, we consider the midpoint of this interval. 25.7525.75>100, so again we consider the lower half of the interval, [1,25.75]. 13.37513.375>100, so we narrow the interval to [1,13.375], the lower half of the interval. 7.18757.1875 100 , so we narrow the interval to its lower half, [7.1875,10.28125], etc. If we continue to halve the interval to one containing the n, we will eventually obtain a midpoint whose square gives us 100. Due to truncation and round-off errors by computers, we pick a very small number, so that if the approximation is within that tolerance, we terminate the algorithm and return the midpoint as the 'best' approximation of the square root of 100 . The smaller the tolerance, the better the approximation. Our tolerance must be a small positive number. Formally, here is a pseudocode description of the square root algorithm: Iterative Square Root Approximation Algorithm: SquareRoot (n) Input: n - a nonnegative number Output: - the approximate square root of n,n if nnthenhighelselowenduntilhighlow> Algorithm 1: Iterative Square Root Approximation The variable n, is the number whose square root is to be found, low and high are endpoints of a continuous interval containing the square root of n, is the midpoint of the interval [low, high], and (epsilon), is the tolerance. In other words n+. When the length of the interval high low = is very close to 0 , then n=lim0()=. Do not use the standard Java library Math.pow or Math.sqrt method but you may use Math.abs. Additional Requirements To obtain a square of a number, multiply the number by itself. Avoid the use of unnecessary variables. Do not calculate the same quantity more than once - save the calculated value to memory and retrieve it from memory when it is needed. If a calculated quantity is used only once in the program, do not save it to a variable. Declare a constant EPSILON with the value 1E10 for in your program. That value of gives a square root within ten billionth of the exact square root. Avoid the use and evaluation of Boolean expressions that are not needed in determining the square root a number. The output should be as shown in the sample run. Each quantity in the table should be right aligned in three-column format and displayed to the nearest ten billionths place, 10 decimal digits. Use a width of twenty five for each column. - Preliminary Version : Write the program so that it reads a number and displays its square root as NaN if the number is negative or the initial interval, values of low and high, if the number is non-negative. For example, when the input is 36.5, the output is sqrt(-36.500000) =NaN, when the input is 0.5, the output is low =0.0000000000high=1.0000000000, and when the input is 25 , the output is low =1.0000000000 high =25.0000000000. Ensure that your program produces the correct output for negative numbers, non-negative numbers less than 1 and numbers greater than 1. Use the standard Java library constant Double.NaN for NaN. - Intermediate Version : Add a loop to determine the square root of the absolute values of numbers. Remove the print statement that printed the initial interval. Your program should now print the square root of the number. For example, when the input is 36.5, the output is sqrt( (36.5)=6.0415229868 and when the inpu t is 0.5, the output is sqr(0.5)=0.7071067811. - Final Version: Modify the program so that it prints a table as shown in the sample runs, including the tal le header. Each row of the table should consist of the approximation (),2, and MAX ERROR (high low ).T 1e printf method may come in handy when generating the table. Compile and run your program. It should be able to reproduce the correct output for the sampi runs shown below. Run the program for additional inputs including negative numbers, nornegative numbers less than 1 and positive numbers greater than 1. Remove all Netbean: auto-generated comments. Write header comments using the following Javadoc documentation template
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started