/*
 * EComputation.java by Richard J. Davies
 * from `Introductory Java for Scientists and Engineers'
 * chapter: `Java for C Programmers'
 * section: `Example - Computation of E'
 *
 * This program computes e by summing its series expansion.
 * Note that we use a method call to initialize the constant E.
 */
public class EComputation
{
  // The number of terms to be added up.
  public static final int TERMS = 20;

  // The constant E to be computed
  public static final double E = computeE();


  // The method 'nextTerm' computes a term
  // in the expansion given the previous term
  // and the index of the term to be computed.
  public static double nextTerm(double prev,
                                int index)
  {
    return prev/index;
  }


  // The method 'computeE' sums the series.
  public static double computeE()
  {
    double res = 1;
    double term = 1;
    
    for (int i=1; i<TERMS; i++)
    {
      term = nextTerm(term, i);
      res += term;
    }

    return res;
  }


  // The main method just prints the constant.
  public static void main(String[] argv)
  {
    System.out.println(E);
  }
}

