Use Python to extend the Fraction Class Start with the Python code below: Fr act
ID: 3561519 • Letter: U
Question
Use Python to extend the Fraction Class
Start with the Python code below: Fraction.py
class Fraction:
def __init__(self,n,d):
self.num = n
self.den = d
def __str__(self):
return str(self.num) + "/" + str(self.den)
def __add__(self,otherfraction):
newnum = self.num*otherfraction.den + self.den*otherfraction.num
newden = self.den * otherfraction.den
cd = self.gcd(newnum,newden)
return Fraction(newnum//cd,newden//cd)
def gcd(self,m,n):
while m%n != 0:
oldm = m
oldn = n
m = oldn
n = oldm%oldn
return n
def __eq__(self,otherFraction):
return( self.num==otherFraction.num and self.den==otherFraction.den)
f1 = Fraction(1,2)
f2 = Fraction(3,4)
f3 = f1 + f2
print(f3)
Extend the Fraction Class above Using Python by finding the solutions for the list 1-5 below:
Explanation / Answer
class Fraction:
def __init__(self,n,d):
'''
if not isinstance(n, int):
print "Error :: Num is not int"
return
if not isinstance(d, int):
print "Error :: Den is not int"
return
if d == 0 :
print "Error :: Infinte fraction value "
return
print 'GGG'
gc = self.gcd(n,d)
print "gcd",gc
self.num = n/gc
self.den = d/gc
print "Class instancteated"
'''
print 'GGG'
gc = self.gcd(n,d)
print "gcd",gc
self.num = n/gc
self.den = d/gc
def gcd(self,m,n):
m = abs(m)
n = abs(n)
while(m > 0):
tmp = m
m = n%m
n = tmp
return n
def __str__(self):
return str(self.num) + "/" + str(self.den)
def __add__(self,other):
new_num = self.num + other.num
new_den = self.gcd(self.den,other.den)
return Fraction(new_num , new_den)
def __sub__(self,other):
new_num = self.num - other.num
new_den = Fraction.gcd(self.den,other.den)
return Fraction(new_num , new_den)
def __mul__(self,other):
new_num = self.num * other.num
new_den = self.den * other.den
return Fraction(new_num , new_den)
def to_decimal(self):
return float(self.num)/self.den
def __truediv__(self,other):
new_num = self.num % other.num
new_den = self.den % other.den
if new_num == new_den == 0 :
return True
return False
def __gt__(self, other):
return self.to_decimal() > other.to_decimal()
def __ge__(self, other):
return self.to_decimal() >= other.to_decimal()
def __lt__(self, other):
return self.to_decimal() < other.to_decimal()
def __le__(self, other):
return self.to_decimal() <= other.to_decimal()
def __eq__(self, other):
return self.to_decimal() != other.to_decimal()
def getNum(self):
return self.num
def getDen(self):
print 'self.den'
return self.den
def __eq__(self,other ):
return( self.num==other.num and self.den==other.den)
def main():
print "aaa"
x = Fraction(1,2)
y = Fraction(3,4)
print("asserts to test Exercise #1 getNum,getDen")
assert x.getDen() == 2
assert y.getNum() == 3
print("asserts to test Exercise #2 GCD in __init__")
z = Fraction(10,20)
assert z.getNum() == 1 # Num should have been reduced to 1
assert x+y == Fraction(5,4) # test original Fraction
print("asserts to test Exercise #3 + - / ")
assert x-y == Fraction(-1,4)
assert x*y == Fraction(3,8)
assert x/y == Fraction(2,3)
print("asserts to test Exercise #4 comparisons" )
assert not (x>y)
assert (x>=x)
assert (y>x)
assert (x<=y)
assert (x<=x)
assert not(x!=x)
assert (x!=y)
assert not (y<=x)
print("asserts to test Exercise #5 integers only" )
print("-- init should print or raise an error from next call" )
Fraction(1.2,3)
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.