// This appears in Core Web Programming from
// Prentice Hall Publishers, and may be freely used
// or adapted. 1997 Marty Hall, hall@apl.jhu.edu.

class Sin implements Evaluatable {
  public double evaluate(double val) {
    return(Math.sin(val));
  }
}

class Cos implements Evaluatable {
  public double evaluate(double val) {
    return(Math.cos(val));
  }
}

class Square implements Evaluatable {
  public double evaluate(double val) {
    return(val * val);
  }
}

public class IntegralTest {
  public static void main(String[] args) {
    for(int steps=10; steps<=10000; steps*=10) {
      System.out.println
	("Estimated with " + steps + " steps:" +
	 "\n  Integral from 0 to pi of sin(x)=" +
	 Integral.integrate(0.0, Math.PI, steps,
			    new Sin()) +
	 "\n  Integral from pi/2 to pi of cos(x)=" +
	 Integral.integrate(Math.PI/2.0, Math.PI, steps,
			    new Cos()) +
	 "\n  Integral from 0 to 5 of x^2=" +
	 Integral.integrate(0.0, 5.0, steps,
			    new Square()));
    }
    System.out.println
      ("`Correct' answer using Math library:" +
       "\n  Integral from 0 to pi of sin(x)=" +
       (-Math.cos(Math.PI) - -Math.cos(0.0)) +
       "\n  Integral from pi/2 to pi of cos(x)=" +
       (Math.sin(Math.PI) - Math.sin(Math.PI/2.0)) +
       "\n  Integral from 0 to 5 of x^2=" +
       (Math.pow(5.0, 3.0) / 3.0));
  }
}

