cop 3503 fall 2012 shayan javed lecture 9

55
1 / 55 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 9 Programming Fundamentals using Java 1

Upload: carson

Post on 23-Feb-2016

64 views

Category:

Documents


1 download

DESCRIPTION

COP 3503 FALL 2012 Shayan Javed Lecture 9. Programming Fundamentals using Java. UML Diagrams. UML. U nified M odeling L anguage. UML. U nified M odeling L anguage Goal: Common language for creating models of Object-Oriented software. UML. U nified M odeling L anguage Goal: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COP 3503 FALL 2012 Shayan Javed Lecture 9

1 / 55

COP 3503 FALL 2012SHAYAN JAVED

LECTURE 9

Programming Fundamentals using Java

1

Page 2: COP 3503 FALL 2012 Shayan Javed Lecture 9

2 / 55

UML Diagrams

Page 3: COP 3503 FALL 2012 Shayan Javed Lecture 9

3 / 55

UML

Unified Modeling Language

Page 4: COP 3503 FALL 2012 Shayan Javed Lecture 9

4 / 55

UML

Unified Modeling Language Goal:

Common language for creating models of Object-Oriented software

Page 5: COP 3503 FALL 2012 Shayan Javed Lecture 9

5 / 55

UML

Unified Modeling Language Goal:

Common language for creating models of Object-Oriented software

Two aspects: Class Diagrams (Static design): Classes, Attributes,

Relationships

Page 6: COP 3503 FALL 2012 Shayan Javed Lecture 9

6 / 55

UML

Unified Modeling Language Goal:

Common language for creating models of Object-Oriented software

Two aspects: Class Diagrams (Static design): Classes, Attributes,

Relationships Dynamic design: Objects, messages, finite state

machines [Sequence/Activity/State Machine diagrams]

Page 7: COP 3503 FALL 2012 Shayan Javed Lecture 9

7 / 55

Class Diagrams

Depict classes within a model

Page 8: COP 3503 FALL 2012 Shayan Javed Lecture 9

8 / 55

Class Diagrams

Depict classes within a model Classes have:

Attributes (Properties/Member Variables)

Page 9: COP 3503 FALL 2012 Shayan Javed Lecture 9

9 / 55

Class Diagrams

Depict classes within a model Classes have:

Attributes (Properties/Member Variables) Operations (Functions/Methods)

Page 10: COP 3503 FALL 2012 Shayan Javed Lecture 9

10 / 55

Class Diagrams

Depict classes within a model Classes have:

Attributes (Properties/Member Variables) Operations (Functions/Methods) Relationships with other classes

Page 11: COP 3503 FALL 2012 Shayan Javed Lecture 9

11 / 55

Class Diagrams

Class

Properties

Methods()

Page 12: COP 3503 FALL 2012 Shayan Javed Lecture 9

12 / 55

Class Diagrams

GeometricObject

color : Stringname : Stringarea : double

GeometricObject()GeometricObject(String, String)get() : String/doubleset(..) : void

Page 13: COP 3503 FALL 2012 Shayan Javed Lecture 9

13 / 55

Class Diagrams

Properties

name : type

Page 14: COP 3503 FALL 2012 Shayan Javed Lecture 9

14 / 55

Class Diagrams

Properties

name : type

Ex.: area : double

Page 15: COP 3503 FALL 2012 Shayan Javed Lecture 9

15 / 55

Class Diagrams

Methods:

name(parameters) : return type

Page 16: COP 3503 FALL 2012 Shayan Javed Lecture 9

16 / 55

Class Diagrams

Methods:

name(parameters) : return type

Ex.: getArea( ) : double

Page 17: COP 3503 FALL 2012 Shayan Javed Lecture 9

17 / 55

Class Diagrams

Methods:

name(parameters) : return type

Ex.: getArea( ) : double setColor(color : String) : void

Page 18: COP 3503 FALL 2012 Shayan Javed Lecture 9

18 / 55

Class Diagrams

Properties/Methods can be public/private/protected/static

