0% found this document useful (0 votes)
4 views10 pages

Experiment 2

The document outlines three programming experiments focused on Object-Oriented Programming concepts, including classes, inheritance, encapsulation, and polymorphism. Experiment 1 involves an Employee Management system with full-time and part-time employee classes, Experiment 2 implements a Hotel Booking System with room management, and Experiment 3 calculates areas of different shapes using a base class. Each experiment includes problem statements, program code, and results demonstrating the functionality of the implemented systems.

Uploaded by

Disha Deshmukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Experiment 2

The document outlines three programming experiments focused on Object-Oriented Programming concepts, including classes, inheritance, encapsulation, and polymorphism. Experiment 1 involves an Employee Management system with full-time and part-time employee classes, Experiment 2 implements a Hotel Booking System with room management, and Experiment 3 calculates areas of different shapes using a base class. Each experiment includes problem statements, program code, and results demonstrating the functionality of the implemented systems.

Uploaded by

Disha Deshmukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

BHARATIYA VIDYA BHAVAN’S

SARDAR PATEL INSTITUTE OF TECHNOLOGY


Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

Name Disha Hemant Deshmukh

UID no. 2023300044

Experiment No. 2

AIM: Implementation of classes and objects (OOPs).

Program 1

PROBLEM Employee Management Question:


STATEMENT : Design a base class Employee with private attributes for name,
employee_id, and salary. Create subclasses FullTimeEmployee and
PartTimeEmployee that inherit from Employee and add their respective
attributes and methods. Include a method to calculate annual salary in both
subclasses and ensure proper access to private variables.

PROGRAM: class Employee :


def __init__(self,name,employee_id,salary):
self.__name = name
self.__employee_id = employee_id
self.__salary = salary
def get_name(self):
return self.__name
def get_employee_id(self):
return self.__employee_id
def get_salary(self):
return self.__salary

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

super().__init__(name, employee_id, salary=0)


self.hourly_rate = hourly_rate
self.hours_per_week = hours_per_week
def annual_salary(self):
return self.hourly_rate * self.hours_per_week * 52

name = input("Enter your name : ")


