0% found this document useful (0 votes)
24 views12 pages

Lab 05 OOP

Uploaded by

Hossam Bakr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views12 pages

Lab 05 OOP

Uploaded by

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

King Faisal University

College of Computer Sciences and Information Technology

Computer Science Department

[0921210]: [Object Oriented Programming 1]

Lab [05]: [Writing code as modeled in UML Class Diagrams]

Lab [05]: [Writing code as modeled in UML Class Diagrams]

Introduction

[0921210]: [Object
Oriented
Programming
Page|
1]
1
King Faisal University
College of Computer Sciences and Information Technology

Unified Modeling Language (UML) is a modeling language that aims to provide standardized methods
to visualize the design of a system. Some of its diagrams include: profile diagram, class diagram,
component diagram, object diagram, activity diagram, use case diagram, and state machine diagram. In
this laboratory meeting, you will learn how to create classes by looking at a UML Class Diagram. As a
programmer, you will understand the relevance of using Class Diagrams to easily see the structure of a
class and its relationship to other classes.

Objectives

In this lab, you will learn how to:


• Draw a UML Class Diagram,
• Write java class given a UML Class Diagram

Tools/Software Requirement
• NetBeans IDE (Version 8.2 or higher)
• Java Development Kit (JDK Version 8 or higher)

Description

This laboratory includes 3 experiments:


Experiment 1: Drawing the Class diagram for the Student Class.
Experiment 2: Writing a java class given a Class Diagram (Advisor Class). Experiment
3: Linking the Student class to the Advisor class in UML.

Deliverable:
You are required to solve the assignment given to you by your lab instructor and submit it before the
deadline. Note that you have to upload your projects to the Assignments and Exercises section on
Blackboard on time.

Experiment 1: Drawing the Class Diagram for the Student Class:

In the previous lab, you learnt how to write code for a Java Class given the description of the
class in a text form. In this experiment, you will draw the class diagram for the Student class in
order to understand how the diagram is used to model a class in an Object-Oriented

[0921210]: [Object
Oriented
Programming
Page|
1]
2
King Faisal University
College of Computer Sciences and Information Technology

Programming language. The following table shows a summary of the attributes, constructor, and
methods we defined for the Student class.

Attribute Constructor Methods (public)


• studentID of integer data type There’s only one setStudentID(int) void
and has private access constructor that has 3 getStudentID() int
• studentName of String data parameters referring to
setStudentName(String) void
type and has private access the 3 attributes
• studentGPA of float data type getStudentName() String
and has private access setStudentGPA(float) void
getStudentGPA() float
displayInfo() void

To draw the class diagram of the Student class, draw a rectangle with 3 parts and fill them with
the appropriate information (class name, attributes, methods):

è Student class name  Attributes

- studentID: int
- studentName: String
è - studentGPA: float constructors and methods

+ Student (int, String, float)


