Question: class Fraction: def __init__(self, top, bottom): self.num = top self.den = bottom def __add__(self, otherFraction): newNum = self.num * otherFraction.den + self.den * otherFraction.num
class Fraction:
def __init__(self, top, bottom):
self.num = top
self.den = bottom
def __add__(self, otherFraction):
newNum = self.num * otherFraction.den + \
self.den * otherFraction.num
newDen = self.den * otherFraction.den
common = self.gcd(newNum, newDen)
return Fraction(newNum // common, newDen // common)
def __eq__(self, other):
firstNum = self.num * other.den
secondNum = other.num * self.den
return firstNum == secondNum
def gcd(self, m, n):
while m % n != 0:
oldm = m
oldn = n
m = oldn
n = oldm % oldn
return n
def __str__(self):
return str(self.num) + "/" + str(self.den)
[Use Pythone Language please ](Use the fraction.py file )
Complete the following:
1.
Modify the Constructor for the Fraction class so that GCD is used to reduce
fractions immediately. This means that the __add__ function no longer needs
to reduce.
2.
Implement the remaining simple arithmetic operators (__sub__, __mul__, and
__truediv__).
3.
Implement the remaining relational operators (__gt__, __ge__, __lt__, __le__,
and __ne__).
4.
Modify the constructor for the Fraction class so that it checks to make sure
that the numerator and denominator are both integers. If either is not an
integer the constructor should raise an exception.
You must use the Fraction class created in class and located on the Instructor drive
as shown above. If you do not do this, you will receive a grade of 0.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
