0% found this document useful (0 votes)
101 views

Cse 142-Object Oriented Programming: Lecture # 6 Uml Diagrams

This document discusses UML diagrams, specifically class diagrams. It begins by explaining that UML diagrams fall into two categories: structure diagrams and behavior diagrams. It then focuses on class diagrams, which are a type of structure diagram used to describe the structure of a system in terms of classes and objects. The rest of the document discusses the key components of a class diagram, including attributes, methods, accessors, mutators, and how to convert a UML class diagram into code.

Uploaded by

Mohmmad Faizan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

Cse 142-Object Oriented Programming: Lecture # 6 Uml Diagrams

This document discusses UML diagrams, specifically class diagrams. It begins by explaining that UML diagrams fall into two categories: structure diagrams and behavior diagrams. It then focuses on class diagrams, which are a type of structure diagram used to describe the structure of a system in terms of classes and objects. The rest of the document discusses the key components of a class diagram, including attributes, methods, accessors, mutators, and how to convert a UML class diagram into code.

Uploaded by

Mohmmad Faizan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

CSE 142- OBJECT ORIENTED PROGRAMMING

LECTURE # 6
UML DIAGRAMS

Spring'19 Object Oriented Programming 1


UNIFIED MODELING LANGUAGE,
VERSION 2.0
• 2 types of diagrams
–Structure Diagrams
• Provide a way for representing the data and
static relationships that are in an information
system
• Class diagram
–Behavior Diagrams
STRUCTURE DIAGRAMS
• Class Diagram
–Describe the structure of the system in
terms of classes and objects
–Primary purpose during analysis workflow: to
create a vocabulary that is used by both the
analyst and users
UML CLASS DIAGRAM
• A UML class diagram is a graphical tool that can aid in the design of a class.
• The diagram has three main sections.

Class Name UML diagrams are easily converted


Attributes to Java class files. There will be more
about UML diagrams a little later.
Methods

• The class name should concisely reflect what


the class represents.
3-4
DESIGNING A CLASS

• When designing a class, decisions about the following must be made.


– what data must be accounted for,
– what actions need to be performed,
– what data can be modified,
– what data needs to be accessible, and
– any rules as to how data should be modified.
• Class design typically is done with the aid of a Unified Modeling Language (UML) diagram.

3-5
TYPES OF CLASSES
• Ones found during analysis:
–people, places, events, and things about which
the system will capture information
–ones found in application domain
• Ones found during design
–specific objects like windows and forms that
are used to build the system
2 KINDS OF CLASSES
DURING ANALYSIS

• Concrete
– Class from application domain
– Example: Customer class and Employee class
• Abstract
– Useful abstractions
– Example: Person class
ATTRIBUTES

• The data elements of a class define the object to be instantiated from the class.
• The attributes must be specific to the class and define it completely.
• Example: A rectangle is defined by
– length
– width.
• The attributes are then accessed by methods within the class.

3-8
ATTRIBUTES IN A CLASS
• Properties of the class about which we
want to capture information
• Represents a piece of information that is
relevant to the description of the class
within the application domain
ATTRIBUTES IN A CLASS
• Only add attributes that are primitive or
atomic types
• Derived attribute
–attributes that are calculated or derived from
other attributes
METHODS

• The attributes of a class might need to be:


– changed,
– accessed, and
– calculated.
• The methods that change and access attributes are called accessors and mutators.

3-11
ACCESSORS AND MUTATORS

• Because of the concept of data hiding, fields in a class are private.


• The methods that retrieve the data of fields are called accessors.
• The methods that modify the data of fields are called mutators.
• Each field that the programmer wishes to be viewed by other
classes needs an accessor.
• Each field that the programmer wishes to be modified by other
classes needs a mutator.

3-12
ACCESSORS AND MUTATORS

• For the Rectangle example, the accessors and mutators are:


– setLength : Sets the value of the length field.
public void setLength(double len) …
– setWidth : Sets the value of the width field.
public void setLength(double w) …
– getLength : Returns the value of the length field.
public double getLength() …
– getWidth : Returns the value of the width field.
public double getWidth() …
• Other names for these methods are getters and setters.

3-13
UML DATA TYPE AND PARAMETER
NOTATION
• UML diagrams are language independent.
• UML diagrams use an independent notation to show return types, access
modifiers, etc.

Access modifiers are Rectangle


denoted as:
+ public
- private - width : double
# protected
+ setWidth(w : double) : void

3-14
UML DATA TYPE AND PARAMETER
NOTATION
• UML diagrams are language independent.
• UML diagrams use an independent notation to show return types, access
modifiers, etc.
Variable types are
Rectangle placed after the variable
name, separated by a
colon.
- width : double

+ setWidth(w : double) : void

3-15
UML DATA TYPE AND PARAMETER
NOTATION
• UML diagrams are language independent.
• UML diagrams use an independent notation to show return types, access
modifiers, etc.

Method return types are


Rectangle placed after the method
declaration name,
separated by a colon.
- width : double

+ setWidth(w : double) : void

3-16
UML DATA TYPE AND PARAMETER
NOTATION
• UML diagrams are language independent.
• UML diagrams use an independent notation to show return types, access
modifiers, etc.
Method parameters
are shown inside the
parentheses using the Rectangle
same notation as
variables.
- width : double

+ setWidth(w : double) : void

3-17
CONVERTING THE UML DIAGRAM TO CODE

• Putting all of this information together, a Java class file can be built easily
using the UML diagram.
• The UML diagram parts match the Java class file structure.

class header
ClassName
{
Attributes Attributes
Methods Methods
}

