cmput 301: lecture 04 practical oo constructs & uml lecturer: martin jagersand department of...

35
CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous courses by Ken Wong, Eleni Stroulia Zach Dodds, Martin Jagersand

Post on 21-Dec-2015

220 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

CMPUT 301: Lecture 04 Practical OO constructs & UML

Lecturer: Martin JagersandDepartment of Computing Science

University of Alberta

Notes based on previous courses byKen Wong, Eleni Stroulia

Zach Dodds, Martin Jagersand

Page 2: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

2

Goals today:

1. Introducing UML

2. Some more basic OO examples in Java

• Note: Many examples taken from:

Dennis Kafura “Object oriented software design and construction”

• On-line version of book:

http://ei.cs.vt.edu/~kafura/java/

Page 3: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

3

UMLUnified Modeling Language

• Class notation • Object Notation

Page 4: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

4

GUI Window

Page 5: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

5

UML class example

• public class Frame { // version 1 … public Frame ( String name, int initXCoord, int initYCoord, int initWidth, int initHeight ) { … } public void resize( int newHeight, int newWidth ) { … } public void moveTo( int newXCoord, int newYCoord ) { … } public void drawText( String text, int atX, int atY ) { … } …}

Java

Page 6: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

6

UML Object Notation

Page 7: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

7

Named Constants

• public class Frame { … public static final int MOUSE_DOWN = 1; public static final int MOUSE_UP = 2; public static final int MOUSE_DRAG = 3; public static final int MOUSE_MOVE = 4; …}

• if (eventType == Frame.MOUSE_DOWN) { …}

Page 8: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

8

UML Constant Notation

• Public class-level attributes in the Frame class:+MOUSE_DOWN: Integer = 1; {invariant}+MOUSE_UP: Integer = 2; {invariant}+MOUSE_DRAG: Integer = 3; {invariant}+MOUSE_MOVE: Integer = 4; {invariant}

Page 9: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

9

Overloaded Methods

• Overloaded methods are a set of methods (of a class) that have the same name (and same conceptual action) but different argument lists.

Page 10: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

10

Overloaded Methods

• public class Frame { … // constructors overloaded to provide defaults public Frame( String name, int initXCoord, int initYCoord ); public Frame( String name ); public Frame(); … // methods overloaded for different inputs public void resize( int newHeight, int newWidth ); public void resize( float factor ); …}

Page 11: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

11

UML Class Notation

Page 12: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

12

Interacting Objects

• An object can be passed around, representing a bundle of related information.

• The information exchange occurs between a sender object and receiver object.

Page 13: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

13

Interacting Objects

• To pass information from a sender object to a receiver object:– parameter passing– return value

Page 14: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

14

Parameter Passing

• public class Sender { … public void callerMethod() { Receiver receiver = new Receiver(); Info argRef = new Info(); … receiver.calledMethod( argRef ); … }}

• public class Receiver { … public void calledMethod( Info infoRef ) { // do something with passed Info object … }}

Page 15: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

15

Return Value

• public class Receiver { … public void callerMethod() { Sender sender = new Sender(); Info infoRef = sender.calledMethod(); // do something with returned Info object … }}

• public class Sender { … public Info calledMethod() { Info resultRef = new Info(); … // return a reference to an Info object return resultRef; }}

Page 16: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

16

Call By Value

• In Java, there is essentially only call by value.

• A primitive value (e.g., int, char, etc.) is copied when passed.

• An object has its reference copied when passed; the object itself is not copied.

• Strings and arrays are passed like objects.

Page 17: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

17

Call By Value

• public class Sender { … public void callerMethod() { Receiver receiver = new Receiver(); Info argRef = new Info(); … receiver.calledMethod( argRef ); argRef.doSomeMore(); }}

• public class Receiver { … public void calledMethod( Info infoRef ) { infoRef.doSomething(); infoRef = null; }}

Page 18: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

18

Collaborating Objects

• A group of interacting objects can form:– associations (or acquaintances)– aggregations (or containments)

Page 19: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

19

Collaborating Objects

• Each object has a role in the group and collaborates with (i.e., uses, connects to, knows about) other objects to perform its role.

Page 20: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

20

Collaborating Objects

• Connections between objects:– client/server

(unidirectional)– client object invokes methods of server object

– client object knows about the server object

– peer(bidirectional)

– peers know about each other and use each others’ methods

Page 21: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

21

Sequences of Interactions

• A controlling object might perform a sequence of operations over a set of objects.

Page 22: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

22

UML Sequence Diagram

Page 23: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

23

UML Sequence Diagram

• Participating objects.• Time flows down.• Method invocations.• Iteration and conditionals.• Activation of objects.• Creation of objects.• Optional return arrows.• Notes.• Not to scale.

Page 24: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

24

Sequence

• Displaying an image:– ask user for filename of image– create image using contents of given file– draw image in a Frame

Page 25: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

25

Sequence

• Classes of participating objects:– ImageTest

(implements Program interface)– FileChooser

(asks for filename)– Image

(create image from file)– Frame

(draw image)

Page 26: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

26

UML Sequence diagram

Page 27: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

27

Assignment 1

• You are to design and implement a simple image viewer that allows the user to:– load and view an image – view the image size – rescale the size of the image – adjust the brightness of the image

• See detailed specification on the course www page

Page 28: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

28

Study (unix/linux) image editor:xv

Page 29: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

29

Mathematical / Computationalimage models

• Continuous mathematical: I = f(x,y)

• Discrete (in computer) addressable 2D array:

I = matrix(i,j)• Discrete (in file) e.g. ascii or binary

sequence:023 233 132 232125 134 134 212

Page 30: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

30

Image representation for display

• True color, RGB, ….

(R,G,B) (R,G,B) … (R,G,B)

:

(R,G,B)

Page 31: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

31

Image representation for display

• Indexed image

(I) (I) … (I)

:

(I)

(R,G,B)

(R,G,B)

:

(R,G,B)

Page 32: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

32

Point operations on images

• Point operations perform some operation on one pixel at a time (independent on the neighboring pixels) For each (x,y)

I2(x,y) = f(I(x,y))

• Contrast to image transforms (later in course) perform operations on the whole image

Page 33: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

33

Common point operations

• Brightness adjustment

• Contrast adjustment– Dynamic range compression– Gray level slicing

• Histogram equalization

• Image (sequence) averaging

• Background subtraction

Page 34: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

34

Linear brightness and contrast adjustment

• As seen on TV!

• Brightness

For each (x,y)

I2(x,y) = I(x,y)+const

• Contrast

I2(x,y) = const*I(x,y)

Page 35: CMPUT 301: Lecture 04 Practical OO constructs & UML Lecturer: Martin Jagersand Department of Computing Science University of Alberta Notes based on previous

35

Contrast adjustment example