|  | Here we demonstate the rectangular and trapezoidal approaches 
        to numerical integration. We also demonstrate more sophisticated object 
        oriented program design based on interface implementations. We first create two interfaces on which we will build our 
        function and integration method classes. 
        
           
            | Function.java |   
            | /** 
              An interface to provide a common method for obtaining * values of 1-D functions. //
 public interface Function
 {
 public 
              double valueAt 
              (double 
              x);
 }
 |   
            | Integrate.java |   
            | /** 
                An interface to provide a common method for executing * integration methods. 
                **/
 public interface Integrator
 {
 public 
                double 
                integrate (double 
                lo, double hi,
 int 
                n, Function 
                fg);
 }
 |    Then we create two Integrate 
        classes that implement the two types of integration mehtods: 
        
           
            | RectInteg.java |   
            | /** Executes 
                the rectangle slice method for * function integration. **/
 class RectInteg implements Integrator
 {
 public double integrate (double lo, double hi,
 int 
                n, Function f) {
 // Check data.
 if (hi <= lo || n <= 0 || f == null) return 
                0.0;
 double dx = (hi-lo)/n;
 double area = 0.0;
 
 double x_lo = lo;
 double x_hi = lo + dx;
 
 for (int i=0; i < n; i++) {
 double left_side = f.valueAt 
                (x_lo);
 area += left_side * dx;
 x_lo = lo + i * dx;
 }
 
 return area;
 } // integrate
 
 } // RectInteg
 |   
            | TrapezoidInteg.java |   
            | /** Executes 
                the trapezoid method for function integration. **/class TrapezoidInteg implements Integrator
 {
 /** Integrate the 1-D function between lo and hi with 
                n slices. */
 public double integrate (double lo, double hi,
 int 
                n, Function f) {
 // Check data.
 if (hi <= lo || n <= 0 || f == null) return 
                0.0;
 
 double dx =  (hi-lo)/n;
 double area = 0.0;
 
 double x_lo = lo;
 double x_hi = lo + dx;
 
 for (int i=0; i < n; i++) {
 double left_side = f.valueAt 
                (x_lo);
 double rite_side = f.valueAt 
                (x_hi);
 area += dx * (left_side + 
                rite_side) / 2.0;
 x_lo = lo + i*dx;
 x_hi = x_lo + dx;
 }
 
 return area;
 } // integrate
 
 } // TrapezoidInteg
 |    The following code shows an implementation of the Function 
        interface called PolyFunc. 
        The applet's init() 
        method creates an instance of the PolyFunc 
        class and passes it as an argument in the integrate methods of instances 
        of the two types of integration classes.  Integration is performed over the given span and with the 
        number of slices passed in the arguments. 
        
           
            | IntegrateApplet.java (Output goes to browser's Java 
              console.)
 |   
            | import java.applet.*;
 /** This class illustrates use of Rectangular and Trapezoidal
 * methods for integration.
 *
 *  Integrator.java - interface
 *  TravezoidInteg.java - implements Integrator
 *  RectInteg.java  - implements 
                Integrator
 *
 *  Function.java - interface
 *  PolyFunc.class - see below
 **/
 public class IntegrateApplet extends Applet
 {
 public void init () {
 
 PolyFunc pf = new PolyFunc ();
 
 RectInteg ri = new RectInteg ();
 double integ = ri.integrate (-2.0,2.0,100,pf);
 System.out.println ("Rectangle integ 
                = "+ integ);
 
 TrapezoidInteg ti = new TrapezoidInteg 
                ();
 integ = ti.integrate (-2.0,2.0,100,pf);
 System.out.println ("Trapezoid integ 
                = " + integ);
 
 } // init
 
 /** Paint message in Applet window. **/
 public void paint (java.awt.Graphics g) {
 g.drawString ("IntegrateApplet",20,20);
 }
 
 /** This program can also run as an application. No 
                graphics
 * needed, so it just runs the applet's 
                init () function.
 **/
 public static void main (String [] args) {
 IntegrateApplet obj = new IntegrateApplet 
                ();
 obj.init ();
 } // main
 
 } // IntegrateApplet
 /** Implement 
                a simple second order polynomial. **/class PolyFunc implements Function
 {
 public double valueAt (double x) {
 return  (4.0 - x*x);
 }
 
 } // Function
 |    The output shows: 
        
           
            | Rectangle integ 
              = 10.659263999999999 Trapezoid integ = 10.6656
 |    Exercise: 
        Compare these values to the calculated integral value. Try some other 
        parameters such as a bigger or lower number of slices and different integration 
        intervals.    Most recent update: Oct. 21, 2005 |  |