Information Technology Reference
In-Depth Information
method = argv[i+1];
}
}
}
public void write ()
{ System.out.println("a=" + a + " b="+b+"n="+n+
" method=" + method);
}
public Integration create ()
{
Integration i;
if (method.compareTo("Trapezoidal") == 0) {
i = new Trapezoidal(this);
}
// else if (method.compareTo("Simpson") == 0) and so on...
else {
i = new Trapezoidal(this);
}
return i;
}
}
The specification of functions to be integrated is exactly the same as in Sect.
6.3.4
;
that is,
f.x/
must be programmed as a subclass of
Func
, since Java does not allow
stand-alone functions.
The final piece of the Java code is the main program, here a part of a class called
Demo
for testing the implementation:
class Demo {
public static void main (String argv[])
{
Integration_prm p = new Integration_prm();
p.read(argv);
p.write();
f1 f = new f1();
Integration i = p.create();
double result=0; int j;
for (j = 1; j <= 10000; j++) {
result = i.integrate();
}
System.out.println(result);
}
}
Notice that the variable
i
is of type
Integration
and that
i.integrate
is
correctly interpreted as a call to the
integrate
method in the
Trapezoidal
class.
6.5.5
A Class Hierarchy in Python
Let us show how the
Integration
hierarchy can be implemented in the Python
programming language. We exemplify one subclass,
Trapezoidal
, and with this
information, the reader should be able to construct other subclasses (
Simpson
,for
instance). The text here assumes that you are familiar with several Python topics: