SHARE
TWEET

Roots

a guest Nov 19th, 2012 38 Never
  1. import java.lang.Math;
  2. public class Roots
  3. {
  4.     private double guess;
  5.     private double a;
  6.     private double n;
  7.     private double epsilon;
  8.    
  9.     public Roots(double a, double n, double epsilon)
  10.     {
  11.         this.a = a;
  12.         this.n = n;
  13.         this.epsilon = epsilon;
  14.         guess = 1;
  15.     }
  16.    
  17.     public boolean hasMoreGuesses()
  18.     {
  19.         if((Math.abs(Math.pow(guess, n) - a)) < epsilon)
  20.         return false;
  21.         else
  22.         return true;
  23.     }
  24.    
  25.     public double nextGuess()
  26.     {
  27.         guess = (guess + a/guess)/2.0;
  28.         return guess;
  29.     }
  30.    
  31.    
  32. }
RAW Paste Data
Top