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

OOPS Assignment2

The document contains code to define classes for different shapes (Circle, Rectangle, Triangle) and their area calculation methods. It also defines a ShapeAreaCalculator class with a static method to calculate the area of any shape. The main code takes user input to create a shape object, calls the area method and prints the result.

Uploaded by

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

OOPS Assignment2

The document contains code to define classes for different shapes (Circle, Rectangle, Triangle) and their area calculation methods. It also defines a ShapeAreaCalculator class with a static method to calculate the area of any shape. The main code takes user input to create a shape object, calls the area method and prints the result.

Uploaded by

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

Question 1

class Hotel:

def __init__(self,name):

self.name=name

self.rooms=[]

def add_room(self,room):

self.rooms.append(room)

def remove_room(self,room):

self.rooms.remove(room)

def display_room(self):

for room in self.rooms:

print("Room Number",room.no,"Status",room.check_status())

def availble_rooms(self):

print("Rooms availble are")

for room in self.rooms:

if(room.status=="vacant"):

print("Room No",room.no,"having capacity of",room.capacity)

class Room:

def __init__(self,number,capacity):

self.no=number

self.capacity=capacity

self.status="vacant"

def check_status(self):

return self.status

def check_in(self):

if self.status=="vacant":

self.status="occupied"

else:
print("Room not vacant")

def check_out(self):

self.status="vacant"

taj=Hotel("Taj")

print(taj.name)

room1=Room(101,2)

room2=Room(102,2)

room3=Room(103,4)

room4=Room(104,1)

taj.add_room(room1)

taj.add_room(room2)

taj.add_room(room3)

taj.add_room(room4)

taj.rooms[1].check_in()

taj.rooms[1].check_out()

taj.rooms[2].check_in()

taj.availble_rooms()

Question 2

from datetime import datetime

class Airport:

def __init__(self,name):

self.__name=name

self.__flights=[]

def __find_flight_by_flightnumber(self,number):
flag=1

for flight in self.__flights:

if(flight.__number==number):

return flight

return None

def add_flight(self,flight):

self.__flights.append(flight)

def remove_flight(self,number):

flight=self.__find_flight_by_flightnumber(number)

if flight:

self.flights.remove(flight)

def upcoming_flights(self):

print("Upcoming flights")

for flight in self.__flights:

if(flight._arrival_time>=datetime.now()):

flight.displaydetails()

def completed_flights(self):

print("Completed flights")

for flight in self.__flights:

if(flight._dep_time<=datetime.now()):

flight.displaydetails()

def details_of_all_flights(self):

for flight in self.__flights:

flight.displaydetails()

class Fligt:
def __init__(self,f_no,dest,dep_time,arrival_time):

self.__number=f_no

self.__dest=dest

self._dep_time=dep_time

self._arrival_time=arrival_time

def _isupcoming(self):

if(self.__arrival_time<datetime.now()):

return True

else:

return False

def update_dep(self,new_dep):

self._dep_time=new_dep

def update_arrival(self,new_arr):

self.__arrival_time=new_arr

def displaydetails(self):

print("Fligt
Number:",self.__number,"Destination:",self.__dest,"Arrival:",self._arrival_time,"Departure:o",self._dep
_time)

Delhi=Airport("Delhi")

f1=Fligt("6E1","Pune",datetime(2023,11,15,12),datetime(2023,11,23,14))

f2=Fligt("6E2","Mumbai",datetime(2023,11,12,12),datetime(2023,11,12,14))

Delhi.add_flight(f1)

Delhi.add_flight(f2)

Delhi.details_of_all_flights()

f2.update_dep(datetime(2023,12,15))

Delhi.completed_flights()

Delhi.upcoming_flights()
Question 3

import math

class Shape():

def area(self):

pass

class Circle(Shape):

PI=math.pi

def __init__(self, radius):

self.radius = radius

def area(self):

return self.PI * self.radius**2

class Rectangle(Shape):

def __init__(self, length, width):

self.length = length

self.width = width

def area(self):

return self.length * self.width

class Triangle(Shape):

def __init__(self, base, height):

self.base = base

self.height = height
def area(self):

return 0.5 * self.base * self.height

class ShapeAreaCalculator:

@staticmethod

def calculate_area(shape):

return shape.area()

while True:

print("Choose shape:")

print("Click 1 for Circle")

print("Click 2 for Rectangle")

print("Click 3 to exit Triangle")

print("Click 4 to exir Exit")

choice = int(input("Enter your choice (1-4): "))

if choice == 1:

radius = float(input("Enter the radius of the circle: "))

circle = Circle(radius)

print("Area of the circle:", ShapeAreaCalculator.calculate_area(circle))

elif choice == 2:

length = float(input("Enter the length of the rectangle: "))

width = float(input("Enter the width of the rectangle: "))

rectangle = Rectangle(length, width)

print("Area of the rectangle:", ShapeAreaCalculator.calculate_area(rectangle))


elif choice == 3:

base = float(input("Enter the base of the triangle: "))

height = float(input("Enter the height of the triangle: "))

triangle = Triangle(base, height)

print("Area of the triangle",ShapeAreaCalculator.calculate_area(triangle))

elif choice == 4:

break

else:

print("Invalid choice. Please enter a number between 1 and 4.")

You might also like