course plan teachers · twopictures mypics; pictureframe mybabypicture, mygradpicture;...

11
Föreläsning 1 - Håkan Jonsson 1 Course info Object-Oriented Design Object-Oriented Programming Software Specification SMD167 Object- Oriented Design Lecture 1 Reily O1 & O2 Efficiency Correctness Föreläsning 1 - Håkan Jonsson 2 Course plan Föreläsning 1 - Håkan Jonsson 3 Teachers Tomas Johansson Lab assistent Håkan Jonsson Lectures Examiner Föreläsning 1 - Håkan Jonsson 4 Course webpage www.sm.luth.se/csee/courses/smd/167

Upload: others

Post on 27-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Course plan Teachers · TwoPictures myPics; PictureFrame myBabyPicture, myGradPicture; myBabyPicture = new PictureFrame(4, 5, "plastic"); myGradPicture = new PictureFrame(10, 14,

Föreläsning 1 - Håkan Jonsson 1

Course infoObject-Oriented

Design

Object-Oriented

Programming

Software

Specification

SMD167 Object-

Oriented Design

Lecture 1

Reily O1 & O2

EfficiencyCorrectness

Föreläsning 1 - Håkan Jonsson 2

Course plan

Föreläsning 1 - Håkan Jonsson 3

Teachers

Tomas Johansson

Lab assistent

Håkan Jonsson

Lectures

Examiner

Föreläsning 1 - Håkan Jonsson 4

Course webpage

www.sm.luth.se/csee/courses/smd/167

Page 2: Course plan Teachers · TwoPictures myPics; PictureFrame myBabyPicture, myGradPicture; myBabyPicture = new PictureFrame(4, 5, "plastic"); myGradPicture = new PictureFrame(10, 14,

Föreläsning 1 - Håkan Jonsson 5

Course requirements

• A written exam at the end of LP3– Exact date and time has not been set yet.

– To help you learn there will be a small set ofhomeworks with typical exam problems for theambitious ones:

• Correct answers gives bonus points on the written exam.

• Not mandatory.

• Lab assignments– 5 labs with strict dead-lines.

• Design and implement the design in the form of a javaprogram.

– Instructions and dead-lines will be announced onthe homepage.

Föreläsning 1 - Håkan Jonsson 6

Grading procedure• 1st lab is individual; the last 4 labs in groups of 3.

– Groups will be formed by us when the list ofparticipants has reached a fairly stable form.

• Grading takes place in the lab, at a computer,together with me or Tomas, and includes amongother things:– A demonstration (a test-run)

• The group demonstrates correct functionality

– An inspection of the code (bring a print-out + frontpage)

• Should be clear and ”well-written”

• Follow ”Java code conventions”

– Some questions to all the team members about theprogram. Typical questions:

• What is this? Why did you do it like this? How does this work?

Föreläsning 1 - Håkan Jonsson 7

(Grading procedure)• 1st lab is individual; the last 4 labs in groups of 3.

– Groups will be formed by us when the list of participants has reacheda fairly stable form.

• Grading takes place in the lab, at a computer, together withme or Tomas, and includes among other things:– A demonstration (a test-run)

• The group demonstrates correct functionality

– An inspection of the code (bring a print-out + front page)

• Should be clear and ”well-written”, follow ”Java code conventions”.

– Some questions to all the team members about the program like:

• What is this? Why did you do it like this? How does this work?

• The outcome of a grading session:– Very often a “retur”, meaning that you will have to change/correct a

number of things and go through a new grading session to pass.

– “Godkänd”, meaning that you pass! This is what we all long for!

– (“Underkänd”, meaning that you fail and have to wait one (1) yearuntil you get another chance to pass. This happens very seldom.)

Föreläsning 1 - Håkan Jonsson 8

Cheating (Fusk)• De!nition: You cheat if you present someone else"s

work as your own.• Everything you/you group write and hand in #no matter

if it is a report, a solution, a computer program, etc$ inthis course must be your own work and be written byyourself/the group you belong to.

• Having said this I would like to urge you to collaboratewith and help #get help from$ fellow students as long asyou acknowledge this properly; write it in your reports,in your solutions, in comments in your computerprograms, etc.

• You are allowed to discuss problems and solutions ingeneral terms but not in very speci!c detail. Under nocircumstances are you allowed to copy verbatim.

• Those who cheats will be reported to thedisciplinn!mnde" and could be thrown out of LTU.

Page 3: Course plan Teachers · TwoPictures myPics; PictureFrame myBabyPicture, myGradPicture; myBabyPicture = new PictureFrame(4, 5, "plastic"); myGradPicture = new PictureFrame(10, 14,

Föreläsning 1 - Håkan Jonsson 9

Datorlab A2506-A2510

Föreläsning 1 - Håkan Jonsson 10

Schedule

Föreläsning 1 - Håkan Jonsson 11

The Object-oriented Model

OOA

OOD

OOP

This course concentrates here

Analyze the problem.

Design a software

Architecture.

Implement in

Code.

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

Föreläsning 1 - Håkan Jonsson 12

Object-Oriented Programming

• Classes

– Are declared

– Patterns for objects

• General behavior and

relations

– A set of classes = a program

– (Are written and found in

text files usually stored in

file systems.)

– Define the type of an object

• Objects– Exist at run-time only

– Have

• a state stored in variables

• methods for transformingthe state (into a final state,the result) based on theway computers are built

– Each object has a specifictype

• Types makes it easier toproduce correct programs.

We will use the object-oriented programming language Java.

Page 4: Course plan Teachers · TwoPictures myPics; PictureFrame myBabyPicture, myGradPicture; myBabyPicture = new PictureFrame(4, 5, "plastic"); myGradPicture = new PictureFrame(10, 14,

Föreläsning 1 - Håkan Jonsson 13

Language graph

C

JavaC++

PHP

Perl

Föreläsning 1 - Håkan Jonsson 14

public class PictureFrame { private int width, height; private String material;

/** modifies: width, height, material post: width == w and height == h and material == m */ public PictureFrame(int w, int h, String m) { width = w; height = h; material = m; }

/** post: result == width */ public int frameWidth() { return width; }

/** post: result == height */ public int frameHeight() { return height; } . . .

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

Föreläsning 1 - Håkan Jonsson 15

PictureFrame

- int width- int height-String material

+ PictureFrame(int w, int h, String m)+ int frameWidth()+ int frameHeight()+ String frameMaterial()

CLASS DIAGRAM - a snapshot of the design

Class name

Instance

variables

methods

What is meant by

+ and -

TwoPictures

- PictureFrame babyPic- PictureFrame graduationPic

+ TwoPictures()+ void setBabyPic(PictureFrame p)+ void setGraduationPic(PictureFrame p)

2

What is the relationship

between PictureFrame

and TwoPictures?

This slide shows some of the TwoPictures design; the next slide shows

an implementation of this design.The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

Föreläsning 1 - Håkan Jonsson 16

public class TwoPictures {

private PictureFrame babyPic; private PictureFrame graduationPic;

/** post: babyPic == null and graduationPic == null */ public TwoPictures() { }

/** pre: pf != null post: babyPic == pf */ public void setBabyPic(PictureFrame pf) { babyPic = pf; }

/** pre: pf != null post: graduationPic == pf */ public void setGraduationPic(PictureFrame pf) { graduationPic = pf; }

}

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

Page 5: Course plan Teachers · TwoPictures myPics; PictureFrame myBabyPicture, myGradPicture; myBabyPicture = new PictureFrame(4, 5, "plastic"); myGradPicture = new PictureFrame(10, 14,

Föreläsning 1 - Håkan Jonsson 17

Object-Oriented Design

1. Identify the objects

– Read the specification!

• Nouns = objects

• Verbs = methods

2. Group objects into classes

3. Locate (and design) the necessary classes,

establishing necessary relationsships

Föreläsning 1 - Håkan Jonsson 18

… but object-oriented programs aren’t constructed from objects.

Objects are the basic building blocks in an O-O program.

Instead, a program consists of one or more classes that define object type.

Each class has two kinds of parts (known as members)

• instance variables determine object state

• methods determine the potential object behavior

Identify the members in the class on the next slide.

Software architecture is determined by classes and their

relationships. Class members, especially public members,

play a key role.

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

Föreläsning 1 - Håkan Jonsson 19

An application begins execution from a method called

main.

The main method must be public, static and void and

have a single parameter of type String[].

If such a main method is stored in Classname.java

it is compiled and run by typing the following shell

commands:

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

How to start executing a Java Program

javac Classname.java

java Classname

A main method is often used merely as a driver = just

starts the real program by creating an object.

Föreläsning 1 - Håkan Jonsson 20

Object Diagram - a snapshot of state of execution

TwoPictures myPics;PictureFrame myBabyPicture, myGradPicture;myBabyPicture = new PictureFrame(4, 5, "plastic");myGradPicture = new PictureFrame(10, 14, "wood");myPics = new TwoPictures();myPics.setBabyPic( myBabyPicture );myPics.setGraduationPic( myGradPicture );

myPics

myBabyPicture myGradPicture

null

null null

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

Page 6: Course plan Teachers · TwoPictures myPics; PictureFrame myBabyPicture, myGradPicture; myBabyPicture = new PictureFrame(4, 5, "plastic"); myGradPicture = new PictureFrame(10, 14,

Föreläsning 1 - Håkan Jonsson 21

Object Diagram - a snapshot of state of execution

TwoPictures myPics;PictureFrame myBabyPicture, myGradPicture;myBabyPicture = new PictureFrame(4, 5, "plastic");myGradPicture = new PictureFrame(10, 14, "wood");myPics = new TwoPictures();myPics.setBabyPic( myBabyPicture );myPics.setGraduationPic( myGradPicture );

myPics

myBabyPicture myGradPicture

null

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

myBabyPicture : PictureFrame

width == 4

height == 5

material == “plastic”

myGradPicture : PictureFrame

width == 10

height == 14

material == “wood”

Föreläsning 1 - Håkan Jonsson 22

Object Diagram - a snapshot of state of execution

TwoPictures myPics;PictureFrame myBabyPicture, myGradPicture;myBabyPicture = new PictureFrame(4, 5, "plastic");myGradPicture = new PictureFrame(10, 14, "wood");myPics = new TwoPictures();myPics.setBabyPic( myBabyPicture );myPics.setGraduationPic( myGradPicture );

myPics : TwoPictures

babyPic == null

graduationPic == null

myBabyPicture : PictureFrame

width == 4

height == 5

material == “plastic”

myGradPicture : PictureFrame

width == 10

height == 14

material == “wood”

myPics

myBabyPicture myGradPicture

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

Föreläsning 1 - Håkan Jonsson 23

Object Diagram - a snapshot of state of execution

TwoPictures myPics;PictureFrame myBabyPicture, myGradPicture;myBabyPicture = new PictureFrame(4, 5, "plastic");myGradPicture = new PictureFrame(10, 14, "wood");myPics = new TwoPictures();myPics.setBabyPic( myBabyPicture );myPics.setGraduationPic( myGradPicture );

myPics : TwoPictures

babyPic

graduationPic == null

myBabyPicture : PictureFrame

width == 4

height == 5

material == “plastic”

myGradPicture : PictureFrame

width == 10

height == 14

material == “wood”

myPics

myBabyPicture myGradPicture

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

Föreläsning 1 - Håkan Jonsson 24

Object Diagram - a snapshot of state of execution

TwoPictures myPics;PictureFrame myBabyPicture, myGradPicture;myBabyPicture = new PictureFrame(4, 5, "plastic");myGradPicture = new PictureFrame(10, 14, "wood");myPics = new TwoPictures();myPics.setBabyPic( myBabyPicture );myPics.setGraduationPic( myGradPicture );

myPics : TwoPictures

babyPic

graduationPic

myBabyPicture : PictureFrame

width == 4

height == 5

material == “plastic”

myGradPicture : PictureFrame

width == 10

height == 14

material == “wood”

myPics

myBabyPicture myGradPicture

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

Page 7: Course plan Teachers · TwoPictures myPics; PictureFrame myBabyPicture, myGradPicture; myBabyPicture = new PictureFrame(4, 5, "plastic"); myGradPicture = new PictureFrame(10, 14,

Föreläsning 1 - Håkan Jonsson 25

Object Diagram - a snapshot of state of execution

TwoPictures myPics;PictureFrame myBabyPicture, myGradPicture;myBabyPicture = new PictureFrame(4, 5, "plastic");myGradPicture = new PictureFrame(10, 14, "wood");myPics = new TwoPictures();myPics.setBabyPic( myBabyPicture );myPics.setGraduationPic( myGradPicture );

myPics : TwoPictures

babyPic

graduationPic

myBabyPicture : PictureFrame

width == 4

height == 5

material == “plastic”

myGradPicture : PictureFrame

width == 10

height == 14

material == “wood”

myPics

The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

Föreläsning 1 - Håkan Jonsson 26

Correctness

• Programs should (of course) be correct.– Note that (non-faulty) computers never err… if they

behave strange it’s because they have been told to doso

• How does one ensure correctness?– Especially when there are many many people

involved using all kinds of different computers andoperating systems, the program consists of gazillionlines of code, and is being simultaneously developedat different locations in different time-zones…

• This course give some techniques and practicesthat helps.

Föreläsning 1 - Håkan Jonsson 27

Software testing and debugging

• To isolate software faults.

• Read O1.6 in Riley to get a short overview

– Page 461 about software engineering and

document reviews…

– SMD136 Software Engineering

(Programvaruteknik, LP1, Kåre Synnes)

Föreläsning 1 - Håkan Jonsson 28

Software Specification

• To ensure that a program behaves in linewith its specification.

– Requirement specifications,

– user manuals,

– on-line help,

– object diagrams,

– class diagrams,

– method specifications,… (Fig. O2.1)

Page 8: Course plan Teachers · TwoPictures myPics; PictureFrame myBabyPicture, myGradPicture; myBabyPicture = new PictureFrame(4, 5, "plastic"); myGradPicture = new PictureFrame(10, 14,

Föreläsning 1 - Håkan Jonsson 29

UML

• Unified Modeling Language

• Class Diagrams

– ”is a…”

– ”has a…”

– ”knows about…”

• Partial specification

Föreläsning 1 - Håkan Jonsson 30

OCL

• Object Constraint Language (a part of UML)– Reily O2.3 - …

• Formal specifications using logical statements– Propositional logic (!, ", ¬, #)

• x ! y " ¬z

– Predicate logic ($ and %)• $x %y (y=2x)

• $x1$x2 (P(x1 ! ¬x2) ! (x2 # Q(x2))

• Are used to ensure correctness via– Pre conditions

– Post conditions

– Invariants

Föreläsning 1 - Håkan Jonsson 31

Correctness in practice

• Design/programming by contract

• Pre and post conditions

– Specify what methods expect and promise

– assert-statement

•java -ea Classfile• (Exceptions are used to ensure robustness, that the program can

handle the unexpected. More about this next lecture.)

Föreläsning 1 - Håkan Jonsson 32The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

Method Contract

Method caller guarantees...precondition & class invariant

(at time of method call)

Method is required to ensure...postcondition & class invariant

(at time of method return)

Addendum: A modifies clause can stipulate

what alterations are permitted

Page 9: Course plan Teachers · TwoPictures myPics; PictureFrame myBabyPicture, myGradPicture; myBabyPicture = new PictureFrame(4, 5, "plastic"); myGradPicture = new PictureFrame(10, 14,

Föreläsning 1 - Håkan Jonsson 33

/* invariant (For every Fraction object) denominator > 0 and gcd(numerator, denominator) == 1 (i.e., this fraction is normalized) */

public class Fraction { private int numerator; private int denominator;

/* pre: d != 0 (throws FractionException) post: real_valueOf(this) == n / d */ public Fraction(int n, int d) { if (d == 0) throw new FractionException("denominator cannot be zero"); if (d < 0) { numerator = -n; denominator = -d; } else { numerator = n; denominator = d; } simplify(); } /* post: result == numerator */ public int numer() { return numerator; } /* post: result == denominator */ public int denom() { return denominator; }

An example of the practical use of pre and post conditions

(1 of 4)

Föreläsning 1 - Håkan Jonsson 34

/* pre: numerator != 0 (throws FractionException) modifies: numerator, denominator post: real_value_of(this) == 1 / (real_value_of(this@pre)) */ public void invert() { if (numerator == 0) throw new FractionException("can't invert fraction with zero numerator"); int temp = numerator; numerator = denominator; denominator = temp; if (denominator < 0) { denominator = -denominator; numerator = -numerator; } } /* modifies: numerator post: real_value_of(this) == -real_value_of(this@pre) */ public void negate() { numerator = -numerator; }

/* pre: f != null (throws FractionException) modifies: numerator, denominator post: real_value_of(result) == real_value_of(this) + real_value_of(f) */ public Fraction sum(Fraction f) { if (f == null) throw new FractionException("null-valued argument"); Fraction result; int newNumerator = numerator*f.denom() + f.numer()*denominator; int newDenominator = denominator * f.denom(); if (newDenominator >= 0) { result = new Fraction(newNumerator, newDenominator); } else { result = new Fraction(-newNumerator, -newDenominator); } result.simplify(); return result; }

(2 of 4)

Föreläsning 1 - Håkan Jonsson 35

/* pre: f != null (throws FractionException) modifies: numerator, denominator post: real_value_of(result) == real_value_of(this) * real_value_of(f) */ public Fraction product(Fraction f) { if (f == null) throw new FractionException("null-valued argument"); Fraction result; result = new Fraction(numerator*f.numer(), denominator*f.denom()); result.simplify(); return result; } /* post: result == numerator + "/" + denominator */ public String toString() { return numerator + "/" + denominator; } /* post: real_value_of(this) = real_value_of(this@pre) and gcd(numerator,denominator) == 1 */ private void simplify() { int div = 2; while ( div <= min(Math.abs(numerator), denominator) ) { if ( (numerator % div == 0) && (denominator % div == 0) ) { numerator = numerator / div; denominator = denominator / div; } else { div++; } } }/* post: a < b implies result == a and a >=b implies result == b */ private int min(int a, int b) { if (a < b) { return a; } else { return b; } }}

(3 of 4)

Föreläsning 1 - Håkan Jonsson 36

/*Specification Functions

real_value_of(Fraction f) [a Real-valued function]

DEFINITION real_value_of(f) == f.numerator / f.denominator INFORMALLY real_value_of(f) is the numeric value from dividing the numerator of f by its denominator.

gcd(int a, int b) [an Integer-valued function]

DEFINITION gcd(a, b) > 0 and a % gcd(a, b) == 0 and b % gcd(a, b) == 0 and forAll (j : Integer and j > gcd(a, b) | a % j != 0 or b % j != 0) INFORMALLY gcd(a, b) is the greatest common divisor of a and b */

(4 of 4)

Page 10: Course plan Teachers · TwoPictures myPics; PictureFrame myBabyPicture, myGradPicture; myBabyPicture = new PictureFrame(4, 5, "plastic"); myGradPicture = new PictureFrame(10, 14,

Föreläsning 1 - Håkan Jonsson 37

Efficiency

• Programs have to be correct but they also need tobe “efficient”.– They should terminate within “reasonable” time and

not need “unreasonable” amounts of computer memory.

– (The exact meaning of “efficient” will be made clearlater on during this course.)

• On a superficial level, efficiency can in most casesbe obtained by careful programming habits - like“tricks of the trade” - and fast hardware (a newGPU that makes a game playable).

• However, there are unfortunately many situationswhere that simply does not suffice.

Föreläsning 1 - Håkan Jonsson 38

An example:

“Circuit Satisfiability”

• Write a program that takes as input a(description of a) logic circuit and decideswhether it can be satisfied or not.– The circuit has many inputs and one output.

– “Description” = a representation of a logicalformula, for example

(¬x1 ! x3 " x2) ! (x2 " x1 ! x4 " ¬x3)

– “Satisfied” = an assignment of truth values to theinput variables (x1, x2, x3, and x4 above) so thatthe output becomes true.

Föreläsning 1 - Håkan Jonsson 39

Basic logic components

Föreläsning 1 - Håkan Jonsson 40

Two (small) examples

OR

AND

NOT

the only difference

Page 11: Course plan Teachers · TwoPictures myPics; PictureFrame myBabyPicture, myGradPicture; myBabyPicture = new PictureFrame(4, 5, "plastic"); myGradPicture = new PictureFrame(10, 14,

Föreläsning 1 - Håkan Jonsson 41

• It’s not that easy to immediately see that Caseb) can not be satisfied (at least not for me).– Is this just a “coincidence”? (Or am I stupid?!)

• No! (Luckily for me… :-) No one has, in fact,ever found a fast (efficient) solution to “CircuitSatisfiability”!– The fastest solution found so far (by Mankind) is

based on trying all combinations of truth values• This is not reasonable except for very small circuits.

• Algorithms is the area where one learns aboutproblems, solutions, and their properties.

• Good programmers know about hard problemsbut also know that there are many easyproblems!