lect1 java oop-review

Post on 18-Jul-2016

33 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

Lect1 Java OOP-Review

TRANSCRIPT

Software EngineeringFall 2014

Lecture 1: ReviewJava and OOP

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

Duc L. M. PPL Review Fall 2014 3

Review (2)

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

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

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

Duc L. M. PPL Review Fall 2014 6

Introduction to Java (2)

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

● decomposition● abstraction

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

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

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

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

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

Duc L. M. PPL Review Fall 2014 12

Example (2)

● Yet another abstraction:

boolean found = a.isIn(e);

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

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 ?

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

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

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

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

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

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

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

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

Duc L. M. PPL Review Fall 2014 22

Design

FloatArray

- array: float[]

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

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)

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

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)

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

Duc L. M. PPL Review Fall 2014 27

Design concepts (3)

FloatArray

- array: float[] concrete type

representation

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

Duc L. M. PPL Review Fall 2014 29

Implementation

● Non-verifiable code● Verifiable code

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)

Duc L. M. PPL Review Fall 2014 31

Design specification

FloatArraySpec

Duc L. M. PPL Review Fall 2014 32

Verifiable code

review.FloatArray

CODE

Duc L. M. PPL Review Fall 2014 33

Design issues

FloatArray- array: float[]

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

potentially exposing the rep

exposing the rep

Duc L. M. PPL Review Fall 2014 34

Application

review.FloatArrayApp

CODE

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

top related