1Modify the constructor for the Fraction class so that GCD i

1.Modify the constructor for the Fraction class so that GCD is sued 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__,__It__,__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.

Note: Please Use \"Python\" language

Solution

from fractions import math
import operator
class Fraction:
def __init__(self, num1, num2):
self.num1 = num1
self.num2 = num2
def __add__(self):
print(\"add mthd\")
print(self.num1 + self.num2)
return self.num1 + self.num2
def __sub__(self):
print(\"sub mthd\")
print(self.num1 - self.num2)
return self.num1 - self.num2
def __mul__(self):
print(\"mult mthd\")
print(self.num1 * self.num2)
return self.num1 * self.num2
def __truediv__(self):
print(\"div block\")
try:
if int(self.num1) and int(self.num2):
print(self.num1 // self.num2)
return self.num1 // self.num2
else:
print(\"The given numbers are not integers\")
except Exception as e:
print(e)
def gcd_of_two_numbers(self):
print(\"GCD of given numbers\", math.gcd(self.num1, self.num2))
return math.gcd(self.num1, self.num2)
  
def arith_operators(self):
if operator.__gt__(self.num1, self.num2):
print(\"Num1 is Gretaer than Num2\")
else:
print(\"Num2 is Gretaer than Num1\")
if operator.__ge__(self.num1, self.num2):
print(\"Num1 is Gretaer than or equals to Num2\")
else:
print(\"Num2 is Gretaer than or equals to Num1\")
if operator.__lt__(self.num1, self.num2):
print(\"Num1 is Less than Num2\")
else:
print(\"Num2 is Less than Num1\")
if operator.__le__(self.num1, self.num2):
print(\"Num1 is Less than or equals to Num2\")
else:
print(\"Num2 is Less than or equals to Num1\")
if operator.__ne__(self.num1, self.num2):
print(\"Num1 is Not equuals to Num2\")
  
x = Fraction(7,8)   
x.arith_operators()
x.__add__()
x.__mul__()
x.__sub__()
x.__truediv__()
x.gcd_of_two_numbers()

1.Modify the constructor for the Fraction class so that GCD is sued to reduce fractions immediately. This means that the __add__function no longer needs to redu
1.Modify the constructor for the Fraction class so that GCD is sued to reduce fractions immediately. This means that the __add__function no longer needs to redu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site