Lect1 Java OOP-Review

34
Software Engineering Fall 2014 Lecture 1: Review Java and OOP

description

Lect1 Java OOP-Review

Transcript of Lect1 Java OOP-Review

Page 1: Lect1 Java OOP-Review

Software EngineeringFall 2014

Lecture 1: ReviewJava and OOP

Page 2: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 2

Review (1)

● An intermediate programming module● Programming:

● language: features & types● compiler & virtual machine

● Program:● verifiable● two levels: procedural, object oriented

Page 3: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 3

Review (2)

● Development techniques:● specification: design● implement: code● verify: reason about the correctness

Page 4: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 4

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

Page 5: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 5

Introduction to Java (1)

● Java programming environment● Basic program structure● Variable and types● Operations on primitive types● Object types:

● String● Enum● Array● Vector

Page 6: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 6

Introduction to Java (2)

● Text input/output● Flow of control● Programming concepts:

● decomposition● abstraction

Page 7: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 7

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:● eases problem solving● eases group work● eases program maintenance● eases understanding

Page 8: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 8

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

Page 9: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 9

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

Page 10: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 10

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

Page 11: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 11

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; }}

Page 12: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 12

Example (2)

● Yet another abstraction:

boolean found = a.isIn(e);

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

Page 13: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 13

Different types of abstraction

Which is simpler to program with

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

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

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

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

?What are the differences ?

Page 14: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 14

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

Page 15: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 15

Procedure

public static float sum(float[] a) { int n = 0; float s = 0; while (n < a.length) { s = s + a[n]; n++; } return s;}

Page 16: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 16

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

Page 17: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 17

accummulated effects on n,s

update vars

With mid-conditions

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;}

Page 18: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 18

(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 ... }

Page 19: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 19

...

/** * 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 ... }

Page 20: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 20

...

/** * 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

Page 21: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 21

Object oriented program

● Design● logical design specification● physical design specification

● Implement:● write code that implement the physical specification

Page 22: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 22

Design

FloatArray

- array: float[]

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

Page 23: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 23

General design concepts

attributes

operations

info. hiding

encapsulation

FloatArray

- array: float[]

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

Page 24: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 24

Design concepts (1)

FloatArray

- array: Float[]

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

[-1.0,0,1.0] [2.0,3.0] … FLOAT ARRAY

abstractconcept

class

formal type

object

Page 25: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 25

Design concepts (2)

FloatArray

- array: Float[]

type mutable optional length min max

Float[] T F

abstract properties(domain constraints)

Page 26: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 26

Other operations

FloatArray- array: float[]

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

default

validateimplementation

Page 27: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 27

Design concepts (3)

FloatArray

- array: float[] concrete type

representation

Page 28: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 28

Helper operations

FloatArray- array: float[]

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

read a number from

keyboard

Page 29: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 29

Implementation

● Non-verifiable code● Verifiable code

Page 30: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 30

Non verifiable code

review.nonverifiable.FloatArray

CODE

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)

Page 31: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 31

Design specification

FloatArraySpec

Page 32: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 32

Verifiable code

review.FloatArray

CODE

Page 33: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 33

Design issues

FloatArray- array: float[]

+ FloatArray(float[])+ ...+ getArray(): float[]

potentially exposing the rep

exposing the rep

Page 34: Lect1 Java OOP-Review

Duc L. M. PPL Review Fall 2014 34

Application

review.FloatArrayApp

CODE

Other design issues:● Object initialisation● Object reference and usage● Stack and heap,● ...