Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Problem create a Java program that repeatedly asks the user whether they wish to calculate another square root. If the response is y, then

The Problem

create a Java program that repeatedly asks the user whether they wish to calculate another square root. If the response is "y", then the program should proceed; if it is anything else, then the program should quit. Whenever it proceeds, the program should prompt the user for a number (a positivedouble, and your program may simply assume the input is consistent with this requirement) and then report the square root of that number to within a relative error of no more than 0.01%. The computation must be done usingNewton iteration.

The intuitive idea of Newton iteration for computing square roots is fairly straightforward. Suppose you have a guessrforx1/2that is too large; the argument is similar if it is too small. Ifris too large to be the square root ofx, thenx/rmust be too small, so the average ofrandx/rshould be a better guess than eitherrorx/r. This suggests that if you repeatedly replace your current guessrby(r + x/r)/2, then your sequence of guesses should converge tox1/2. And indeed it can be proved that it does. A good initial guess forx1/2is simplyr = x. If you continue updatingruntil |r2- x|/x < 2, then the relative error of the guessrwill be less than.

After your initial program works, there are a number of other requirements to change it slightly, one step at a time, as explained below.

Method

Note: this is the last project description that will include instructions about basic Eclipse operations you have practiced already; seeEnvironment SetupandGetting to Know Eclipse and Java. As always, we recommend that you start early, and ask (and answer) questions onPiazzaso everyone learns from everyone.

  1. Create a new Eclipse project by copyingProjectTemplate(if needed, seeCreating a New Project from a Project Templatefor details). Name the new projectNewton.
  2. Open thesrcfolder of this project and then open(default package). As a starting point you should useProgramWithIOAndStaticMethod.java. Rename itNewton1and delete the other files from the project (if needed, seeCreating a Program from a Skeleton (also Renaming a Java Program)for details).
  3. EditNewton1.javato satisfy the problem requirements stated above, including updating comments appropriately. Estimating the square root should be done in a static method declared as follows:

/**

* Computes estimate of square root of x to within relative error 0.01%.

*

* @param x

* positive number to compute square root of

* @return estimate of square root

*/

private static double sqrt(double x) {

...

}

  1. CopyNewton1.javato createNewton2.java. Changesqrt(including its Javadoc comments) so it also works whenx = 0. Note: if your code fromNewton1appears to work without any changes, but it is such that it might execute a division by 0, then it is not correct. Division by 0, in general, is undefined and you should not write code that attempts to compute it.
  2. CopyNewton2.javato createNewton3.java. Change it so the main program prompts the user to input the value of(rather than assuming it is 0.0001), just once as the program begins, and so this value is also passed tosqrt.
  3. CopyNewton3.javato createNewton4.java. Change it so the main program does not ask the user whether they wish to calculate another square root, but instead simply asks for a new value ofxand interprets a negative value as an indication that it's time to quit.

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

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

organizations that have the most progressive work schedules

Answered: 1 week ago

Question

U11 Informing Industry: Publicizing Contract Actions 317

Answered: 1 week ago