You are on page 1of 34

Software Engineering

Fall 2014

Lecture 1: Review
Java and OOP

Review (1)

An intermediate programming module

Programming:

language: features & types

compiler & virtual machine

Program:

verifiable

two levels: procedural, object oriented

Duc L. M.

PPL Review Fall 2014

Review (2)

Development techniques:

specification: design

implement: code

verify: reason about the correctness

Duc L. M.

PPL Review Fall 2014

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.

PPL Review Fall 2014

Introduction to Java (1)

Java programming environment

Basic program structure

Variable and types

Operations on primitive types

Object types:

Duc L. M.

String
Enum
Array
Vector
PPL Review Fall 2014

Introduction to Java (2)

Text input/output

Flow of control

Programming concepts:

Duc L. M.

decomposition
abstraction

PPL Review Fall 2014

Decomposition

The process of dividing a large problem into


smaller ones

the divide and rule principle

Goal: decompose a program into smaller ones


that interact in simple and well-defined ways
Benefits:

Duc L. M.

eases problem solving

eases group work

eases program maintenance

eases understanding
PPL Review Fall 2014

Decomposition criteria

Each subproblem:

is at the same level of detail

can be solved independently

The subproblems' solutions can be combined


to solve the original problem

Duc L. M.

PPL Review Fall 2014

Abstraction

To look at a problem at a different level of detail


Helpful for simplifying a problem in attempt to
solve it
Performed in tandem with decomposition at
increasing levels of detail

Duc L. M.

PPL Review Fall 2014

Programming language
abstractions

Programming constructs that map more closely


to how humans solve problems than machines

Help simplify program development

Example: a search problem


determines if an element e is in an array a; if so
returns true and an arbitrary index of e in a,
otherwise returns false

Duc L. M.

PPL Review Fall 2014

10

Example (1)

One abstraction we know:

boolean found = false;


for (int i = 0; i < a.length; i++) {
if (a[i] == e) {
z = i;
found = true;
}
}

Duc L. M.

PPL Review Fall 2014

11

Example (2)

Yet another abstraction:


boolean found = a.isIn(e);
if (found) {
z = a.indexOf(e);
}

Duc L. M.

PPL Review Fall 2014

12

Different types of abstraction


for
for (int
(int ii == 0;
0; ii << a.length;
a.length; i++)
i++) {{
if
if (a[i]
(a[i] ==
== e)
e) {{
zz == i;
i;
found
found == true;
true;
}}
}}
boolean
boolean found
found == a.isIn(e);
a.isIn(e);
if
if (found)
(found) {{
zz == a.
a.indexOf(e);
indexOf(e);
}}

What are the differences

Which is simpler to program with


Duc L. M.

PPL Review Fall 2014

?
13

Programming language

A compromise between:

programmer (PL user) and

PL implementer

Programmer:

language = means of expressing algorithms

conceptual clarity

PL implementer:

language = means of instructing computers

implementation efficiency

Duc L. M.

PPL Review Fall 2014

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.

PPL Review Fall 2014

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.

PPL Review Fall 2014

16

With mid-conditions

accummulated
effects on n,s

public static float sum(float[] a) {


int n = 0; float s = 0;
for (n = 0; n < a.length; n++) {
// 0 <= n < a.length /\ (n >=1
//
(n=n+1 /\ s = a[0] + ... + a[n-1]))
s = s + a[n];
// s = a[0] + ... + a[n]
}
// n=a.length /\ s = a[0] + ... + a[n-1]
return s;
update vars
}
Duc L. M.

PPL Review Fall 2014

17

(Verifiable) procedural program


/**
* @overview a program that computes
*
the sum of real numbers
*
public class Sum {
/**
* the main procedure
*
* @effects prints the sum of an array of
*
real numbers obtained from the input
*
or 0 if no numbers were given
*/
public static void main(String[] args) {

//... code omitted ...


}

Duc L. M.

PPL Review Fall 2014

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.

PPL Review Fall 2014

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) {

//... code omitted ...


}
} // end Sum
Duc L. M.

PPL Review Fall 2014

20

Object oriented program

Design

logical design specification

physical design specification

Implement:

Duc L. M.

write code that implement the physical specification

PPL Review Fall 2014

21

Design
FloatArray
- array: float[]
+ FloatArray(float[])
+ sum(): float
+ getNumbers(int)

Duc L. M.

PPL Review Fall 2014

22

General design concepts


info. hiding

FloatArray
- array: float[]

attributes

+ FloatArray(float[])
+ sum(): float
+ getNumbers(int)

operations

encapsulation
Duc L. M.

PPL Review Fall 2014

23

Design concepts (1)


abstract
concept

[-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.

PPL Review Fall 2014

24

Design concepts (2)


FloatArray
- array: Float[]

type
Float[]

mutable optional length min


T

max

abstract properties
(domain constraints)
Duc L. M.

PPL Review Fall 2014

25

Other operations
FloatArray
default

- array: float[]
+
+
+
+
+

FloatArray(int[])
sum(): float
getNumbers(int)
toString(): String
repOK(): boolean

validate
implementation

Duc L. M.

PPL Review Fall 2014

26

Design concepts (3)


FloatArray
- array: float[]

concrete
type

representation
Duc L. M.

PPL Review Fall 2014

27

Helper operations
FloatArray
- array: float[]
+
+
+
+
+

Duc L. M.

FloatArray(float[])
sum(): float
getNumbers(int)
read(): float
toString(): String
repOK(): boolean

PPL Review Fall 2014

read a
number from
keyboard

28

Implementation

Non-verifiable code

Verifiable code

Duc L. M.

PPL Review Fall 2014

29

Non verifiable code

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.

PPL Review Fall 2014

30

Design specification
FloatArraySpec

Duc L. M.

PPL Review Fall 2014

31

CODE

Verifiable code
review.FloatArray

Duc L. M.

PPL Review Fall 2014

32

Design issues
FloatArray
- array: float[]
+ FloatArray(float[])
+ ...
+ getArray(): float[]

Duc L. M.

potentially exposing
the rep
exposing the rep

PPL Review Fall 2014

33

CODE

Application
review.FloatArrayApp

Other design issues:


Object initialisation
Object reference and usage
Stack and heap,
...

Duc L. M.

PPL Review Fall 2014

34

You might also like