Subject:
Object Oriented Programming.
Submitted To:
Ma’am Anum Liaqat.
Submitted By:
Waleed Butt (2023-BBIT-40).
TASK:
Show association between an apartment class in which maximum
four individuals can live in.
Think of apartment attributes as apartment size, address
(including apartment number, location, city and country) etc.
Give details of all individuals currently using the apartment.
Determine the age group of all individuals as given below: Age:
13-19 as teens
Age: 20-39 as Young Adults
Age: 40-59 as Middle-aged Adults
Age: 60-99 as Old Adults
Hint: You may create a list of objects and pass it in the public
function of an apartment class to see the details of individuals
currently residing in.
Python Code:
class Individual: def
__init__(self, name, age):
[Link] = name
[Link] = age
def
get_age_group(self):
if 13 <= [Link] <= 19:
return "Teen"
elif 20 <= [Link] <= 39:
return "Young Adult"
elif 40 <= [Link] <= 59:
return "Middle-aged Adult"
elif 60 <= [Link] <= 99:
return "Old Adult"
else:
return "Invalid Age"
def __str__(self):
return f"{[Link]}, Age: {[Link]}, Group:
{self.get_age_group()}"
class Apartment: def __init__(self, size,
address, max_occupants=4):
[Link] = size [Link]
= address self.max_occupants =
max_occupants [Link] =
[]
def add_occupant(self, individual):
if len([Link]) < self.max_occupants:
[Link](individual)
else:
print("Apartment is full! Cannot add more occupants.")
def show_occupants(self):
print(f"Apartment Details: {[Link]}, Size:
{[Link]}") print("Occupants:") for occupant
in [Link]:
print(occupant)
# Example usage: # Create an apartment apartment =
Apartment("1200 sqft", "123 Main St, Apt 5B, City, Country")
# Create individuals person1
= Individual("Alice", 18)
person2 = Individual("Bob",
25) person3 =
Individual("Charlie", 45)
person4 = Individual("Diane",
65)
# Add individuals to the apartment
apartment.add_occupant(person1)
apartment.add_occupant(person2)
apartment.add_occupant(person3)
apartment.add_occupant(person4) #
Display apartment details and
occupants
apartment.show_occupants()
Python Code:
class User: def
__init__(self, name):
[Link] = name
def __repr__(self):
return [Link]
class WifiRouter:
max_connections = 10 connected_users = []
@classmethod def connect(cls, user): if
len(cls.connected_users) < cls.max_connections:
cls.connected_users.append(user)
print(f'{[Link]} connected to the Wifi Router.')
else:
print(f'Connection failed: maximum limit reached')
@classmethod def
disconnect(cls, user): if
user in cls.connected_users:
cls.connected_users.remove(user)
print(f'{[Link]} disconnected from Wifi Router')
else:
print(f'{[Link]} is not connected to the Wifi Router')
@classmethod def
show_connected_users(cls):
print('connected_users are:')
for user in cls.connected_users:
print(f'-
{[Link]}') if not
cls.connected_users:
print('No users currently connected.')
user1 = User("Alice")
user2 = User("Bob")
user3 =
User("Charlie") user4
= User("Momma")
user5 =
User("Khadia") user6
= User("Zanirah")
user7 = User("Hajira")
user8 =
User("Samana") user9
= User("Amna")
user10 = User("Ali")
user11 =
User("Nooy")
[Link](user1)
[Link](user2)
[Link](user3)
[Link](user4)
[Link](user5)
[Link](user6)
[Link](user7)
[Link](user8)
WifiRouter.show_connected_users()
[Link](user9)
[Link](user10)
[Link](user11)
WifiRouter.show_connected_users()
[Link](user2)
[Link](user10)
Diamond Problem Solving
Task 01
Provide the Python implementation of the following.
The Person class has name and age as its attributes. It has an
overloaded constructor to initialize the values and appropriate
accessor and mutator methods. The Employee class has name of
the employer and wage as its attributes with an overloaded
constructor and appropriate accessor and mutator methods.
The Teacher class is inherited from Person and Employee class
with an attribute of Pay Scale of type integer. It has overloaded
constructor, appropriate accessor, mutator methods and a display
function to print the Name, Age, Name of Employer, Wage and
Pay Scale of Teacher.
Task 02
Create class A with pass. Create class B inherited from A with
pass. Create class C inherited from A with pass. Create class D
inherited from class B and C. Now do the following:
a) Make a display function in class A to print “I am in class A”.
Call this method with class D object. Identify the output
b) Make a display function in class B to print “I am in class B”.
Call this method with class D object. Identify the output.
c) Make a display function in class C to print “I am in class C”.
Call this method with class D object. Identify the output
d) Remove the function from class B and now call the method
from D object. Identify the output.
e) Put the method back in class B. Make a display function in
class D to print “I am in class D”. Call this method with class
D object. Identify the output
Task 03
Provide the Python implementation of following scenario
Solve the above diamond problem. The Power Device has
attribute PDID. While the Scanner and Printer classes has
attributes SID and PID respectively. Provide overloaded
constructors for the Power Device, Scanner and Copier Classes.
Create only copier class object and pass values to the constructor.
Make explicit calls to parent functions wherever necessary.
Python Code:
Task 01: Inheritance with Overloaded Constructors and
Methods: class Person:
def __init__(self, name="", age=0):
[Link] = name
[Link] = age
def set_name(self, name):
[Link] = name
def get_name(self):
return [Link]
def set_age(self, age):
[Link] = age
def get_age(self):
return [Link]
class Employee: def __init__(self,
employer_name="", wage=0):
self.employer_name = employer_name
[Link] = wage
def set_employer_name(self, employer_name):
self.employer_name = employer_name
def get_employer_name(self):
return self.employer_name
def set_wage(self, wage):
[Link] = wage
def get_wage(self):
return [Link]
class Teacher(Person, Employee):
def __init__(self, name="", age=0, employer_name="",
wage=0, pay_scale=0):
Person.__init__(self, name, age)
Employee.__init__(self, employer_name, wage)
self.pay_scale = pay_scale
def set_pay_scale(self, pay_scale):
self.pay_scale = pay_scale
def get_pay_scale(self):
return self.pay_scale
def display(self):
print(f"Name: {[Link]}, Age: {[Link]}, Employer:
{self.employer_name}, Wage: {[Link]}, Pay Scale:
{self.pay_scale}")
# Example usage:
teacher = Teacher("John", 35, "ABC School", 50000, 4)
[Link]()
Task 02: Diamond Problem and Method Resolution Order
(MRO):
class A: def
display(self):
print("I am in class A")
class B(A): def
display(self):
print("I am in class B")
class C(A): def
display(self):
print("I am in class C")
class D(B, C):
def display(self):
print("I am in class D")
# Example usage:
d = D()
[Link]() # MRO will determine which display method is called
Task 03: Solving the Diamond Problem with Explicit Calls:
class PowerDevice:
def __init__(self, pdid):
[Link] = pdid
class
Scanner(PowerDevice):
def __init__(self, pdid, sid):
super().__init__(pdid)
[Link] = sid class
Printer(PowerDevice):
def __init__(self, pdid, pid):
super().__init__(pdid)
[Link] = pid
class Copier(Scanner, Printer):
def __init__(self, pdid, sid, pid):
Scanner.__init__(self, pdid, sid)
Printer.__init__(self, pdid, pid)
def display(self):
print(f"PDID: {[Link]}, SID: {[Link]}, PID: {[Link]}")
# Example usage:
copier = Copier(101, 202, 303)
[Link]()
UML Task:
Draw a UML Class Diagram representing the
following elements from the problem domain for a
Book Store. Book Store contain two Categories of
book i.e. Comic books and Mystery books.
Each book has name and price. Each book is
written by Author. Customer can buy books.
Customer and author both are persons. Person has
name and age. Bookstore has name. Bookstore has
workers. Workers are of two types of workers: Full
time and Part time workers. Workers has name,
working hour and salary as an attribute. Customer
has address. Address has street, city and country.
Workers can work in another place other than
bookstore.
Python Code:
# Base class Person with common
attributes class Person: def
__init__(self, name, age):
[Link] = name
[Link] = age
# Author class inheriting from
Person class Author(Person):
def __init__(self, name, age):
super().__init__(name, age)
# Customer class inheriting from
Person class Customer(Person):
def __init__(self, name, age,
address):
super().__init__(name, age)
[Link] = address
# Address class for storing address
information class Address: def
__init__(self, street, city, country):
[Link] = street
[Link] = city [Link] =
country # Base class for different
types of Books class Book:
def __init__(self, name, price, author):
[Link] = name
[Link] = price
[Link] = author #
ComicBook class inheriting
from Book class
ComicBook(Book): def
__init__(self, name, price,
author):
super().__init__(name, price,
author)
# MysteryBook class inheriting from
Book class MysteryBook(Book): def
__init__(self, name, price, author):
super().__init__(name, price, author
# Worker class inheriting from Person class
Worker(Person): def __init__(self, name, age,
working_hours, salary):
super().__init__(name, age)
self.working_hours = working_hours
[Link] = salary
# FullTimeWorker class inheriting from Worker
class FullTimeWorker(Worker): def __init__(self,
name, age, working_hours, salary):
super().__init__(name, age, working_hours, salary)
# PartTimeWorker class inheriting from Worker
class PartTimeWorker(Worker): def __init__(self,
name, age, working_hours, salary):
super().__init__(name, age, working_hours, salary)
# BookStore class with its attributes and associated
classes class BookStore: def __init__(self, name):
[Link] = name
[Link] = []
[Link] = [] def
add_book(self, book):
[Link](book)
def add_worker(self, worker):
[Link](worker)
def display_books(self):
for book in [Link]:
print(f"Book Name: {[Link]}, Price: {[Link]},
Author: {[Link]}")
def display_workers(self):
for worker in [Link]:
print(f"Worker Name: {[Link]}, Age: {[Link]},
Salary: {[Link]}, Working Hours: {worker.working_hours}")
# Example usage:
# Creating authors and books author1 = Author("John
Doe", 40) comic_book = ComicBook("Comic
Adventures", 300, author1)
# Creating customers and their addresses
address = Address("123 Main Street", "New
York", "USA") customer = Customer("Alice", 25,
address)
# Creating workers worker1 =
FullTimeWorker("Bob", 30, 40, 50000) worker2
= PartTimeWorker("Charlie", 22, 20, 20000)
# Creating bookstore and adding books and workers
bookstore = BookStore("Dreamland Books")
bookstore.add_book(comic_book)
bookstore.add_worker(worker1)
bookstore.add_worker(worker2)
# Displaying details
bookstore.display_books()
bookstore.display_workers()