Information Technology Reference
In-Depth Information
common to all subclasses, where the numerical integration rule is to be implemented
(in the subclasses):
class Integration
data:
prm
methods:
constructor(Integration_prm p)
prm=p
integrate()
Whenever appropriate, we insert the body of a function as indented statements under
the function heading.
The trapezoidal method is our first subclass candidate:
class Trapezoidal, subclass of Integration
data:
methods:
constructor(Integration_prm p)
Integration.constructor(p)
integrate()
h = (prm.b-prm.a)/(prm.npoints-1)
x=a
s=0
for i = 2,...,n-1
x=x+h
s=s+f(x)
s=s+0.5 * (f(prm.a) + f(prm.b))
return h * s
This class apparently has data on its own, but it inherits the prm object from its base
class Integration , and no additional data are needed. The constructor passes
the Integration prm input object onto the base class constructor, which stores the
parameter object as the data member prm .Inthe integrate method, the integration
parameters are accessed through the prm object. For example, prm.a means the a
data member of object prm . This is the same notation as used in common languages
supporting classes, such as C CC , Java, and Python.
6.5.4
A Class Hierarchy in Java
Readers who are familiar with the Java programming language will quite quickly
convert the previous pseudo code to Java. The following text is written for this group
of readers.
Thebaseclass Integration is realized as
class Integration {
public Integration_prm prm;
public Integration (Integration_prm p)
{prm=p;}
public double integrate ()
{
Search WWH ::




Custom Search