Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your first job is to create a Java program that repeatedly asks the user whether they wish to calculate another square root. If the response

Your first job is to 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 positive double, 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 using Newton iteration.

The intuitive idea of Newton iteration for computing square roots is fairly straightforward. Suppose you have a guess r for x1/2 that is too large; the argument is similar if it is too small. If r is too large to be the square root of x, then x/r must be too small, so the average of r and x/r should be a better guess than either r or x/r. This suggests that if you repeatedly replace your current guess r by (r + x/r)/2, then your sequence of guesses should converge to x1/2. And indeed it can be proved that it does. A good initial guess for x1/2 is simply r = x. If you continue updating r until |r2 x |/x < 2, then the relative error of the guess r will 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; see Environment Setup and Getting to Know Eclipse and Java. As always, we recommend that you start early, and ask (and answer) questions on Piazza so everyone learns from everyone.

  1. Create a new Eclipse project by copying ProjectTemplate (if needed, see Creating a New Project from a Project Template for details). Name the new project Newton.
  2. Open the src folder of this project and then open (default package). As a starting point you should use ProgramWithIOAndStaticMethod.java. Rename it Newton1 and delete the other files from the project (if needed, see Creating a Program from a Skeleton (also Renaming a Java Program) for details).
  3. Edit Newton1.java to satisfy the problem requirements stated above, including updating comments appropriately. Estimating the square root should be done in a static method declared as follows:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    /**

    * 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) {

    ...

    }

  4. Copy Newton1.java to create Newton2.java. Change sqrt (including its Javadoc comments) so it also works when x = 0. Note: if your code from Newton1 appears 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.
  5. Copy Newton2.java to create Newton3.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 to sqrt.
  6. Copy Newton3.java to create Newton4.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 of x and interprets a negative value as an indication that it's time to quit.
  7. Select your Eclipse project Newton (not just some of the files, but the whole project), create a zip archive of it, and submit the zip archive to the Carmen dropbox for this project, as described in Submitting a Project.

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

More Books

Students also viewed these Databases questions

Question

U11 Informing Industry: Publicizing Contract Actions 317

Answered: 1 week ago