Experiment 2
Experiment 2
Experiment No. 2
Program 1
class FullTimeEmployee(Employee):
def __init__(self, name, employee_id, salary, bonus):
super().__init__(name, employee_id, salary)
self.bonus = bonus
def annual_salary(self):
return self.get_salary() * 12 + self.bonus
class PartTimeEmployee(Employee):
def __init__(self, name, employee_id, hourly_rate, hours_per_week):
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
Program 2
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
room = None
available =room.check_availability(room_number)
if choice ==2 :
name = input("Enter your name: ")
room_number2 = int(input("Enter room number (101 or 102): "))
if room_number2 == 101:
room = room_1
elif room_number2 == 102:
room = room_2
else:
print("Invalid room number")
continue
room.book_room(name, room_number2)
booking_date1 = input("Enter booking start date: ")
booking_date2 = input("Enter booking end date: ")
if choice == 3 :
room_number2 = int(input("Enter room number (101 or 102): "))
if room_number2 == 101:
room = room_1
elif room_number2 == 102:
room = room_2
else:
print("Invalid room number")
room = None
room.display_booking_details(booking_date1, booking_date2, name)
2
Enter your name: Disha
Enter room number (101 or 102): 102
Room 102 booked successfully by Disha
Enter booking start date: 12/08/24
Enter booking end date: 14/08/24
Program 3
class circle(shapes) :
def __init__(self,r) :
self.r = r
def calculate_area(self) :
return 3.14*self.r*self.r
class Rectangle(shapes) :
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
def __init__(self,l,b) :
self.l = l
self.b = b
def calculate_area(self) :
return self.l*self.b
class Triangle(shapes) :
def __init__(self,b,h) :
self.b = b
self.h = h
def calculate_area(self) :
return 0.5*self.h*self.b
choice=0
while(choice !=4) :
print("Enter 1 for circle ")
print("Enter 2 for rectangle")
print("Enter 3 for triangle ")
print("Enter 4 to exit ")
choice = int(input("Enter your choice:"))
if choice == 1 :
r = int (input("Enter radius :"))
c1 = circle(r)
print(f"Area of circle is : {c1.calculate_area()} ")
if choice == 2 :
l = int (input("Enter length :"))
b = int (input("Enter breadth :"))
r1 = Rectangle(l,b)
print(f"Area of rectangle is : {r1.calculate_area()} ")
if choice == 3 :
h = int (input("Enter height :"))
b = int (input("Enter base :"))
t1 = Triangle(b,h)
print(f"Area of triangle is : {t1.calculate_area()} ")
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
CONCLUSION: Program 1:
Encapsulation: The Employee class uses private attributes (__name,
__employee_id, __salary) to encapsulate data, ensuring that these attributes
are only accessible through getter methods.
Inheritance:
FullTimeEmployee inherits from Employee and adds functionality
to include a bonus in the annual salary calculation.
PartTimeEmployee also inherits from Employee, but instead of a
fixed salary, it calculates the annual salary based on an hourly rate
and weekly hours worked.
Polymorphism: Both subclasses implement an annual_salary method, but
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering
Program 2:
Abstraction:
The shapes class serves as an abstract base class with a
calculate_area method that is not implemented, enforcing that every
derived shape class must provide its own implementation of this
method.
Inheritance:
The circle, Rectangle, and Triangle classes inherit from the shapes
class. This allows them to share a common interface (calculate_area)
while implementing their specific area calculations.
Polymorphism:
The calculate_area method is defined differently in each subclass,
allowing the same method name to be used in different contexts.
This makes the code more flexible and easier to maintain or extend.
Program 3:
Encapsulation: