cmput 301: lecture 04 practical oo constructs & uml lecturer: martin jagersand department of...
Post on 21-Dec-2015
220 views
TRANSCRIPT
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
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/
3
UMLUnified Modeling Language
• Class notation • Object Notation
4
GUI Window
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
6
UML Object Notation
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) { …}
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}
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.
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 ); …}
11
UML Class Notation
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.
13
Interacting Objects
• To pass information from a sender object to a receiver object:– parameter passing– return value
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 … }}
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; }}
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.
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; }}
18
Collaborating Objects
• A group of interacting objects can form:– associations (or acquaintances)– aggregations (or containments)
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.
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
21
Sequences of Interactions
• A controlling object might perform a sequence of operations over a set of objects.
22
UML Sequence Diagram
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.
24
Sequence
• Displaying an image:– ask user for filename of image– create image using contents of given file– draw image in a Frame
25
Sequence
• Classes of participating objects:– ImageTest
(implements Program interface)– FileChooser
(asks for filename)– Image
(create image from file)– Frame
(draw image)
26
UML Sequence diagram
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
28
Study (unix/linux) image editor:xv
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
30
Image representation for display
• True color, RGB, ….
(R,G,B) (R,G,B) … (R,G,B)
:
(R,G,B)
31
Image representation for display
• Indexed image
(I) (I) … (I)
:
(I)
(R,G,B)
(R,G,B)
:
(R,G,B)
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
33
Common point operations
• Brightness adjustment
• Contrast adjustment– Dynamic range compression– Gray level slicing
• Histogram equalization
• Image (sequence) averaging
• Background subtraction
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)
35
Contrast adjustment example