Professional Documents
Culture Documents
En_no : 21162171012
Subject : FP
PRACTICAL-8
CODE :
#8_1
class Bank:
def __init__(self,name,account_type,balance):
self.name = name
self.account_type = account_type
self.balance = balance
def withdraw(self,amount):
self.balance -= amount
def deposit(self,amount):
self.balance += amount
def setbalance(self,balance):
self.balance = balance
def getbalance(self):
return self.balance
def __str__(self):
b = Bank("John","Saving",1000)
b.withdraw(100)
print(b)
b.deposit(100)
print(b)
b.setbalance(1000)
print(b)
print(b.getbalance())
OUTPUT :
2. Design a class named StopWatch. The class contains:
■ The private data fields startTime and endTime with get methods.
■ A constructor that initializes startTime with the current time.
■ A method named start() that resets the startTime to the current time.
■ A method named stop() that sets the endTime to the current time.
■ A method named getElapsedTime() that returns the elapsed time for the
stop watch in milliseconds.
Write a test program that measures the execution time of adding numbers
from 1 to 1,000,000.
CODE :
# 8_2)
import time
class StopWatch:
def __init__(self):
self.__startTime = time.time()
self.__endTime = time.time()
def start(self):
self.__startTime = time.time()
def stop(self):
self.__endTime = time.time()
def getElapsedTime(self):
return self.__endTime - self.__startTime
def getStartTime(self):
return self.__startTime
def getEndTime(self):
return self.__endTime
def main():
stopwatch = StopWatch()
stopwatch.start()
sum = 0
for i in range(1000000):
sum += i
stopwatch.stop()
main()
OUTPUT :
3. A small module for manipulation of complex numbers is being developed
for ease of research related work. Implement add() for addition, mul() for
multiplication , sub() for subtraction of two complex numbers. By default, a
complex number will be assigned an imaginary value of 3. Also ensure that
mul() cannot be ever 0 ; so program should raise an error.
CODE :
#8_3
class Complex:
def __init__(self,real,imag=3):
self.real = real
self.imag = imag
def add(self,other):
return Complex(self.real+other.real,self.imag+other.imag)
def mul(self,other):
return Complex(self.real*other.real,self.imag*other.imag)
def sub(self,other):
return Complex(self.real-other.real,self.imag-other.imag)
def __str__(self):
return "({0},{1})".format(self.real,self.imag)
c1 = Complex(2,3)
c2 = Complex(3,4)
print(c1.add(c2))
print(c1.mul(c2))
print(c1.sub(c2))
OUTPUT :