Powerpoint slides - The Java Gently Project

Download Report

Transcript Powerpoint slides - The Java Gently Project

Object-orientation in Java
for scientific programmers
Judith Bishop
University of Pretoria
Nigel Bishop
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
:
tolerance
:
var estimate
:
var iteration
:
function f (x : real)
:
function fdiff (x : real):
integer;
real;
real;
integer;
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
Program
INPUTS
OUTPUTS
functions
read inputs
solve (inputs, outputs, functions)
print outputs
Self-standing procedure
solve (inputs,outputs,
functions)
Structure of Java version 0
INPUTS
OUTPUTS
functions
solve(inputs)
solve(inputs)
...println(outputs)
• 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
Structure of Java version 1a
inputs
Inner class
functions
outputs
worker
solve(inputs)
instantiates
...println(outputs)
worker.solve(inputs)
• Printing final outputs in the inner class
Structure of Java version 1b
inputs
Inner class
functions
outputs
worker
solve(inputs)
instantiates
worker.solve(inputs)
...println(worker.outputs)
• 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”);
Structure of Java version 2
Abstract library class
INPUTS
WorkerAClass
inherits
functions A
OUTPUTS
solve(inputs)
workerA = new WorkerAClass ();
workerA.solve(inputs)
...println(workerA.outputs)
• Worker is inherited and instantiated for each function
Java version 2 with two functions
INPUTS
WorkerAClass WorkerBClass
functions A
Abstract library class
OUTPUTS
functions B
solve(inputs)
workerA = new WorkerAClass ();
workerA.solve(inputs)
...println(workerA.outputs)
workerB = new WorkerBClass ();
workerB.solve(inputs)
...println(workerB.outputs)
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
Structure of the Java version 3
Data Handler
INPUTS
Library class
functions
OUTPUTS
data
solve(inputs)
getData( )
worker
showResults
(outputs)
data.getData( )
worker.solve(data.inputs)
data.showResults(worker.outputs)
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]);
}
}
function
input section
output section
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 method
NewtonRaphson, PredictorCorrector, Secant
 Abstract classes with more than one solve method
Integrate (has simpson, midpoint etc)
 Classes with one solve method
SORSolver, LeastSquares
 Classes with one or more static methods
Sort, Stats
 Class defining an adt
Complex
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