3-18
CONVERTING THE UML DIAGRAM TO CODE
The structure of the class can be public class Rectangle
{
compiled and tested without having
private double width;
bodies for the methods. Just be sure to private double length;
put in dummy return values for methods
that have a return type other than void. public void setWidth(double w)
{
}
public void setLength(double len)
{
}
public double getWidth()
{ return 0.0;
}
public double getLength()
{ return 0.0;
}
public double getArea()
{ return 0.0;
}
}
3-19
Rectangle

- width : double
- length : double

+ setWidth(w : double) : void


+ setLength(len : double): void
+ getWidth() : double
+ getLength() : double
+ getArea() : double

Spring'19 Object Oriented Programming 20


CONVERTING THE UML DIAGRAM TO CODE
public class Rectangle
Once the class structure has been {
tested, the method bodies can be private double width;
written and tested. private double length;

public void setWidth(double w)


{ width = w;
}
Rectangle public void setLength(double len)
{ length = len;
- width : double }
- length : double public double getWidth()
{ return width;
+ setWidth(w : double) : void }
public double getLength()
+ setLength(len : double: void { return length;
+ getWidth() : double }
+ getLength() : double public double getArea()
+ getArea() : double { return length * width;
}
}
3-21
CONSTRUCTORS IN UML
• In UML, the most common way constructors are defined is:

Rectangle
Notice there is no
return type listed
- width : double for constructors.
- length : double
+Rectangle(len:double, w:double)
+ setWidth(w : double) : void
+ setLength(len : double): void
+ getWidth() : double
+ getLength() : double
+ getArea() : double

3-22
CLASS LAYOUT CONVENTIONS

• The layout of a source code file can vary by employer or instructor.


• Generally the layout is:
– Attributes are typically listed first
– Methods are typically listed second
• The main method is sometimes first, sometimes last.
• Accessors and mutators are typically grouped.

3-23
A DRIVER PROGRAM

• An application in Java is a collection of classes that interact.


• The class that starts the application must have a main method.
• This class can be used as a driver to test the capabilities of other classes.
• In the Rectangle class example, notice that there was no main method.

3-24
A DRIVER PROGRAM
public class RectangleDemo public class Rectangle
{ {
public static void main(String[] args) private double width;
{ private double length;
Rectangle r = new Rectangle();
r.setWidth(10); public void setWidth(double w)
r.setLength(10); {width = w;
System.out.println("Width = " }
+ r.getWidth()); public void setLength(double len)
System.out.println("Length = " {length = len;
+ r.getLength()); }
System.out.println("Area = " public double getWidth()
+ r.getArea()); {return width;
} }
} public double getLength()
{return length;
This RectangleDemo class is a }
public double getArea()
Java application that uses the {return length * width;
Rectangle class. }
}
Another Example:
LengthWidthDemo.java
3-25
Example of a Class Diagram
Video Rental System
visibility multiplicity class name

Customer 1..* 1..*


Video
-CID: int -cassetteID : int
-name: String rents -cassetteVolumeNo: int
+rentMovie()
+authenticateCustomer ()

attributes relationship methods


UML CLASS DIAGRAM FOR FLIGHT

Spring'19 Object Oriented Programming 27


DRAW UML CLASS DIAGRAM
• A class called Book is designed (as shown in the class diagram) to model a book written by one author. It
contains:
• Four private instance variables: name (String), author (of the class Author you have just created, assume
that a book has one and only one author), price (double), and qty (int);
• Two constructors:
– public Book (String name, Author author, double price) { ...... }
– public Book (String name, Author author, double price, int qty) { ...... }

• public methods getName(), getAuthor(), getPrice(), setPrice(), getQty(), setQty().


• A toString() that returns "Book[name=?,Author[name=?,email=?,gender=?],price=?,qty=?". You should
reuse Author’s toString().
• Write a test driver called TestBook to test all the public methods in the class Book. Take Note that you
have to construct an instance of Author before you can construct an instance of Book.

Spring'19 Object Oriented Programming 28


Spring'19 Object Oriented Programming 29
Spring'19 Object Oriented Programming 30
CONVERT EMPLOYEE CLASS DIAGRAM
INTO CODE

Spring'19 Object Oriented Programming 31


DRAW
• class called MyPoint, which models a 2D point with x and y coordinates, is designed as shown
in the class diagram. It contains:
• Two instance variables x (int) and y (int).
• A default (or "no-argument" or "no-arg") constructor that construct a point at the default
location of (0, 0).
• A overloaded constructor that constructs a point with the given x and y coordinates.
• Getter and setter for the instance variables x and y.
• A method setXY() to set both x and y.
• A method getXY() which returns the x and y in a 2-element int array.
• A toString() method that returns a string description of the instance in the format "(x, y)".
• A method called distance(int x, int y) that returns the distance from this point to another point
at the given (x, y) coordinates, e.g.,
– MyPoint p1 = new MyPoint(3, 4);
– System.out.println(p1.distance(5, 6));
Spring'19 Object Oriented Programming 32
• An overloaded distance(MyPoint another) that returns the distance from this point to the
given MyPoint instance (called another), e.g.,
– MyPoint p1 = new MyPoint(3, 4);
– MyPoint p2 = new MyPoint(5, 6);
– System.out.println(p1.distance(p2));
• Another overloaded distance() method that returns the distance from this point to the origin
(0,0), e.g.,
– MyPoint p1 = new MyPoint(3, 4);
– System.out.println(p1.distance());

Write the code for class myPoint. Also write a test program (called TestMyPoint) to test
all the methods defined n the class.

Spring'19 Object Oriented Programming 33

You might also like