0% found this document useful (0 votes)
50 views6 pages

15CSE337 Foundation of IT: Name: Gokula Krishnaa P Date: 04/03/2022 Roll No: CB - EN.U4EIE18017

1. The document discusses creating classes for distance, complex numbers, and time in Python using operator overloading. It includes code examples to compare distances, add complex numbers, and add time objects. 2. A monthly expense class is described that tracks household, EMI, and education expenses for May and June. The class initializes expenses from user input. 3. A menu-driven program is outlined that uses the monthly expense class to compare total expenses between months using operators, and add the total expenses for May and June. Sample inputs and outputs are provided.

Uploaded by

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

15CSE337 Foundation of IT: Name: Gokula Krishnaa P Date: 04/03/2022 Roll No: CB - EN.U4EIE18017

1. The document discusses creating classes for distance, complex numbers, and time in Python using operator overloading. It includes code examples to compare distances, add complex numbers, and add time objects. 2. A monthly expense class is described that tracks household, EMI, and education expenses for May and June. The class initializes expenses from user input. 3. A menu-driven program is outlined that uses the monthly expense class to compare total expenses between months using operators, and add the total expenses for May and June. Sample inputs and outputs are provided.

Uploaded by

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

15CSE337

Foundation of IT
Name: Gokula Krishnaa P Date: 04/03/2022
Roll no: CB.EN.U4EIE18017

1. Create a class distance with feet as data member.


Declare and initialize two objects d1 and d2 of type distance.
Write a python program to compare two distance objects d1 and d2.
[Hint: If feet of d1 is greater than feet of d2, then we can say d1 is greater
than d2, else d2 is greater].

Code:
class feet:
def __init__(self, d):
self.d = d

def __gt__(self, other):


if(self.d>other.d):
return True
else:
return False
d1 = feet(2)
d2 = feet(3)
if(d1>d2):
print("d1 is greater than d2")
else:
print("d2 is greater than d1")

Output:

2. Write a python program to perform complex number addition and


subtraction using operator overloading.

first complex number : 2 + i 3


second complex number: 3+ i4
final complex number: 5+ i7

Code:
class complex:
def __init__(self,a,b,i):
self.x = a
self.y = b
self.i = i
def display(self):
print("complex number-{} is {}+i{}".format(self.i,self.x,self.y))

def __add__(self,other):
return self.x+other.x,self.y+other.y

i=int(1)
c1 = complex(2,3,i)
i+=1
c2 = complex(4,5,i)
c1.display()
c2.display()
c3 = c1+c2
print("complex number-3 is {}+i{}".format(c3[0],c3[1]))
Output:

3. OPERATOR OVERLOADING - ADDING TWO 'TIME' OBJECTS

Create a class called 'times' that has three integer data members for hours,
minutes and seconds,
define a member function to read the values, member operator function to
add time, member function to display time in HH:MM:SS format.
Write a main function to create two time objects, use operator function to
add them and display the results in HH:MM:SS format.
Input Format:
First 3 values of the sample input represents the hr, min, sec values of time 1,
next 3 values represents the hr, min, sec values of time 2.
Output Format:
Display the resultant time in HH:MM:SS format.
Sample Input:
time1=3:44:12
time2=2:12:13
time3=time1+time2 = 5:56:25
print(time1+time2)
Code:
class times:
def readvalues(self,i):
self.i = i
self.h = int(input("Enter hours for time{}:".format(self.i)))
self.m = int(input("Enter minutes for time{}:".format(self.i)))
self.s = int(input("Enter seconds for time{}:".format(self.i)))
def display(self):
print("time{}={}:{}:{}".format(self.i,self.h,self.m,self.s))
def __add__(self,other):
return other.i+1,self.h+other.h,self.m+other.m,self.s+other.s

i = int(1)
t1 = times()
t1.readvalues(i)
i+=1
t2 = times()
t2.readvalues(i)
t3=t1+t2
t3=list(t3)
while(t3[3]>60):
t3[2]+=1
t3[3]-=60
while(t3[2]>60):
t3[2]-=60
t3[1]+=1
print("time{}={}:{}:{}".format(t3[0],t3[1],t3[2],t3[3]))
Output:

4. Monthly Expense

Aim:

Create a class for tracking Monthly Expenses.


Your monthly expense class keeps track of 3 different types of expenses
(such as household expenses, emi expenses, education expenses).
Create two objects of this class for month of May and June.

__init__() method initialize 3 types of expense values from user.

Write a Python Code for the following operations as a menu-driven program.

Option1: To check the total expenses in May Month is greater than June or
not. { Possible Outputs: TRUE/FALSE } -gt

Option2: To check the total expenses in May Month is lesser than June or
not. { Possible Outputs: TRUE/FALSE } -lt
Option3: To check the total expenses in May and June is equal or not.
{ Possible Outputs: TRUE/FALSE } -eq

Option4: To add and print the total expenses in the month of May and June
Month.
Main Program Template is given.

May=Monthlyexpense()
June=Monthlyexpense()

choice=int(input())
if (choice==1):
May>June
elif (choice==2):
May<June
elif (choice==3):
May==June
elif (choice==4):
May+June
...........................

case1:
Sample Input:
details for May month:
household exp:2000
emi exp: 3200
education expense: 500
details for June month:
household exp:1800
emi exp: 3200
education expense: 1000
option: 1
Sample Output:
FALSE
....................
case2:
Sample Input:
details for May month:
household exp:2000
emi exp: 3200
education expense:500
details for June month:
household exp:1800
emi exp: 3200
education expense:1000
option : 4
Sample Output:
11700

You might also like