Lab Manual No 15 (Class Diagram)
Lab Manual No 15 (Class Diagram)
Lab Manual No 15 (Class Diagram)
CAED
Lab Manual 15
Dated:
st th
4 December, 2023 to 8 December, 2023
Semester:
FALL 2023
Lab Instructions
1. All Students should reach at lab on time.
2. Students should sit on the same computer during whole semester.
3. Student should make a directory by his/her name and store all his/her data in it.
4. Student could take the backup of his/her program the instructor permission.
5. Record Instructions: 1. Lab copy should be updated. 2. Lab copy should be checked on each lab.
Lab Objective:
Understand what is UML?, What are its goals?, Different types different UML diagrams and their usage.
How to add Notes and Constraints in UML?, Understand concepts of “Association” and “Relationship”
between classes, How to represent associations in UML?.
Understand concepts of “Relationship” between different classes and a class itself, Understand
“Generalization” in UML, How to represent these things in UML?.
Understand “Association classes, Links, Aggregation and Composition” in UML, How to represent
these things in UML?
Tools:
MS Visio, E-draw
UML:
UML is a standard language for specifying, visualizing, constructing, and documenting the artifacts
of software systems.
UML stands for Unified Modelling Language.
UML is different from the other common programming languages like C++, Java, COBOL
etc.
UML is a pictorial language used to make software blue prints.
So UML can be described as a general purpose visual modelling language to visualize, specify,
construct and document software system.
UML has a direct relation with object oriented analysis and design.
A picture is worth a thousand words, this absolutely fits while discussing about UML. There
are a number of goals for developing UML but the most important is to define some general
purpose modelling language which all modelers can use and also it needs to be made simple
to understand and use.
UML diagrams are not only made for developers but also for business users, common people
and anybody interested to understand the system. The system can be a software or non-
software. So it must be clear that UML is not a development method rather it accompanies
with processes to make a successful system.
At the conclusion the goal of UML can be defined as a simple modelling mechanism to
model all possible practical systems in today’s complex environment.
A Conceptual model of UML:
To understand conceptual model of UML first we need to clarify: What is a conceptual model? and
why a conceptual model is at all required?
A conceptual model can be defined as a model which is made of concepts and their
relationships. A conceptual model is the first step before drawing a UML diagram. It helps
to understand the entities in the real world and how they interact with each other.
As UML describes the real time systems it is very important to make a conceptual model and then
proceed gradually. Conceptual model of UML can be mastered by learning the following three
major elements:
UML building blocks
Rules to connect the building blocks
Common mechanisms of UML
Class: Class is an entity, a template for making objects, a collection of things that share
some common features.
Object: Object represent an instance of a single entity/class.
Abstraction: It means, simply, to filter out an object's properties and operations until just the
ones you need are left.
Encapsulation: Encapsulation is the mechanism of hiding data from outside world.
Association: When classes are connected together conceptually.
Composition: When an object is composed of another object.
Inheritance: Inheritance is the mechanism of making new classes from existing one.
Polymorphism: It defines the mechanism to exist in different forms.
Interface: Implemented by multiple classes.
UML Diagrams:
There are two broad categories of diagrams and then are again divided into sub-categories:
1. Structural Diagrams
2. Behavioral Diagrams
The structural diagrams represent the static aspect of the system. These static aspects represent
those parts of a diagram which forms the main structure and therefore stable.
Behavioral diagrams basically capture the dynamic aspect of a system. Dynamic aspect can be
further described as the changing/moving parts of a system.
UML has the following five types of behavioral diagrams:
Use case diagram
Sequence diagram
Collaboration diagram
Activity diagram
UML Class Diagram
The class diagram is a static diagram. It represents the static view of an application. The class
diagram describes the attributes and operations of a class and also the constraints imposed on the
system.
Purpose:
The cla’ss diagrams are widely used in the modelling of object oriented systems because they are
the only UML diagrams which can be mapped directly with object oriented languages.
As objects in the Person class, you and I each have these attributes: height, weight, and age.
(You can imagine a number of others.) Each of us is unique because of the specific values that each
of us has for those attributes. We also perform these operations: eat, sleep, read, write, talk, go to
work, and more (Or in object speak, eat(), sleep(), read(), write(), talk(), and goToWork()).
The pieces of information about an operation are called the operation's signature.
Example of a Class diagram:
Let’s take an example of a washing machine.
The class icon enables you to specify still another type of information about a class. In an
area below the operations list, you can show the class's responsibility. The responsibility is a
description of what the class has to do—that is, what its attributes and operations are trying to
accomplish. A washing machine, for example, has the responsibility of taking dirty clothes as input
and producing clean clothes as output.
Notes
All Vehicles have some common attributes (speed and colour) and common behaviour
(turnLeft, turnRight)
Bicycle and MotorVehicle are both kinds of Vehicle and are therefore shown to inherit
from Vehicle. To put this another way, Vehicle is the superclass of both Bicycle and
MotorVehicle
Teachers are modelled as Lecturer objects. As a lecturer may teach more than one course
there is an association between Course and Lecturer. The “taughtBy” relationship shows that
a Course only has a single teacher, but that a lecturer may teach several Courses.
Each Lecturer object also maintains a list of the Courses that it teaches.
There is a similar relationship between Course and Student. A course is attended by zero
or more Students, and a Student may attend multiple courses.
This example also demonstrates the use of interfaces. The diagram shows a Person interface that
stipulates that objects conforming to this interface will have a getName and getEmailAddress
methods. Both Lecturer and Student are shown to be types of Person.
Case Study #3: Mixed Example – Company, Employee, Manager
This example is from a system that models companies, e.g. for a payroll or reporting system. See if
you can interpret it yourself!
The class icon enables you to specify still another type of information about a class. In an
area below the operations list, you can show the class's responsibility. The responsibility is a
description of what the class has to do—that is, what its attributes and operations are trying to
accomplish. A washing machine, for example, has the responsibility of taking dirty clothes as input
and producing clean clothes as output.
Constraints:
UML provides facility to add constraints on a class attributes or properties. Constraint is
added as a free-form text enclosed in curly brackets. The bracketed text specifies one or more rules
the class follows. For example, suppose in the WashingMachine class you wanted to specify that the
capacity of a washer can be only 16, 18, or 20 pounds (and thus "constrain" the WashingMachine
class's capacity attribute). You would write {capacity = 16 or 18 or 20 lbs} near the
WashingMachine class icon.
A conceptual model can be defined as a model which is made of concepts and their
relationships. A conceptual model is the first step before drawing a UML diagram. It helps to
understand the entities in the real world and how they interact with each other. As UML describes
the real time systems, it is very important to make a conceptual model and then proceed gradually.
Classes are interrelated to each other in specific ways. In particular, relationships in class diagrams
include different types of logical connections. The following are such types of logical connections
that are possible in UML:
Association
Directed Association
Reflexive Association
Multiplicity
Inheritance/Generalization
Aggregation
Composition
Realization
Associations:
When classes are connected together conceptually, that connection is called an association.
Association can be uni-directional or bi-directional.
Uni-directional: A Car “contains” Wheel but Wheel does not contain Car.
Bi-directional: A Boy is married to a Girl.
Associations in UML
Let's examine one—the association between a player and a team. You can characterize this
association with the phrase "a player plays for a team." You visualize the association as a line
connecting the two classes, with the name of the association ("Plays for") just above the line. You
show how to read the relationship with a filled triangle pointing in the appropriate direction.
When on class associates with another, each one plays a role with that association. You can
show each class’s role by writing it near the line next to the class. In association between a team and
a player, a team is usually and employer and the player is employee.
You can imagine an association that you could read in the other direction: A team employs
players. You can show both associations in the same diagram, with a filled triangle indicating how
to read each association.
Multiplicity:
Multiplicity is an important aspect of associations among objects. It tells the number of
objects in one class that relate to a single object of the associated class. For example, in a typical
college course, the course is taught by a single instructor. The course and the instructor are in a one-
to-one association.
In UML, a variety of multiplicities are possible. One class can relate to another class in
different ways as following:
1-to-1
1-to-many
many-to-many
1-to-“1 or more” (also called “Mandatory” relationship)
1-to-“0 or one” (also called “Optional” relationship)
1-to-a bounded interval (e.g. 1-to-5 through 10)
1-to-exactly ‘n’
1-to-a set of choices (e.g. 1-to-“9 or 10”)
UML uses an asterisk (*) to represent more/many. In one context ‘or’ is represented as two
dots, for example 1..* (“one or more”). In other context, “or” is represented by a comma (,) e.g. 5,10
(“5 or 10”).
This is a complete example of representing association in both ways. A Professor can write zero or many
Books and a Book is written by at least one author. There can be more than one authors for a Book.
A conceptual model can be defined as a model which is made of concepts and their
relationships. A conceptual model is the first step before drawing a UML diagram. It helps to
understand the entities in the real world and how they interact with each other.
In particular, relationships in class diagrams include different types of logical connections. The
following are such types of logical connections that are possible in UML:
Association
Directed Association
Reflexive Association
Multiplicity
Inheritance/Generalization
Aggregation
Composition
Realization
Associations:
When classes are connected together conceptually, that connection is called an association.
Association can be uni-directional or bi-directional.
Associations in UML:
In Lab 02, we have learnt how to create Associations between different classes and how to
Multiplicity:
Multiplicity tells the number of objects in one class that relate to a single object of the
associated class. In UML, a variety of multiplicities are possible. One class can relate to another
class in different ways as following:
1-to-1, 1-to-many, many-to-many, 1-to-“1 or more” (also called “Mandatory” relationship),
1-to-“0 or one” (also called “Optional” relationship), 1-to-a bounded interval (e.g. 1-to-5
through 10), 1-to-exactly ‘n’, 1-to-a set of choices (e.g. 1-to-“9 or 10”).
Multiplicity Meanings
1 Exactly one instance
* Many instances
5 Exactly 5 instances
m,n Exactly m or n
Qualified Associations:
When you make a reservation at a hotel, the hotel assigns you a confirmation number. If you call
with questions about the reservation, you have to supply the confirmation number, so that someone
looking through the reservation list can select your reservation.
In the UML, the ID information is called a qualifier. Its symbol is a small rectangle
adjoining the class that has “one” part in the one-to-any multiplicity. Although the reservation
between ReservationList and Reservation is one-to-many, the multiplicity between
confirmationNumber and Reservation is one-to-one.
Reflexive Associations:
Sometimes, a class is in an association with itself. Referred to as a reflexive association, this can
happen when a class has objects that play a variety of roles. For example, a CarOccupant can be
either a driver or a passenger. In the role of the driver, one CarOccupant drives zero or more
additional CarOccupants who play the role of passenger.
You represent this by drawing an association line from the class rectangle back to the same
class rectangle, and on the association line you indicate the roles, name of the association, direction
of the association, and multiplicity as before.
Associations may be more complex than just one class connected to another. Several classes can
connect to one class. If you consider guards, forwards, and centers, and their associations with
the Team class, you'll have the diagram in figure below.
Constraints on Associations
Sometimes an association between two classes has to follow a rule. You indicate that rule by
putting a constraint near the association line. For example, a Bank Teller serves a Customer, but
each Customer is served in the order in which he or she appears in line. You capture this in the
model by putting the word ordered inside curly brackets (to indicate the constraint) near the
Customer class, as in figure.
Another type of constraint is the Or relationship, signified by {or} on a dashed line that connects
two association lines. Figure models a high school student choosing either an academic course of
study or a commercial one.
Object-orientation refers to this as inheritance. The UML also refers to this as generalization. One
class (the child class or subclass) inherits attributes and operations from another (the parent class or
superclass). The parent class is more general than the child class.
The inheritance hierarchy doesn't have to end at two levels: A child class can be a parent class for
still another child class. Mammal is a child class of Animal, and Horse is a child class of Mammal.
In the UML, you represent inheritance with a line that connects the parent class to the child
class. On the part of the line that connects to the parent class, you put an open triangle that points to
the parent class. This type of connection stands for the phrase is a kind of. A Mammal is a kind of
Animal, and a Horse is a kind of Mammal.
Dependencies:
In a different kind of relationship, one class uses another. This is called a dependency. The most
common usage of a dependency is to show that the signature of one class's operation uses another
class.
Suppose you're designing a system that displays corporate forms on-screen so employees can
fill them out. The employee uses a menu to select the form to fill out. In your design, you have a
System class and a Form class. Among its many operations, the System class has
displayForm(f:Form). The form the system displays obviously depends on which form the user
selects. The UML notation for this is a dashed line with an arrowhead pointing at the class depended
on.
This figure below shows how to represent a dependency in UML. The class Form depends
on the method displayForm(f:Form) of class System.
In particular, relationships in class diagrams include different types of logical connections. The
following are such types of logical connections that are possible in UML:
Association
Directed Association
Reflexive Association
Multiplicity
Inheritance/Generalization
Aggregation
Composition
Realization
Association Classes
An association can have attributes and operations, just like a class. In fact, when this is the
case, you have an association class. You visualize an association class the same way you show a
regular class, and you use a dashed line to connect it to the association line. An association class can
have associations to other classes. Figure shows an association class for the Plays on association
between a player and a team. The association class, Contract is associated with the GeneralManager
class.
Link
Compositions:
A composite is a strong type of aggregation. Each component in a composite can belong to just one
whole. The components of a coffee table—the tabletop and the legs—make up a composite. The
symbol for a composite is the same as the symbol for an aggregation except the diamond is filled, as
shown in figure.
The composite is one way to show the components of a class. If you want to give the sense
of showing the class's internal structure, you can go with the UML composite structure diagram.
Here's an example. Suppose you're creating a model of a shirt. Figure below shows the shirt
as a large class rectangle with its components nested inside. The nested diagram shows how the
components of the shirt relate to one another. The composite structure diagram focuses attention on
the shirt and its internal components.
We want to design UMS i.e. University Management System. Possible entities for such a
system can be University, Department, Student, Teacher and Course. Draw class
diagrams and Object for each of these entities and connect these classes with each other
using associations/relationships.
Draw a model consisting of class diagrams (no need to write attributes or operations for
classes) and relationship between them for a Hospital Management System.
Identify different entities/classes required for a Pizza Delivery System. Draw class
diagrams, associations and object diagram between different classes if exist.
Draw class diagrams for an On-line Shopping system (a system where a visitor can visit
any online-shopping site for buying required needs).
Draw class diagrams (including attributes and operations) and qualified associations if
exist, for the following pair of entities:
o Student and Registration List
o Order and Order Line
o and Product Catalog
o Bank Account and Customer
Possible entities for a University Management System are University, Department, Student,
Teacher and Course. Identify different attributes, operations and sub-classes/parent classes
(if exist) for each of these entities. Draw class diagrams, show inheritance among different
CAED Session:-2K22 Computer Lab Instructor:-Adnan Mustafa
Engineering
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
DEPARTMENT OF COMPUTER ENGINEERING
entities/classes and object diagram. Identify associations/relationships and their multiplicities
between different classes. Remember, an association with a parent class will be inherited by
all child classes. If you want to associate a relationship with a particular child class only,
then draw association with that child class only.
For each of the following pairs of related entities, indicate whether there exists any
association class between them or not? Then, using the UML notation introduced in the lab,
draw a diagram for association among each pair.
o Customer and Product
o Student and Course
o Supplier and Item
o Driver and Car
Draw a UML class diagram for a Vehicle showing aggregation/composition/inheritance
relationship between different components.
Draw a UML inheritance diagram for a Cricket Team Management including players. You
have to identify different sub-classes/parent classes, their attributes and operations which are
required for managing a cricket team.