object-orientation in java for scientific programmers judith bishopnigel bishop university of...
TRANSCRIPT
Object-orientation in Javafor scientific programmers
Judith Bishop Nigel Bishop
University of Pretoria University of South Africa
Requirements of scientific programs
Simple access to a library of numerical methods Routines which can return multiple values and
take functions as parameters Simple re-running of a program with changed
input parameters. Bulk input from and output to files. Simple drawing and labelling of graphs on axes.
Breaking the mould
A set of guidelines for class design to allow for returning values function parameters no input-output in library routines
A specially designed GUI interface class
A specially designed graph drawing class
Example - the Newton Raphson solver
Required: a facility which
can find the root of any function given f and f’ can also return how many iterations were taken can arrange for printing interim results
The classic procedural approach
PROCEDURE GeneralNewton ( imax : integer; tolerance : real; var estimate : real;
var iteration : integer;function f (x : real) : real;
function fdiff (x : real): real);
Two input parameters to start the process going Two output parameters to record the results Two function parameters on which to work called by:
GeneralNewton (10, 1Oe-6, x, i, sin, cos);
Structure of a Pascal version
OUTPUTSINPUTS
solve (inputs,outputs, functions)
Program
functions
read inputssolve (inputs, outputs, functions)print outputs
Self-standing procedure
OUTPUTS
solve(inputs)
solve(inputs)...println(outputs)
Structure of Java version 0
functions
INPUTS
• static method accessing outputs as static variables• only works for one function
Steps to achieving the object model
Version 1 Solves only a single given function No interim results, or print them in solver
Version 2 Generalise the function using abstract methods Put the stand-alone, generalised, public class in a library
Version 3 Declare input data values in a data handler Generalise i-o for the solver in question
outputs
solve(inputs)
...println(outputs)
Inner class
worker.solve(inputs)
Structure of Java version 1a
functions
inputs
workerinstantiates
• Printing final outputs in the inner class
outputs
solve(inputs)
Inner class
worker.solve(inputs)...println(worker.outputs)
Structure of Java version 1b
functions
inputs
worker
instantiates
• Move outputs into a class with the solve routine• Access outputs as member variables for printing
Version 1 - A class for one function
public class NewtonForSine { public int iteration; public double estimate; public void solve (double tolerance,
int imax) { ….. }}
Output values are accessible (could be wrapped) Input values are parameters
NewtonForSine worker = new NewtonForSine();
// in a loop
worker.solve(1E-6, 10);
System.out.println(“Root “ + worker.estimate +
“ found in “ + worker.iteration + “ iterations”);
OUTPUTS
solve(inputs)
Abstract library class
workerA = new WorkerAClass ();workerA.solve(inputs)...println(workerA.outputs)
Structure of Java version 2
functions A
INPUTS
WorkerAClass inherits
• Worker is inherited and instantiated for each function
OUTPUTS
solve(inputs)
Abstract library class
workerA = new WorkerAClass ();workerA.solve(inputs)...println(workerA.outputs)
Java version 2 with two functions
functions A
INPUTS
WorkerAClass
workerB = new WorkerBClass ();workerB.solve(inputs)...println(workerB.outputs)
functions B
WorkerBClass
Printing of interim results
The library routine stores the result of each iteration in an array called estimate
estimate[iteration] is the final root the estimate array can then be printed by the
caller
Putting it in a Package
package scieng;
public abstract class NewtonRaphson {
public abstract double f (double x);
public abstract double df (double x);
public int iteration;
public double estimate [];
public void solve (double xnew,
double tolerance, int imax) {
… the loop
}
}
Input output in general
If solve is called for several sets of data or there are several functions to be solved then, there will be a lot of input-output.
Can the input-output be generalised?Yes.
We define a DataHandler class with get and put methods
A DataHandler class
static class DataHandler {
double x, tolerance;
int imax;
void getData () {
…. read in x, tolerance and imax
}
void showResults (int i, double x) {
… print i and x
}
}
This data handler is specific for Newton Raphson But it is general over all functions The i-o statements are only written once
INPUTS
getData( )
showResults (outputs)
Data Handler
OUTPUTS
solve(inputs)
Library class
worker
data
data.getData( )worker.solve(data.inputs)data.showResults(worker.outputs)
Structure of the Java version 3
functions
The main program
class MyNewtonRaphson extends NewtonRaphson {
… define functions here
}
public static void main (String [] args) {
NewtonRaphson worker = new MyNewtonRaphson();
DataHandler data = new DataHandler ( );
while(true) {
data.getData();
worker.solve(data.x, data.tolerance, data.imax);
data.showResults(worker.iteration,
worker.estimate[worker.iteration]);
}
}
input section
output section
function
The Display class
new Display (String)
void println (String)
void prompt (String, value)
void ready (String)
double getDouble (String)
int getInt (String)
String getString (String)
void reposition (Graph)
The Graph class
Graph ()
Graph (String, String, String)
void add(double, double)
void showGraph()
void nextGraph()
void setColor(int)
void setSymbol(boolean)
void setSymbol(int)
void setLine(boolean)
void setTitle(String)
int black, magenta, blue, red
Using Graph
public static void main ( String args []) {
Graph g = new Graph("Sine and Cosine","x","y");
double x;
// The first graph - y=sin(x)
g.setSymbol(true);
g.setColor(g.blue);
g.setTitle("Sine");
for (int i = 0; i <= 100; i++) {
x = i / 10.0;
g.add(x, Math.sin(x));
}
// Same for second graph, then
g.showGraph();
}
Library routines written
Abstract classes with one solve methodNewtonRaphson, PredictorCorrector, Secant
Abstract classes with more than one solve methodIntegrate (has simpson, midpoint etc)
Classes with one solve methodSORSolver, LeastSquares
Classes with one or more static methodsSort, Stats
Class defining an adtComplex
Java added value
Bulk input output use serialization
Network access use URLConnection and sockets
Parallel processing use threads
More information
Website
www.cs.up.ac.za/javagently
Text Book
“Java Gently for Engineers and Scientists”, Addison Wesley, 2000