Page 19: COP 3503 FALL 2012 Shayan Javed Lecture 9

19 / 55

Class Diagrams

Properties/Methods can be public/private/protected/static

Need a way to indicate that somehow

Page 20: COP 3503 FALL 2012 Shayan Javed Lecture 9

20 / 55

Class Diagrams

Properties/Methods can be public/private/protected/static

Need a way to indicate that somehow Add a sign next to the property/method:

Public (+)

Page 21: COP 3503 FALL 2012 Shayan Javed Lecture 9

21 / 55

Class Diagrams

Properties/Methods can be public/private/protected/static

Need a way to indicate that somehow Add a sign next to the property/method:

Public (+) Private (-)

Page 22: COP 3503 FALL 2012 Shayan Javed Lecture 9

22 / 55

Class Diagrams

Properties/Methods can be public/private/protected/static

Need a way to indicate that somehow Add a sign next to the property/method:

Public (+) Private (-) Protected(#)

Page 23: COP 3503 FALL 2012 Shayan Javed Lecture 9

23 / 55

Class Diagrams

Properties/Methods can be public/private/protected/static

Need a way to indicate that somehow Add a sign next to the property/method:

Public (+) Private (-) Protected(#)

To indicate a static property/method, underline it

Page 24: COP 3503 FALL 2012 Shayan Javed Lecture 9

24 / 55

Class Diagrams

Final (constant) properties are declared by writing them in ALL_CAPS (Math.PI for example)

Page 25: COP 3503 FALL 2012 Shayan Javed Lecture 9

25 / 55

Class Diagrams

GeometricObject

# Color : String# name : String# area : double

+ GeometricObject()+ GeometricObject(String, String)+ get() : String/double+ set(..) : void

Page 26: COP 3503 FALL 2012 Shayan Javed Lecture 9

26 / 55

Class Diagrams

But GeometricObject is abstract

Page 27: COP 3503 FALL 2012 Shayan Javed Lecture 9

27 / 55

Class Diagrams

But GeometricObject is abstract

How do you differentiate between an abstract and non-abstract class/method?

Page 28: COP 3503 FALL 2012 Shayan Javed Lecture 9

28 / 55

Class Diagrams

But GeometricObject is abstract

How do you differentiate between an abstract and non-abstract class/method?

Italicize it

Page 29: COP 3503 FALL 2012 Shayan Javed Lecture 9

29 / 55

Class Diagrams

GeometricObject

# Color : String# name : String# area : double

+ GeometricObject()+ GeometricObject(String, String)+ get() : String/double+ set(..) : void+ getArea() : double

Page 30: COP 3503 FALL 2012 Shayan Javed Lecture 9

30 / 55

Class Diagrams

But can’t italicize when writing...

Page 31: COP 3503 FALL 2012 Shayan Javed Lecture 9

31 / 55

Class Diagrams

But can’t italicize when writing...

So to indicate abstract methods/classes on paper write {abstract} in brackets

Page 32: COP 3503 FALL 2012 Shayan Javed Lecture 9

32 / 55

Class Diagrams

GeometricObject {abstract}

# Color : String# name : String# area : double

+ GeometricObject()+ GeometricObject(String, String)+ get() : String/double+ set(..) : void+ getArea() : double {abstract}

Page 33: COP 3503 FALL 2012 Shayan Javed Lecture 9

33 / 55

Class Diagrams

Relationships between classes: Generalization (Inheritance = “is a”)

Page 34: COP 3503 FALL 2012 Shayan Javed Lecture 9

34 / 55

Class Diagrams

Relationships between classes: Generalization (Inheritance = “is a”) Aggregation (“has a”)

Page 35: COP 3503 FALL 2012 Shayan Javed Lecture 9

35 / 55

Class Diagrams

Relationships between classes: Generalization (Inheritance = “is a”) Aggregation (“has a”) Composition (“owns a”)

Page 36: COP 3503 FALL 2012 Shayan Javed Lecture 9

36 / 55

Class Diagrams

Generalization (Inheritance - “is a”):

Circle

- radius : double

+ Circle (...)+ getRadius( ) : double+ getArea( ) : double+ setRadius(radius : double) : void

Page 37: COP 3503 FALL 2012 Shayan Javed Lecture 9

37 / 55

Class Diagrams

Generalization (Inheritance - “is a”):

Circle

GeometricObject

Page 38: COP 3503 FALL 2012 Shayan Javed Lecture 9

38 / 55

Class Diagrams

Generalization (Inheritance - “is a”):

Used to indicate inheritance relationship

Page 39: COP 3503 FALL 2012 Shayan Javed Lecture 9

39 / 55

Class Diagrams

Generalization (Inheritance - “is a”):

Circle

GeometricObject

Rectangle

Page 40: COP 3503 FALL 2012 Shayan Javed Lecture 9

40 / 55

Class Diagrams

Aggregation (“has a”):

Page 41: COP 3503 FALL 2012 Shayan Javed Lecture 9

41 / 55

Class Diagrams

Aggregation (“has a”):

Class1 has a number of Class2

Page 42: COP 3503 FALL 2012 Shayan Javed Lecture 9

42 / 55

Class Diagrams

Aggregation (“has a”):

Class1 has a number of Class2

Course has a number of Students Course has many Students

Page 43: COP 3503 FALL 2012 Shayan Javed Lecture 9

43 / 55

Class Diagrams

Aggregation (“has a”):

public Course {Student[] students = new Student[capacity];

}

Page 44: COP 3503 FALL 2012 Shayan Javed Lecture 9

44 / 55

Class Diagrams

Aggregation (“has a”):

public Course {Student[] students = new Student[capacity];

}

Student Course0..capacitystudents

Page 45: COP 3503 FALL 2012 Shayan Javed Lecture 9

45 / 55

Class Diagrams

Aggregation (“has a”):

0...capacity = number of students in the Course

“students” = the variable

Page 46: COP 3503 FALL 2012 Shayan Javed Lecture 9

46 / 55

Class Diagrams

Aggregation (“has a”):

0...capacity = number of students in the Course

“students” = the variable

So aggregation indicated by empty diamond

Page 47: COP 3503 FALL 2012 Shayan Javed Lecture 9

47 / 55

Class Diagrams

Composition (“owns a”):

Page 48: COP 3503 FALL 2012 Shayan Javed Lecture 9

48 / 55

Class Diagrams

Composition (“owns a”):

Similar to Aggregation – but one class completely depends on the other.

Page 49: COP 3503 FALL 2012 Shayan Javed Lecture 9

49 / 55

Class Diagrams

Composition (“owns a”):

Similar to Aggregation – but one class completely depends on the other. Destroying one also destroys the dependent class

Page 50: COP 3503 FALL 2012 Shayan Javed Lecture 9

50 / 55

Class Diagrams

Composition (“owns a”):

Wheels Car4wheels

Page 51: COP 3503 FALL 2012 Shayan Javed Lecture 9

51 / 55

Class Diagrams

Composition (“owns a”):

Deleting a Car object also deletes the Wheel objects

Wheels Car4wheels

Page 52: COP 3503 FALL 2012 Shayan Javed Lecture 9

52 / 55

Class Diagrams

Composition (“owns a”):

Aggregation (“has a”):

Page 53: COP 3503 FALL 2012 Shayan Javed Lecture 9

53 / 55

Class Diagrams

Interfaces: Need to show interfaces being implemented

Page 54: COP 3503 FALL 2012 Shayan Javed Lecture 9

54 / 55

Class Diagrams

Interfaces:

Circle

<<interface>>Comparable

+ compareTo (Object) : int

r

Page 55: COP 3503 FALL 2012 Shayan Javed Lecture 9

55 / 55

Summary

UML Diagrams = used for creating models of Object-Oriented Software

Class diagrams = Depict classes Properties Methods Relationships

Relationships: Generalization (Inheritance = “is a”) Aggregation (“has a”) Composition (“owns a”)