employee_id= input("Enter your employee id : ")
choice = int(input("Enter 1 for full-time employee and 2 for part time
employee : "))
if choice == 1 :
salary = int(input("Enter your salary : "))
bonus = 3000
emp_1 = FullTimeEmployee(name,employee_id,salary,bonus)

print(f"Your Annual Salary(with bonus of Rs.3000) is :


{emp_1.annual_salary()}")
if choice == 2 :
hourly_rate = int(input("Enter your hourly rate : "))
hourly_per_week = int(input("Enter the no.of hours you work in a week :
"))
emp_1 =
PartTimeEmployee(name,employee_id,hourly_rate,hourly_per_week)
print(f" Your Annual salary is : {emp_1.annual_salary()}")

RESULT: Enter your name : disha


Enter your employee id : 3456
Enter 1 for full-time employee and 2 for part time employee : 1
Enter your salary : 20000
Your Annual Salary(with bonus of Rs.3000) is : 243000

Enter your name : chandan


Enter your employee id : 1234
Enter 1 for full-time employee and 2 for part time employee : 2
Enter your hourly rate : 100
Enter the no.of hours you work in a week : 35
Your Annual salary is : 182000

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

PROBLEM Hotel Booking System :


STATEMENT : Implement a class Room with attributes for room_number, room_type, and
price_per_night. Include methods to check availability, book a room, and
display booking details. Simulate room bookings with multiple instances of
Room.

PROGRAM: class Room:


isAvailable1 = True
isAvailable2 = True
isAvailable3 = True

def __init__(self, room_number, room_type, price_per_night):


self.room_number = room_number
self.room_type = room_type
self.price_per_night = price_per_night

def check_availability(self, choice):


if choice == 101:
if Room.isAvailable1:
print("Room 101 is available")
return True
else:
print("Room 101 is booked")
return False
elif choice == 102:
if Room.isAvailable2:
print("Room 102 is available")
return True
else:
print("Room 102 is booked")
return False

def book_room(self, name, choice):


if choice == 101:
if Room.isAvailable1:
print(f"Room 101 booked successfully by {name}")
Room.isAvailable1 = False
else:
print("Room 101 is already booked")
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

elif choice == 102:


if Room.isAvailable2:
print(f"Room 102 booked successfully by {name}")
Room.isAvailable2 = False
else:
print("Room 102 is already booked")

def display_booking_details(self, booking_date1, booking_date2, name):


status = "Available"
if self.room_number == 101 and not Room.isAvailable1:
status = f"Booked by {name}"
elif self.room_number == 102 and not Room.isAvailable2:
status = f"Booked by {name}"

print(f"Room number: {self.room_number}")


print(f"Room type: {self.room_type}")
print(f"Price per night: {self.price_per_night}")
print(f"Status: {status}")
if status != "Available":
print(f"Booking dates: {booking_date1} to {booking_date2}")

room_1 = Room(101, 'Single', 2000)


room_2 = Room(102, 'Double', 3000)
choice=0
while(choice !=4) :
print("Enter 1 to check availablility ")
print("Enter 2 to book a room")
print("Enter 3 to see booking details ")
print("Enter 4 to exit ")
choice = int(input())
if choice ==1 :
room_number = int(input("Enter room number (101 or 102): "))
if room_number == 101:
room = room_1
elif room_number == 102:
room = room_2
else:
print("Invalid room number")
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)

RESULT: Enter 1 to check availablility


Enter 2 to book a room
Enter 3 to see booking details
Enter 4 to exit
1
Enter room number (101 or 102): 101
Room 101 is available

Enter 1 to check availablility


Enter 2 to book a room
Enter 3 to see booking details
Enter 4 to exit
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

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

Enter 1 to check availablility


Enter 2 to book a room
Enter 3 to see booking details
Enter 4 to exit
3
Enter room number (101 or 102): 101
Room number: 101
Room type: Single
Price per night: 2000
Status: Available

Enter 1 to check availablility


Enter 2 to book a room
Enter 3 to see booking details
Enter 4 to exit
4
PS C:\Users\disha deshmukh>

Program 3

PROBLEM Shape Area Calculation :


STATEMENT: Design a base class Shape with a method calculate_area(). Derive classes
Circle, Rectangle, and Triangle from Shape. Each derived class should
implement its own version of calculate_area().

PROGRAM: class shapes :


def calculate_area(self) :
pass

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

RESULT: Enter 1 for circle


Enter 2 for rectangle
Enter 3 for triangle
Enter 4 to exit
Enter your choice:1
Enter radius :10
Area of circle is : 314.0

Enter 1 for circle


Enter 2 for rectangle
Enter 3 for triangle
Enter 4 to exit
Enter your choice:2
Enter length :20
Enter breadth :30
Area of rectangle is : 600

Enter 1 for circle


Enter 2 for rectangle
Enter 3 for triangle
Enter 4 to exit
Enter your choice:3
Enter height :6
Enter base :3
Area of triangle is : 9.0

Enter 1 for circle


Enter 2 for rectangle
Enter 3 for triangle
Enter 4 to exit
Enter your choice:4

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

each does so according to its specific requirements, demonstrating


polymorphism.

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:

 The Room class encapsulates room attributes


(room_number, room_type, price_per_night) and methods
(check_availability, book_room, display_booking_details)
within itself.
 Availability status is managed using class-level variables
(isAvailable1, isAvailable2), controlling access to the room’s
availability status internally.
Inheritance:
 Though not explicitly used in this code, if expanded,
inheritance could be used to create different types of rooms
(e.g., SuiteRoom extending Room) that inherit common
functionality while adding specific attributes or methods.
Polymorphism:
 Polymorphism is indirectly applied in how the
check_availability, book_room, and display_booking_details
methods are used. The methods have different
implementations based on room availability but are accessed
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

through a common interface (the Room class).


Abstraction:
 The Room class abstracts the details of room management.
Users interact with high-level methods (check_availability,
book_room, display_booking_details) without needing to
understand the underlying details of how room availability is
tracked.

You might also like