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

Python Practical 13

Python Practical

Uploaded by

rutujapawar.1408
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)
5 views

Python Practical 13

Python Practical

Uploaded by

rutujapawar.1408
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/ 3

[] #Q.1.

Define a class Distance with constructor that accept distance in


#foot and inches.Constructor must convert inches in foot. Example
#Distance(2, 19) must convert to 3'7'".Define two methods to print() to
#print distance and meter () to return distance in meter.

class Distance:
Os

definit_(self, feet, inches) :


self. inches = inches % 12
self. feet = int((inches+feet*12) /12)
self.meters = (feet*12 + inches )* 0.0254
defprint_(self):
print("The distance is" self.feet self.inches ."!")
def meter(self):
print(" The distance in meters is" ,self . meters )
d=Distance(2, 19)
d._print_()
d. meter_)

E The distance is 3 7
The distance in meters is 1.0922
V[81 #0.2.In class defined in Q.1.overload the operator + to add two distances.
0s

V16] class Distance:


def init_(self, feet,inches) :
self.inches = inches % 12
self.feet = int((inches+feet*12)/12)
self.meters = ( feet*12 + inches )* 0.0254
def print_(self):
print("The distance is" , self. feet ", self. inches "")
def meter_(self):
print("The distance in meters is" ,self . meters )
def add_(self, other):
return self.feettother.feet, self. inchestother. inches
d1=Distance (2,19)
d1._print_0)
d1 meter0
d2=Distance (2,4)
d2._print_()
d2._meter_(O
print("The total distance is : ")
print (d1 +d2 )

The distance is 3' 7 ' '


The distance in meters is 1.0922
The distance is 2 ' 4 !
The distance in meters is 0.7111999999999999
The total distance is:
(5, 11)
Os [18] #Q.3. In class defined Q.2.overload string operator to convert distance
#into string using _str_() method.

class Distance:
Os
def init_(self,feet, inches):
self.inches = inches % 12
self.feet = int((inches+feet*1 2)/12)
self.meters = ( feet*12 + inches )* 0.0254
def print(self):
print("The distance is" , self. feet , , self. inches ")
def meter (self) :
print ("The distance in meters is" ,self. meters )
def add_(self, other):
return self.feet+other. feet, self. inchestother.inches
def_str_(self):
return self.feet, self. inches

d1=Distance (2,19)
d1. print_()
d1 meter)
d1._str_0
d2=Distance (2,4)
d2._print_(0
d2. meter_)
d2._str_)

d3=d1+d2
print(f"\nThe total distance is:{d3} ")
print("\nDistance in string format is given as: ")
d3._str

E The distance is 3 '7


The distance in meters is 1.0922
The distance is 2 ' 4 !
The distance in meters is 0.7111999999999999

The total distance is: (5, 11)

Distance in string format is given as:


'(5, 11)'

You might also like