object-orientation in java for scientific programmers judith bishopnigel bishop university of...
TRANSCRIPT
![Page 1: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/1.jpg)
Object-orientation in Javafor scientific programmers
Judith Bishop Nigel Bishop
University of Pretoria University of South Africa
![Page 2: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/2.jpg)
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.
![Page 3: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/3.jpg)
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
![Page 4: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/4.jpg)
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
![Page 5: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/5.jpg)
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);
![Page 6: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/6.jpg)
Structure of a Pascal version
OUTPUTSINPUTS
solve (inputs,outputs, functions)
Program
functions
read inputssolve (inputs, outputs, functions)print outputs
Self-standing procedure
![Page 7: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/7.jpg)
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
![Page 8: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/8.jpg)
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
![Page 9: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/9.jpg)
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
![Page 10: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/10.jpg)
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
![Page 11: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/11.jpg)
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”);
![Page 12: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/12.jpg)
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
![Page 13: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/13.jpg)
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
![Page 14: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/14.jpg)
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
![Page 15: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/15.jpg)
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
}
}
![Page 16: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/16.jpg)
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
![Page 17: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/17.jpg)
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
![Page 18: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/18.jpg)
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
![Page 19: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/19.jpg)
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]);
}
}
![Page 20: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/20.jpg)
input section
output section
function
![Page 21: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/21.jpg)
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)
![Page 22: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/22.jpg)
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
![Page 23: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/23.jpg)
![Page 24: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/24.jpg)
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();
}
![Page 25: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/25.jpg)
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
![Page 26: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/26.jpg)
Java added value
Bulk input output use serialization
Network access use URLConnection and sockets
Parallel processing use threads
![Page 27: Object-orientation in Java for scientific programmers Judith BishopNigel Bishop University of PretoriaUniversity of South Africa](https://reader036.vdocuments.net/reader036/viewer/2022062500/5697c0021a28abf838cc31ae/html5/thumbnails/27.jpg)
More information
Website
www.cs.up.ac.za/javagently
Text Book
“Java Gently for Engineers and Scientists”, Addison Wesley, 2000