/*
 * Integ.java by Richard J. Davies
 * from `Introductory Java for Scientists and Engineers'
 * chapter: `Java for C Programmers'
 * section: `Similarities'
 *
 * This program integrates 1/(1+x) between x=1 and x=2 using the trapezium
 * rule. It is provided for comparision with Integ.c which is exactly the
 * same program written in the C language.
 */
public class Integ
{
  public static void main(String[] argv)
  {
    double total, x, y;

    total = 0;

    // We are using the trapezium rule, so each end
    // point has a weighting of one.

    total += 1 / (1 + 1.0);  // LH end, x = 1
    total += 1 / (1 + 2.0);  // RH end, x = 2

    // whilst each middle point has weight two
    
    for (int i=1; i<=99; i++)
    {
      x = 1 + i/100.0;
      y = 1 / (x + 1);

      total += 2 * y;
    }

    // Finally, we multiply by half the step size
	
    total *= 0.005;

    System.out.println(total);
  }
}

