Nikhil Assignment 2
Nikhil Assignment 2
October 9, 2024
OOPS ASSINGMENT 1
NAME: NIKHIL VERMA ROLL NO: 23CH10018
Q1: A user enters time in seconds. Display it in the HH:MM:SS format.
[1]: a=int(input('enter timme in seconds:'))
x=a
h=x//3600
x=x%3600
m=x//60
s=x%60
l=[h,m,s]
for i in range(0,len(l)):
if(len(str(l[i]))==1):
l[i]='0'+str(l[i])
print('Time in HH:MM:SS format = ',end='')
for i in range(0,len(l)):
print(l[i],end='')
if(i!=len(l)-1):
print(':',end='')
1
ct=ct+1
print('value of sin(x) using the series = ',s)
print('number of terms of series required to compute the value of sin(x) so␣
↪that the error is less than 0.02 =',ct)
enter a1: 2
enter b1: 3
enter c1: 5
enter a2: 1
enter b2: 4
enter c2: 8
x = 0.800
y = -2.200
Q4: Write an example each to demonstrate the encapsulation,inheritance, and polymorphism con-
cepts
[17]: #encapsulation
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.__balance = balance
2
def withdraw(self, amount):
if 0 < amount <= self.__balance:
self.__balance -= amount
print(f'Withdrew:{amount}')
else:
print('Insufficient funds or invalid amount.')
def get_balance(self):
return self.__balance
Deposited:50
Withdrew:30
Balance: 120
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
dog = Dog()
cat = Cat()
print(dog.speak())
print(cat.speak())
Woof!
Meow!
Q5: Write Python classes to model items in a Stationary shop. Create a ShoppingCart class which
can keep the items selected by the customer along with their quantities. Write a test driver to
handle a customer transaction.
[31]: class stationary_items:
def __init__(self,quantity,price):
self.quantity=quantity
3
self.price=price
def buy(self,quantity):
if(self.quantity !=0):
self.quantity=self.quantity-quantity
return self.price*quantity
else:
print(self.name,'out of stock!!')
return None
def add_stock(self,quantity):
self.quantity=self.quantity+quantity
def upadte_price(self,newprice):
self.price = newprice
def current_stock(self):
return self.quantity
class shopping_cart:
def __init__(self,l):
self.value=0
for i in l:
self.value=self.value+i[0].buy(i[1])
def bill(self):
return self.value
pencil=stationary_items(100,10)
book=stationary_items(100,90)
copy=stationary_items(100,50)
pen=stationary_items(100,15)
eraser=stationary_items(100,5)
shapner=stationary_items(100,5)
cart1=shopping_cart([[pencil,2],[pen,1],[book,1]])
print('bill =',cart1.bill())
bill = 125