Lab 05 OOP
Lab 05 OOP
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
Tools/Software Requirement
• NetBeans IDE (Version 8.2 or higher)
• Java Development Kit (JDK Version 8 or higher)
Description
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.
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.
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):
- studentID: int
- studentName: String
è - studentGPA: float constructors and methods
• 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.
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
[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:
[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
Student Advisor
[0921210]: [Object
Oriented
Programming
1]
10
King Faisal University
College of Computer Sciences and Information Technology
Page|
Challenge Yourself!
[0921210]: [Object
Oriented
Programming
1]
11
King Faisal University
College of Computer Sciences and Information Technology
Page|
[0921210]: [Object
Oriented
Programming
1]
12