Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This exercise will use the Newton-Raphson algorithm to find an approximation to

ID: 3767042 • Letter: T

Question

This exercise will use the Newton-Raphson algorithm to find an approximation to the square root of a non-negative number, The algorithm computes successive “guesses” for the square root, each guess being calculated from the previous guess, This type of algortithm is usually referred to has a “successive substitution” algorithm. , a very important type of algorithm in computer science, science, engineering. Here is some pseudo-code for this algorithm: Given: a non-negative floating point number, n an initial guess x0, for the square root of n a floating point number, c, that determines when the most recently computer guess is good enough Process: Compute x1 = 0.5 * (x0 + n/x0) Loop while abs(x1 * x1 – n) > c Set x0 = x1 Compute x1 – 0.5 * (x0 + n/x0) End loop Return x1 Create an object-oriented implementation of this algorithm. Then test it by allowing the user to enter floating point numbers, and the constant for c. If the user enters a negative value, print an error message and continue. This is worth up to 50 extra credit points to your Programming Test points. It must be well documented and must work. This grade will also include points for your object-oriented design and the readability of your code.

How do I do this in Java?

Explanation / Answer

/***
* The java class SquareRoot that takes the double value
* in constructor of the SquareRoot class.
* Then the method getSquareRoot that returns the square
* root of a given double value.
* */
//SquareRoot.java
public class SquareRoot
{
   private double c; //initial value
   private double x1;//approximate vlaue
   private double EPSILON=0.0001; //accuracy vlaue

   //Constructor that takes double value, c as input argument
   //and sets c value
   public SquareRoot(double c)
   {
       //Set c vlaue
       this.c=c;
   }

   //Returns the square root of a value
   public double getSquareRoot()
   {
       //Initil guess of
       double x0=c;
              
       //Check the condition for newton rapsion for approximate vlaue
       //of epsilon
       while(Math.abs(x0-c/x0)>EPSILON)
       {
           //Calculate the average of c/x0 and x0
           x0=(c/x0+x0)/2.0;
           //set x0 to x1
           x1=x0;
       }
       //approximate value
       //return x1
       return x1;
   }
}


-----------------------------------------------------------------------------

/**
* The java program that prompts user to enter
* a double vlaue and then instantiates the object
* of SquareRoot. Then calls getSquareRoot value
*
* */
//NewtonRapson.java
import java.util.Scanner;
public class NewtonRapson
{
   public static void main(String[] args)
   {
       //Create a Scanner class
       Scanner scanner=new Scanner(System.in);
       System.out.println("Enter a double value to find square root :");
       //prompt user to enter a double value
       double value=scanner.nextDouble();
      
       //Create an instance of SquareRoot
       SquareRoot sqrt=new SquareRoot(value);
       //print the square root
       System.out.println("Square Root : "+sqrt.getSquareRoot());
      
   }
}


-----------------------------------------------------------------------------

Sample output:

Enter a double value to find square root :
2
Square Root : 1.4142156862745097

Enter a double value to find square root :
25
Square Root : 5.000023178253949

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote