title: Beginner's Java Code Help: Newton Raphson Square Root Loop
body: <p>In my Java class for college we are learning about the Looping control structure, and we got an assignment to code a little program that I am guessing is supposed to give the square root of a number and keep on taking the square root until the difference or accuracy is met. Here are the instructions: </p>
  
  <p>"Write a class called NewtonRaphson that can be used to find an approximate solution of sqrt(a) using Newton's method, for any positive real number. </p>
  
  <p>Note: sqrt(a) can be expressed in functional notation as follows: f(x) = x2 – a, 
  From which f ' (x) = 2 * x, </p>
  
  <p>Print the iteration sequence and the approximation for each iteration. (That is, in a tabular form). </p>
  
  <p>Write a driver class called TestNewton. Use the following data to test the class NewtonRaphson. </p>
  
  <p>•   The initial guess is 5.0 
  •   In this exercise, the process terminates when the difference between two consecutive approximations is less than 0.00005" </p>
  
  <p>I have my code linked at the bottom here, the main class and the test class, but I am not getting the looping result I am just getting the same square root of 5 when I type in 5 after running the program. Can someone please tell me where I messed up on? </p>
  
  <p>Thanks, I am really new to coding, and this took forever to make and I had to ask for some friends help. </p>
  
  <p>Main Class: <a href="http://pastebin.com/eiUJFJjQ" rel="nofollow">http://pastebin.com/eiUJFJjQ</a> 
  Test Class: <a href="http://pastebin.com/sJ4dB5uZ" rel="nofollow">http://pastebin.com/sJ4dB5uZ</a></p>
  
  <p>Or if you prefer the code here it is:</p>
  
  <pre><code>import java.text.NumberFormat; 
  import java.text.DecimalFormat; 
  
  public class NewtonRaphson { 
  
  static final double DIFFERENCE = 0.00005; 
  double n; 
  double x; 
  double derivative; 
  double function; 
  double xold; 
  double xnew; 
  int i; 
  
  public NewtonRaphson(double n2, int x2) 
  
  { 
      n=n2; 
      x=x2; 
      function = Math.pow(n, 2)-x; 
      derivative = 2*n; 
      xnew=n-function/derivative; 
      xold=0; 
  } 
  
  boolean positive() 
  
  { 
      return (n &gt;= 0); 
  } 
  
  public double findXNew(double xold2) 
  
  { 
      function = Math.pow(xold2, 2)-x; 
      derivative = 2*xold2; 
      return xold2-function/derivative; 
  } 
  
  public void findSqRtA() 
  
  { 
  
      i=0; 
      while(Math.abs(xnew-xold)&gt; DIFFERENCE)
  
  { 
  
      xold=xnew; 
      xnew=findXNew(xold); 
      i++; 
      System.out.println(this); 
  
  } 
  
      System.out.println("\nIteration completed, difference is less than 0.00005"); 
  
  } 
  
  public String toString() 
  
  { 
      NumberFormat nf = NumberFormat.getInstance(); DecimalFormat df = (DecimalFormat)nf; 
      df.applyPattern("0.00000000"); 
  
      return "The approximate value of the square root of "+x+" is " + xnew + "\n" + 
  
      "The number of iterations is " + i; 
  } 
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>import java.io.Console; 
  import java.util.Scanner; 
  
  public class TestNewton { 
  
  public static void main(String[] args) { 
  
  Scanner reader = new Scanner(System.in); 
  System.out.println("Enter a number you would like to find the square root of"); 
  
  int a = reader.nextInt(); 
  
  NewtonRaphson nr = new NewtonRaphson(5.0, a); 
  
  nr.findSqRtA(); 
  
  } 
  } 
  </code></pre>
  
  <p>My output is this, but I want it to take the square root after each iteration's result.</p>
  
  <pre><code>Enter a number you would like to find the square root of
  5
  The approximate value of the square root of 5.0 is 2.3333333333333335
  The number of iterations is 1
  The approximate value of the square root of 5.0 is 2.238095238095238
  The number of iterations is 2
  The approximate value of the square root of 5.0 is 2.2360688956433634
  The number of iterations is 3
  The approximate value of the square root of 5.0 is 2.236067977499978
  The number of iterations is 4
  
  Iteration completed, difference is less than 0.00005
  </code></pre>