0% found this document useful (0 votes)
16 views4 pages

Nikhil Assignment 2

Uploaded by

nikhilverma
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)
16 views4 pages

Nikhil Assignment 2

Uploaded by

nikhilverma
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/ 4

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='')

enter timme in seconds: 4800


Time in HH:MM:SS format = 01:20:00
Q2: calculate the value of sin(x) using the series expansion of sin(x).

[3]: import math


def factorial(n):
p=1
for i in range(1,n+1):
p=p*i
return p
x=float(input('enter the value of x:'))
ct=0
s=0
i=1
while(abs(math.sin(x)-s)>0.02):
s=s+((-1)**ct)*((x**i)/factorial(i))
i=i+2

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 the value of x: 8


value of sin(x) using the series = 0.9871283394876361
number of terms of series required to compute the value of sin(x) so that the
error is less than 0.02 = 12
Q3: Write a Python program to solve two variable linear equations
[11]: a1=int(input('enter a1:'))
b1=int(input('enter b1:'))
c1=int(input('enter c1:'))
a2=int(input('enter a2:'))
b2=int(input('enter b2:'))
c2=int(input('enter c2:'))
if((a1*b2-a2*b1) != 0):
print('x =','%.3f'%((b1*c2-b2*c1)/(a1*b2-a2*b1)))
print('y =','%.3f'%((c1*a2-c2*a1)/(a1*b2-a2*b1)))
else:
print('no solution')

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

def deposit(self, amount):


if amount > 0:
self.__balance += amount
print(f'Deposited:{amount}')
else:
print('Deposit amount must be positive.')

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

account = BankAccount("Aditya", 100)


account.deposit(50)
account.withdraw(30)
print('Balance:',account.get_balance())

Deposited:50
Withdrew:30
Balance: 120

[19]: #inheritance and polymorphism


class Animal:
def speak(self):
return "Animal speaks"

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

You might also like