Information Technology Reference
In-Depth Information
System.out.println("integrate: not impl. in subclass");
return 0;
}
}
A specific integration method, such as the trapezoidal rule, is implemented in the
subclass Trapezoidal :
class Trapezoidal extends Integration {
public Trapezoidal (Integration_prm p)
{ super(p); }
public double integrate ()
{
double a = prm.a; double b = prm.b; int n = prm.n;
Func f = prm.f;
double h = (b-a)/((double)n);
double s = 0;
double x = a;
int i;
for (i = 1; i <= n-1; i++) {
x=x+h;
s=s+f.f(x);
}
s = 0.5 * (f.f(a) + f.f(b)) + s;
return h * s;
}
}
The Integration prm class holds a , b , n ,and f . In addition, the class provides
a function read for extracting information about a , b ,and n from command-line
arguments. A function write dumps the contents of a , b ,and n on the screen. Here
is the code:
class Integration_prm {
public double a, b;
public Func f;
public int n;
public String method;
public Integration_prm ()
{ * default values * /
a=0;b=1;n=10;f=newf2();
method = "Trapezoidal";
}
public void read (String argv[])
{
int i;
for (i = 0; i < argv.length; i=i+2) {
if (argv[i].compareTo("-a") == 0) {
a = Double.valueOf(argv[i+1]).doubleValue();
}
if (argv[i].compareTo("-b") == 0) {
b = Double.valueOf(argv[i+1]).doubleValue();
}
if (argv[i].compareTo("-n") == 0) {
n = Integer.valueOf(argv[i+1]).intValue();
}
if (argv[i].compareTo("-m") == 0) {
Search WWH ::




Custom Search