Need Help for the python: Classes and Operator Overloading* Constraints: Do not
ID: 3779453 • Letter: N
Question
Need Help for the python:
Classes and Operator Overloading*
Constraints: Do not import/use any library modules .
Design and implement a Class named FootMeasure that stores a linear measurement of feet and inches. Your class should have the following function header for the definition of the special method
__init__:
def (self, feet=0, inches=0):
a) The class should be able to create a FootMeasure object in various ways by using the optional key-word arguments. For example:
meas1 = FootMeasure()
meas2 = FootMeasure(feet = 5)
meas3 = FootMeasure(feet = 5, inches = 8)
meas4 = FootMeasure(inches = 68)
b) Implement the special method __repr__ in the class so that the measurements are displayed as follows:
>>> meas2
5 ft.
>>> meas4
5 ft. 8 in.
Note, if the value of inches is zero no value is displayed. Conversely, if an instance of FootMeasure holds a value where inches are less than 12, and the value of feet is zero, only the value for inches is displayed. Finally, when the measurement is 0, it should be displayed as: 0 ft. 0 inches. Note also, that all values, feet and inches, are stored as integers.
c) Implement the special methods __add__ for adding FootMeasure objects and __sub__ for subtracting FootMeasure objects. For example:
>>> meas5 = meas2 + meas3
>>> meas5
10 ft. 8 in.
>>> meas3 + meas4
11 ft. 4 in.
>>> meas3 – meas4
0 ft. 0 in.
>>> meas6 = meas2 – meas3
Error: subtraction results in a negative value of -8 inches
>>> meas6
Traceback (most recent call last):
File "", line 1, in meas6
NameError: name 'meas6' is not defined
d) Implement the relational operators: < (less than), > (greater than), and == (equivalence) for the class FootMeasure.
e) Design implement and run a non-pure function named testMeasure that successfully executes all of the special methods, mathematical operators, and relational operators on instances of the FootMeasure class. At a minimum, the testMeasure function should execute the examples we have illustrated above, and test each relational operator to ensure that it correctly returns True and False values.
Constraints: Do not import/use any library modules .
Explanation / Answer
PROGRAM CODE:
# FootMeasure
class FootMeasure:
def __init__(self, feet=0, inches=0):
while(inches >= 12):
feet = feet + 1
inches = inches - 12
self.__feet = feet
self.__inches = inches
def __repr__(self):
if(self.__inches < 0):
toString = "Error: subtraction results in a negative value of " + str(self.__inches) + " inches"
else:
if(self.__inches>0 and self.__feet == 0):
toString = str(self.__inches) + " in. "
else:
toString = str(self.__feet) + " ft. " + str(self.__inches) + " in. "
return toString
def __add__(self, anotherFootMeasure):
addfeet = self.__feet + anotherFootMeasure.__feet
addInch = self.__inches + anotherFootMeasure.__inches
while(addInch >= 12):
addfeet = addfeet + 1
addInch = addInch - 12
return FootMeasure(addfeet, addInch)
def __sub__(self, anotherFootMeasure):
subfeet = self.__feet - anotherFootMeasure.__feet
subInch = self.__inches - anotherFootMeasure.__inches
while(subInch >= 12):
subfeet = subfeet + 1
subInch = subInch - 12
return FootMeasure(subfeet, subInch)
def __gt__(self, anotherFootMeasure):
inchSelf = self.__inches + (self.__feet * 12)
inchAnother = anotherFootMeasure.__inches + (anotherFootMeasure.__feet * 12)
return inchSelf > inchAnother
def __lt__(self, anotherFootMeasure):
inchSelf = self.__inches + (self.__feet * 12)
inchAnother = anotherFootMeasure.__inches + (anotherFootMeasure.__feet * 12)
return inchSelf < inchAnother
def __eq__(self, anotherFootMeasure):
inchSelf = self.__inches + (self.__feet * 12)
inchAnother = anotherFootMeasure.__inches + (anotherFootMeasure.__feet * 12)
return inchSelf == inchAnother
meas1 = FootMeasure()
meas2 = FootMeasure(feet = 5)
meas3 = FootMeasure(feet = 5, inches = 8)
meas4 = FootMeasure(inches = 68)
print(repr(meas1))
print(repr(meas2))
print(repr(meas3))
print(repr(meas4))
print(repr(meas2 - meas3))
print(repr(meas3 + meas4))
print(meas2<meas3)
print(meas3==meas4)
print(meas2>meas3)
OUTPUT:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.