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.

Respuesta :

toxci

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 updatingr 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.

Open the src folder of this project and then open (default package). As a starting point you should useProgramWithIOAndStaticMethod.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).

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

   ...

}

Copy Newton1.java to create Newton2.java. Change sqrt (including its Javadoc comments) so it also works when x = 0.

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.

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.

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.

Following are the program to calculate the square root:

Program Explanation:

  • Import package.
  • Defining a class "SqaureRoot", inside the class, a method "sqrt" is declared that takes double "x" variable in the parameter.
  • Inside the method, a conditional statement has been used that checks the input value calculates the square root value, and return its value.
  • In the next step, the main method is declared that creates scanner class object to the input value.
  • In this method two-variable "g,l" is declared, that input the value and use a conditional statement to check the input value and call the method and print its return value.

Program:

import java.util.*;//import package

public class SqaureRoot //defining a class SqaureRoot

{

  public static double sqrt(double x)//defining a method sqrt that takes double variable in the parameter

  {

      double g = x / 2;//holding value in g

      if (x<0)//defining if block that check x value lessthan 0

      {

          System.out.println("Can't find square root of a negative number");//print message

          g = -1;//holding value in g variable

      }

      else//else block

      {

          double p = Double.MAX_VALUE;//defining double variable p that hold value in variable

          double l= g;//defining double variable l that hold g value

          while (Math.abs(p) >= 0.01)//defining while loop that holds abs method and check its value greater than equal to 0.01

          {

              //calculating the sqrtroot value

              double r = n / g;//defining double variable r that holds value in it

              g = (g + r) / 2;//calculating value and holding value in g

              p= ((g - l) / l);//calculating value and holding value in p

              l = g;//holding g value in l variable

          }

      }

      return g;//return g value

  }

  public static void main(String args[]) //defining main method

  {

      Scanner input = new Scanner(System.in);//creating Scanner class object that inputs value

      String l = new String();//creating String class object

      while(true)//defining loop that calculates value

      {

          System.out.print("do you wish to calculate a square root enter  y/n: ");//print message

          l = input.nextLine();//input string vlue

          if(l.equalsIgnoreCase("y"))//defining if block that checks string value

          {

              System.out.print("Enter number to compute the square root of: ");//print message

              double n = input.nextDouble();//input n value

              double g = sqrt(n);//defining double variable g that calls sqrt method value

              System.out.println("The estimate of the square root of " + n + " is " + guess);//print value with message

              input.nextLine();//input value

          }

          else//defining else block

          {

              break;//using break keyword

          }

      }

      System.out.println("End of Program");//print message

  }

}

Output:

Please find the attached file.

Learn more:

brainly.com/question/15460565