The value of ex can be computed as the power series

@MY_IMAGE_IN_STATEMENT@

where n! = 1 · 2 · 3 · … · n.

Write a program that computes ex using this formula. Of course, you can't compute an infinite sum. Just keep adding values until an individual summand (term) is less than a certain threshold. At each step, you need to compute the new term and add it to the total. Update these terms as follows:

term = term * x / n;

Follow the approach of the preceding two exercises, by implementing a class ExpApproximator. Its first guess should be 1.

Complete the following files:

ExpApproximator.java

/** This class calculates e^x as a power series. */ public class ExpApproximator { . . . /** Creates a ExpApproximator object to calculate e^x. @param x the input value @param eps the comparison threshold */ public ExpApproximator(double x, double eps) { . . . } /** Calculates the next approximation. @return the next approximation of e^x */ public double nextGuess() { . . . } /** Checks whether a more precise approximation is available @return true if there is a more precise approximation */ public boolean hasMoreGuesses() { . . . } }

ExpCalculator.java

Use the following file:

ExpApproximatorTester.java

public class ExpApproximatorTester
{
   public static void main(String[] args)
   {
      double a = 1;
      double epsilon = 0.25; 
      ExpApproximator approx = new ExpApproximator(a, epsilon);
      System.out.println(approx.nextGuess());
      System.out.println("Expected: 1");
      System.out.println(approx.nextGuess());
      System.out.println("Expected: 2");
      System.out.println(approx.nextGuess());
      System.out.println("Expected: 2.5");
      System.out.println(approx.hasMoreGuesses());
      System.out.println("Expected: true");
      approx.nextGuess();
      System.out.println(approx.hasMoreGuesses());
      System.out.println("Expected: false");
   }
}