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

Lab Python 03

The document outlines four exercises for a Python programming lab focused on object-oriented programming concepts. Exercises include creating classes for animals and dogs, defining a person and student class with course registration, developing a personnel management system with a class hierarchy, and modeling a banking system using encapsulation and inheritance. Each exercise emphasizes class attributes, methods, inheritance, and encapsulation principles.

Uploaded by

rorosam867
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab Python 03

The document outlines four exercises for a Python programming lab focused on object-oriented programming concepts. Exercises include creating classes for animals and dogs, defining a person and student class with course registration, developing a personnel management system with a class hierarchy, and modeling a banking system using encapsulation and inheritance. Each exercise emphasizes class attributes, methods, inheritance, and encapsulation principles.

Uploaded by

rorosam867
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Advanced Programming (CSC 122)

Lab Python #03


Exercise #01
Write a python program defines two classes, Animal and Dog, The Animal class has
a constructor that ini alizes the name a ribute and sets age to 0. It also has
methods eat() and sleep() which print out the animal's ac ons for example, {Animal
name} is ea ng and {Animal name} is sleeping in order. The Dog class inherits from
the Animal class. It has its constructor that ini alizes the name and breed a ributes.
Addi onally, it has a method bark() which prints out the dog's ac on for example
{Animal name} is barking.
An instance of the Dog class named dog is created with the name "Buddy" and
breed "Labrador". Name , breed and age a ributes are printed and a call to the
methods of the dog object are accessed.
Exercise #02
Write a python code that defines a Person class with a private a ribute for the
person's name and a method to retrieve the name. Building upon this, a Student
class inherits from Person and adds a private a ribute for the registered courses list
& student's ID number along with func onality to enroll in courses (accept course
name and add it to the list of courses the student register) and display relevant
informa on (print Name & ID of the students and the registered courses). The
Course class contains course details such as code and name.
 Two student’ objects are ini alized with data and two courses are ini alized.
 The first student is assigned two courses, and the second student is assigned
two courses
Print the details of the two students with registered courses.
Exercise #03
A university is developing a personnel management system that organizes
individuals into different categories using a class hierarchy with encapsula on. The
Person class stores private a ributes such as name, address, phone number, and
email, with a public method to retrieve this informa on. The Student class inherits
from Person and adds a private a ribute for student status (Freshman, Sophomore,
Junior, or Senior). The Employee class, also inheri ng from Person, introduces
private a ributes for office loca on, salary, and hiring date, represented using a
separate MyDate class with private a ributes for year, month, and day. Further
specializa on includes the Faculty class, which extends Employee by adding office
hours and rank, and the Staff class, which introduces a tle a ribute. Each class
provides a method to return a forma ed string of its details, ensuring data
encapsula on. Addi onally, upon ini aliza on, each object displays a message
indica ng its crea on. Students are required to define the class hierarchy,
implement the methods to retrieve object details, create instances, and explain
how encapsula on is applied in this design.

Exercise #04
Design a Python program that models a banking system using encapsula on and
inheritance. Define a class Account with private a ributes id, balance, and annual
rate. The class includes methods such as deposit(amount) and withdraw(amount)
to modify the balance. Two subclasses, SavingsAccount and CheckingAccount,
inherit from Account. SavingsAccount restricts withdrawals if the balance falls
below a minimum threshold, while CheckingAccount allows overdra s up to a
predefined limit. Outside the class defini ons, create an Account object with an ID
of 1122, a balance of $20,000, and an interest rate of 4.5%. Perform a withdrawal
of $2,500, a deposit of $3,000, and print the final balance.

You might also like