UNIVERSITY OF CALOOCAN CITY
Caloocan, 1400 Metro Manila, Philippines
COLLEGE OF ENGINEERING
Computer Engineering
2nd Semester, School Year 2024-2025
Laboratory Activity No. 3.1
Introduction to Object-Oriented Programming
Course Code: CPE103 Program: BSCPE
Course Title: Object-Oriented Programming Date Performed: January 25, 2025
Section: BSCPE – 1A Date Submitted: January 29, 2025
Name: Monoy, Justin Rhey A. Instructor: Engr. Maria Rizette H. Sayo
1. Objective(s):
This activity aims to familiarize students with the concepts of Object-Oriented Programming
2. Intended Learning Outcomes (ILOs):
The students should be able to:
2.1 Identify the possible attributes and methods of a given object
2.2 Create a class using the Python language
2.3 Create and modify the instances and the attributes in the instance.
3. Discussion:
Object-Oriented Programming (OOP) is an approach to programming that views the world and systems
as consisting of objects that relate and interact with each other. This involves identifying the
characteristics that describe the object which are known as the Attributes of the object. Furthermore, it also
deals with identifying the possible capabilities or actions that an object is able to do which are called
Methods.
An object is simply composed of Attributes and Methods wherein Attributes are variables that hold
the information describing the object and Methods are functions which allow the object to perform its
defined capabilities/actions. A UML Class Diagram is used to formally represent the collection of
Attributes and Methods.
An example is given below considering a simple banking system.
Accounts ATM
+ account_number: int + serial_number: int
+ account_firstname: string
+ account_lastname: string
+ current_balance: float
UNIVERSITY OF CALOOCAN CITY
Caloocan, 1400 Metro Manila, Philippines
COLLEGE OF ENGINEERING
Computer Engineering
2nd Semester, School Year 2024-2025
+ address: string + deposit(account: Accounts, amount: int) + email: string + widthdraw(account:
Accounts, amount: int) + update_address(new_address: string) +
check_currentbalance(account: Accounts) + update_email(new_email: string) +
view_transactionsummary()
4. Materials and Equipment:
Desktop Computer with Anaconda Python/Python
Colab Windows Operating System
5. Procedure:
Creating Classes
1. Create a folder named OOPIntro_LastName
2.Create a Python file inside the OOPIntro_LastName folder named Accounts.py and copy the code shown
below:
UNIVERSITY OF CALOOCAN CITY
Caloocan, 1400 Metro Manila, Philippines
COLLEGE OF ENGINEERING
Computer Engineering
2nd Semester, School Year 2024-2025
3. Modify the Accounts.py and add self, before the new_address and new_email.
4. Create a new file named ATM.py and copy the code shown below:
Creating Instances of Classes
5. Create a new file named main.py and copy the code shown below:
UNIVERSITY OF CALOOCAN CITY
Caloocan, 1400 Metro Manila, Philippines
COLLEGE OF ENGINEERING
Computer Engineering
2nd Semester, School Year 2024-2025
6.
Run the main.py program and observe the output. Observe the variables names
UNIVERSITY OF CALOOCAN CITY
Caloocan, 1400 Metro Manila, Philippines
COLLEGE OF ENGINEERING
Computer Engineering
2nd Semester, School Year 2024-2025
account_firstname, account_lastname as well as other variables being used in the Account1 and
Account2. 7. Modify the main.py program and add the code underlined in red.
8. Modify the main.py program and add the code below line 38.
UNIVERSITY OF CALOOCAN CITY
Caloocan, 1400 Metro Manila, Philippines
COLLEGE OF ENGINEERING
Computer Engineering
2nd Semester, School Year 2024-2025
9. Run the main.py program.
Create the Constructor in each Class
1. Modify the Accounts.py with the following code:
Reminder: def init (): is also known as the constructor class
2. Mod
ify the main.py and change the following codes with the red line. Do not remove the other
codes in the program.
UNIVERSITY OF CALOOCAN CITY
Caloocan, 1400 Metro Manila, Philippines
COLLEGE OF ENGINEERING
Computer Engineering
2nd Semester, School Year 2024-2025
3. Run the main.py program again and run the output.
6. Supplementary Activity:
UNIVERSITY OF CALOOCAN CITY
Caloocan, 1400 Metro Manila, Philippines
COLLEGE OF ENGINEERING
Computer Engineering
2nd Semester, School Year 2024-2025
Tasks
1. Modify the ATM.py program and add the constructor function.
2. Modify the main.py program and initialize the ATM machine with any integer serial number
combination and display the serial number at the end of the program.
3. Modify the ATM.py program and add the view_transactionsummary() method. The method should
display all the transaction made in the ATM object.
Questions
1. What is a class in Object-Oriented Programming?
- class is a template definition of the methods and variables in a particular kind of object
2. Why do you think classes are being implemented in certain programs while
some are sequential(line-by-line)?
- classes are used in programs when organizing code into reusable and
manageable parts is needed, especially for larger or more complex tasks.
Sequential programming is used when a program can run step by step
without needing extra structure.
3. How is it that there are variables of the same name such account_firstname and
account_lastname that exist but have different values?
- variables with the same name, like account_firstname and account_lastname, can
exist with different values because they are stored separately in different objects,
functions, or scopes. In object-oriented programming, each object has its own
copy of the variables, so different instances can hold different values for the same
variable name.
4. Explain the constructor functions role in initializing the attributes of the class? When
does the Constructor function execute or when is the constructor function called?
- the constructor function sets up an object by giving its variables starting values
when the object is created. It runs automatically when a new object is made from the
class.
5. Explain the benefits of using Constructors over initializing the variables one by one in the
main program?
- Using a constructor saves time by automatically setting up an object's variables
when it is created, instead of writing separate lines for each variable. It also
keeps the code organized and reduces mistakes by ensuring all objects start with
the right values. This makes programs easier to manage, especially when working
with many objects
UNIVERSITY OF CALOOCAN CITY
Caloocan, 1400 Metro Manila, Philippines
COLLEGE OF ENGINEERING
Computer Engineering
2nd Semester, School Year 2024-2025
7. Conclusion:
This laboratory exercise taught me how to create and manage multiple Python files (i.e.,
account.py, atm.py, and main.py) and import them into the main file (main.py) to utilize their functionality.
Additionally, it helped improve my coding skills, particularly in applying different formulas to calculate
ATM balances. Overall, this laboratory was a valuable learning experience, providing me with useful
knowledge and hands-on practice.
8. Assessment Rubric: