Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using python Create a class called Fraction with the following methods: Construc

ID: 3806617 • Letter: U

Question

using python

Create a class called Fraction with the following methods: Constructor: All fractions have a numerator and a denominator. Precondition: parameter for the denominator of a fraction should NEVER be 0. Store all fractions with a non-negative denominator Make sure to reduce the faction to lowest terms before storing: Use an external function called GCD (Greatest Common Divisor). Provide DEFAULT parameters for the fraction so that objects can be instantiated with 0, 1 or 2 parameters. _str_ method to convert the fraction object/address to a string (_str_) It should return a string that looks similar to this: "3/4" or in general "numer/denom" _add_(self, op2) method that allows fractions to be added: It should RETURN a fraction object. _sub_(self, op2) method that allows fractions to be subtracted: It should RETURN a fraction object. Other methods for fractions: Allow conversion to a float. Given a fraction, it returns a float. _float_ Allow comparison for equality _eq_ Allow comparison for less than _It_ Test your Fraction class carefully and completely ~ test each method of the class. Example test data: from fraction import * f1 = Fraction(1, -3); f2 = Fraction(2, 8); f3 = Fraction(); f4 = Fraction(2, -4) print("Testing ADD") print("1/-3 + 2/8 should be -1/12: ", f1 + f2) print("1/-3 + 2/-4 should be -5/6: ", f1 + f4) print("1/-3 + 0/1 should be -1/3: ", f1 + f3) print("1/-3 + 5 should be 14/3: ", f1 + 5) print(Testing EQ") print("4/3 == 3/9 False: Fraction(4, 3) == Fraction(3, 9)) print("6/18 == 3/9 True: ", Fraction(6, 18) == Fraction(3, 9)) print("Testing float") print("4/3 should be 1.3: ", float(Fraction(4, 3))) print(Testing LT") print("4/3

Explanation / Answer

class Fraction:
    def __init__(self,top,bottom):
        def gcd(m, n):
            while m % n != 0:
                old_m = m
                old_n = n
                m = old_n
                n = old_m % old_n
            return n
        common = gcd(top,bottom)
        self.num = top/common
        self.den = bottom/common
    def __str__ (self):
        return str(self.num) + "/" + str(self.den)
    def get_num(self):
        return self.num
    def get_den(self):
        return self.den
    def __add__(self, other_fraction):
        new_num = self.num * other_fraction.den + self.den * other_fraction.num
        new_den = self.den * other_fraction.den
        return Fraction(new_num, new_den)
    def __sub__(self, other_fraction):
        new_num = self.num * other_fraction.den - self.den * other_fraction.num
        new_den = self.den * other_fraction.den
        return Fraction(new_num, new_den)
    def __mul__ (self, other_fraction):
        new_num = self.num * other_fraction.num
        new_den = self.den * other_fraction.den
        return Fraction(new_num, new_den)
    def __div__(self, other_fraction):
        new_num = self.num * other_fraction.den
        new_den = self.den * other_fraction.num
        return Fraction(new_num, new_den)
    def __eq__(self, other):
        first_num = self.num * other.den  
        second_num = other.num * self.den
  
   def __lt__(self, other):
       first_num = self.num * other.den  
       second_num = other.num * self.den
       return first_num < second_num
   def __div__(self ):
       new_num = -self.num
       new_den = self.den
        return Fraction(new_num, new_den)
      
   def __float__(self ):
       return float(self.num/self.den)
   def __trunc__(self ):
       return int(self.num/self.den)

a = Fraction(10,20)
b = Fraction(30,20)
print a
print "numerator is",a.get_num()
print "denominator is",a.get_den()
print "equality is",(a<b)
print "sum is",(a+b)
print "difference is",(a-b)
print "product is",(a*b)
print "division is",(a/b)