Professional Documents
Culture Documents
Fall 2014
Lecture 1: Review
Java and OOP
Review (1)
Programming:
Program:
verifiable
Duc L. M.
Review (2)
Development techniques:
specification: design
implement: code
Duc L. M.
Topics at a glance
1) Introduction to Java
2) Overview of programming languages
Operational principles of compiler & VM
3) Verifiable program: design & implement
4) Principles of reasoning about programs
Reasoning about programs
5) Introduction to object oriented program
Class: design & implement
Class design issues
6) Basic ADTs
Duc L. M.
Object types:
Duc L. M.
String
Enum
Array
Vector
PPL Review Fall 2014
Text input/output
Flow of control
Programming concepts:
Duc L. M.
decomposition
abstraction
Decomposition
Duc L. M.
eases understanding
PPL Review Fall 2014
Decomposition criteria
Each subproblem:
Duc L. M.
Abstraction
Duc L. M.
Programming language
abstractions
Duc L. M.
10
Example (1)
Duc L. M.
11
Example (2)
Duc L. M.
12
?
13
Programming language
A compromise between:
PL implementer
Programmer:
conceptual clarity
PL implementer:
implementation efficiency
Duc L. M.
14
Procedure
public static float sum(float[] a) {
int n = 0;
float s = 0;
while (n < a.length) {
s = s + a[n];
n++;
}
return s;
}
Duc L. M.
15
Verifiable procedure
/**
* determine the sum of array of real
* numbers
* @requires a is not null
* @effects return the sum of the array
* elements, i.e. result =
*
a[0]+...+a[a.length-1]
*/
public static float sum(float[] a) {
//... code omitted ...
}
Duc L. M.
16
With mid-conditions
accummulated
effects on n,s
17
Duc L. M.
18
...
/**
* determine the sum of array of real
*
numbers
* @requires a is not null
* @effects return the sum of the array
* elements, i.e. result =
*
a[0]+...+a[a.length-1]
*/
private static float sum(float[] a) {
//... code omitted ...
}
Duc L. M.
19
...
/**
* obtain an array of real numbers from a
*
pre-defined input source
* @requires n > 0
* @effects return an array of n real
*
numbers or null if no real numbers
*
were found
*/
private static float[] getNumbers(int n) {
20
Design
Implement:
Duc L. M.
21
Design
FloatArray
- array: float[]
+ FloatArray(float[])
+ sum(): float
+ getNumbers(int)
Duc L. M.
22
FloatArray
- array: float[]
attributes
+ FloatArray(float[])
+ sum(): float
+ getNumbers(int)
operations
encapsulation
Duc L. M.
23
[-1.0,0,1.0]
[2.0,3.0]
FLOAT
ARRAY
object
class
FloatArray
- array: Float[]
formal type
+ FloatArray(float[])
+ sum(): float
+ getNumbers(int)
Duc L. M.
24
type
Float[]
max
abstract properties
(domain constraints)
Duc L. M.
25
Other operations
FloatArray
default
- array: float[]
+
+
+
+
+
FloatArray(int[])
sum(): float
getNumbers(int)
toString(): String
repOK(): boolean
validate
implementation
Duc L. M.
26
concrete
type
representation
Duc L. M.
27
Helper operations
FloatArray
- array: float[]
+
+
+
+
+
Duc L. M.
FloatArray(float[])
sum(): float
getNumbers(int)
read(): float
toString(): String
repOK(): boolean
read a
number from
keyboard
28
Implementation
Non-verifiable code
Verifiable code
Duc L. M.
29
CODE
review.nonverifiable.FloatArray
Problem:
Usable but non-provable
is it really FLOAT ARRAY?
Why? lost all the design information
Solution:
Use design specification to capture
design information
(Add mid-conditions where appropriate)
Duc L. M.
30
Design specification
FloatArraySpec
Duc L. M.
31
CODE
Verifiable code
review.FloatArray
Duc L. M.
32
Design issues
FloatArray
- array: float[]
+ FloatArray(float[])
+ ...
+ getArray(): float[]
Duc L. M.
potentially exposing
the rep
exposing the rep
33
CODE
Application
review.FloatArrayApp
Duc L. M.
34