+ setStudentID(int)
+ getStudentID()
+ setStudentName(String)
+ getStudentName()
+ setStudentGPA(float)
+ getStudentGPA()
Things + displayInfo() to remember about UML class diagram:
• It is divided into 3 parts: class name, attributes, and
constructors and methods.
• Private access modifier attribute/method starts with hyphen or minus sign (-), public access
modifier starts with plus sign (+), protected access modifier starts with hash, sharp or
pound sign (#), and default access modifier starts with tilde (~).
[0921210]: [Object
Oriented
Programming
Page|
1]
3
King Faisal University
College of Computer Sciences and Information Technology

• An attribute starts with its access modifier followed by its name, a colon ( : ) and its data
type.
• A constructor starts with its access modifier (though it should always be public), followed by
its name (which should also be the name of the class) and its parameter/s enclosed in rounded
brackets or parenthesis.
• A method starts with its access modifier followed by its name, its parameters enclosed in
rounded brackets, and if it’s non-void, a colon ( : ) and its return type. In the previous
example, there are some methods like the setters and the displayInfo which do not have colon
and a data type because their return type is void.

Challenge Yourself!

Draw the class diagram for the Book class given that each Book has an identifier, name, and a
price.

Experiment 2: Writing a java class given a Class Diagram (Advisor Class):

By now, you should be able to create a class by simply looking at a class diagram. Can you
describe the class diagram below:

Advisor

- advisorID: int
- advisorName: String
- advisorDept: String

+ Advisor (int, String, String)


+ setAdvisorID(int)
+ getAdvisorID()
+ setAdvisorName(String)
+ getAdvisorName()
+ setAdvisorDept(String)
+ getAdvisorDept()
+ toString()

[0921210]: [Object
Oriented
Programming
Page|
1]
4
King Faisal University
College of Computer Sciences and Information Technology

What is the name of the class? How many attributes does it have? Can you describe each? How
about the constructor and the methods? Everything should be pretty clear except for the
toString() method which will return a String that concatenates (String addition) the 3 attributes’
values. Modifying toString() method is called overriding. You will learn more of this later.

Challenge Yourself!

Try to write the code on your own based on the class diagram drawn above in the same project as
the one you used in the previous lab (StudentInformation).

To write the code as described in the previous class diagram, follow these steps:

1. Create a new class as you learnt in the previous lab (Right click on the package name >
New > Java Class.. and call the class Advisor:

2. Define the 3 attributes: advisorID, advisorName, advisorDept, as shown in the class


diagram with a private access modifier (-), and proper corresponding data types:

[0921210]: [Object
Oriented
Programming
Page|
1]
5
King Faisal University
College of Computer Sciences and Information Technology

3. Create a constructor to initialize the 3 instance variables, as you learnt from the previous
lab:

4. Create the setters for the 3 attributes using the proper data type for the parameter and with
a public accessibility modifier (+) and void return type:

5. Create the getters for the 3 attributes using the proper return type and with a public
accessibility modifier (+):

[0921210]: [Object
Oriented
Programming
Page|
1]
6
King Faisal University
College of Computer Sciences and Information Technology

6. Create the toString() method which should return a string that is the result of
concatenating all the information about the advisor. (You can add \t to give some space
between the values when printed):

7. Go to the StudentInformation class, and create 2 objects of the Advisor class with the
following details:
{11, “Maytham”, “Computer Science”} , {21, “Sara”, “Information Systems”}:

8. Print the information of the 2 advisors using the println method while calling the
toString() method on each object, as shown in the figure below:

[0921210]: [Object
Oriented
Programming
Page|
1]
7
King Faisal University
College of Computer Sciences and Information Technology

9. Run the file, and check the output. It should look like the figure below:

Challenge Yourself!

- Remove the call to the toString() method from the printing statements and check the
output again.
- Try to organize the output in a similar fashion to the one you learnt in lab 2, but
instead of using printf, use String.format() in the return statement of the toString()
method. The method format() is used to define a pattern, then a comma separated list
of arguments corresponding to the pattern’s placeholders.

[0921210]: [Object
Oriented
Programming
Page|
1]
8
King Faisal University
College of Computer Sciences and Information Technology

[0921210]: [Object
Oriented
Programming
Page|
1]
9
King Faisal University
College of Computer Sciences and Information Technology

Experiment 3: Linking the Student class to the Advisor class in UML:


Being a student at King Faisal University, you already know that every student has an academic
advisor which means that the advisor is one of the information that can be stored about a student.
One of the uses of the Class Diagram is to show the relationships between the different classes in
a system. For example, there is a relationship between the Student and the Advisor class, where
each student has an advisor. This type of relationship can be simply called association. There are
many types of association relationships but this is not our concern now. To show the association
relationship between 2 classes you simple connect the 2 classes with a link, as shown in the
figure below:

Student Advisor

- studentID: int - advisorID: int


- studentName: String - advisorName: String
- studentGPA: float - advisorDept: String
- studentAdvisor: Advisor
Has-a
+ Student (int, String, float) + Advisor (int, String, String)
+ setStudentID(int) + setAdvisorID(int)
+ getStudentID() + getAdvisorID()
+ setStudentName(String) + setAdvisorName(String)
+ getStudentName() + getAdvisorName()
+ setStudentGPA(float) + setAdvisorDept(String)
+ getStudentGPA() + getAdvisorDept()
+ displayInfo() + toString()

[0921210]: [Object
Oriented
Programming
1]
10
King Faisal University
College of Computer Sciences and Information Technology

Page|

Challenge Yourself!

- A manager for a Blood Donation Bank is seeking a development company’s


help in building a system to manage active donors’ data. Note that each
donor has an id, name, birth year, and a blood group. Your job is to:
- Draw the class diagram for the Donor class with all attributes, constructor,
and methods (setters, getters, and toString).
- Write the Java code as described in the UML diagram you have drawn.

[0921210]: [Object
Oriented
Programming
1]
11
King Faisal University
College of Computer Sciences and Information Technology

Page|

[0921210]: [Object
Oriented
Programming
1]
12

You might also like