In computer science, the "complicated" stuff that we do are based on need. (But then again, maybe there's some masochistic computer scientists out there :))
We have studied this in this webpage: click here
public class MathToolBox { public static double rectangleRule(double a, double b, int n) { double h; // Length of each subinterval double x_i; int i; double integral; /*  Sanity check: b > a  */ if ( b < a ) { System.out.println("Error: illegal interval "); return(0); // We need to return something.... } /*  The Rectangle Rule Algorithm  */ h = (ba)/n; integral = 0.0; x_i = a; // First subinterval point for ( i = 1; i <= n; i++ ) { integral = integral + MyFunction.f(x_i) * h; // ^^^^^^^^^^^^^^^^^^^^^ Area of rectangle x_i = x_i + h; } return ( integral ); } } 
If you need to integrate a new function, you must:

public class MathToolBox { public double f(double x) { return(x*x  3); } public double rectangleRule(double a, double b, int n) { double h; // Length of each subinterval double x_i; int i; double integral; /*  Sanity check: b > a  */ if ( b < a ) { System.out.println("Error: illegal interval "); return(0); // We need to return something.... } /*  The Rectangle Rule Algorithm  */ h = (ba)/n; integral = 0.0; x_i = a; // First subinterval point for ( i = 1; i <= n; i++ ) { integral = integral + f(x_i) * h; // ^^^^^^^^^^ Area of rectangle x_i = x_i + h; } return ( integral ); } } 
public class UseMathToolBox { public static void main(String[] args) { ... MathToolBox dummy = new MathToolBox(); // Make a MathToolBox object ans = dummy.rectangleRule(a, b, n); // Because we need // an instance variable // to invoke instance method System.out.println("ans = " + ans); } } 
public double f(double x) { return (x + Math.sin(x) ); } 
public class MyClass extends MathToolBox { public double f(double x) // override f() in MathToolBox { return (x + Math.sin(x) ); } } 
The following figure illustrates what is going on:
As you can see, the inherited method rectangleRule() will now invoke the new f()
As a result, the rectangleRule() in the sub class MyClass in compute the integral using the new function !!!
public class UseMathToolBox { public static void main(String[] args) { ... MyClass dummy = new MyClass(); // Make a MyClass object !!! ans = dummy.rectangleRule(a, b, n); // Because we need // an instance variable // to invoke instance method System.out.println("ans = " + ans); } } 
Well, using the first approach, it is NOT possible to make the method rectangleRule() integrate 2 different methods...
That is because you need to:
You cannot do it in the same program
(And because it is not possible, I cannot demostrate it :))
Suppose we want to compute the integrals for these 2 functions:
public double f(double x) { return (x + Math.sin(x) ); } 
and:
public double f(double x) { return (x*x + Math.cos(x) ); } 
public class MyClass1 extends MathToolBox { public double f(double x) // override f() in MathToolBox { return (x + Math.sin(x) ); } } 
And:
public class MyClass2 extends MathToolBox { public double f(double x) // override f() in MathToolBox { return (x*x + Math.cos(x) ); } } 
public class UseMathToolBox { public static void main(String[] args) { ... MyClass1 dummy1 = new MyClass1(); // Make a MyClass1 object !!! MyClass2 dummy2 = new MyClass2(); // Make a MyClass2 object !!! ans1 = dummy1.rectangleRule(a1, b1, n1); // Integrates function in MyClass1 ans2 = dummy2.rectangleRule(a2, b2, n2); // Integrates function in MyClass2 ... } } 