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

Please use the provided template: Your task in this programming assignment is to

ID: 3874196 • Letter: P

Question

Please use the provided template:

Your task in this programming assignment is to write a Python program that efficiently implements a fraction class. What does efficiently mean? Well, think through the problem so that you do not have to unnecessarily perform operations (as they can be costly). In addition, you should make use of any additional functions that reasonably encapsulate separate logical tasks. This is a part of good coding style You must provide/address the following in the fraction class A constructor that initializes fractions (with specified values for the numerator and denominator or 0/1 as default) Appropriate accessors and mutators for the instance variables Proper automatic reducing/simplifying of fractions when necessary Prevention of a 0 denominator; .Overloading of the arithmetic operators on fractions (i.e., the ability to add, subtract, multiply, and divide fractions using the arithmetic operators +, -, *, and / respectively); A method that returns the floating point representation of a fraction; and A method that specifies how fractions should be displayed (e.g., via print). A template is provided that includes a main part of the program intended to test your fraction class Here's a sample run of a correctly implemented fraction class: fl: 0/1 (0.0) f2: 5/8 (0.625) f3: 3/4 (0.75) fl: 5/4 (1.25) £2: 0/1 (0.0) f3: 25/16 (1.5625) f4: 2/275 (0.00727272727273) To help clarify, here are some specifics and/or constraints: (1) You must include a meaningful header, use good coding style, use meaningful variable names, and comment your source code where appropriate (2) Your output should be exactly like the sample run shown above; (3) Therefore, you should not modify the main part of the program (at all); and (4) You must submit your source code as a single py file

Explanation / Answer

Fraction class program

from __future__ import division                                        #import division for getting rational values
def gcd(n, d):                                                                 #GCD function for fractin generation
    while n != d:
        if n > d:
            n = n - d
        else:
            d = d - n
    return n
class Fraction:                                                                                #fraction class
    def __init__(self, n, d):                                                                  #generate fraction
        self.num = int(n / gcd(abs(n), abs(d)))
        self.denom = int(d / gcd(abs(n), abs(d)))
        if self.denom < 0:
            self.denom = abs(self.denom)
            self.num = -1*self.num
        elif self.denom == 0:
             raise ZeroDivisionError

    def Add(self, other):                                                                          #addition Function
        return 2*(self.num*other.denom + self.denom*other.num)
    def Sub(self, other):                                                                         #subtraction function
        return 2*(self.num*other.denom - self.denom*other.num)
    def Mul(self, other):                                                                          #multiplication function
        return (self.num*other.num) * (self.denom*other.denom)
    def Div(self, other):                                                                             #Division function
        return (self.num*other.denom)/(self.denom*other.num)
    def __str__(self):
        if type(self) is tuple:                                                         #string check function
            if self[1] < 0:
                return '%s/%s' %(self[0], -1*self[1])
            else:
                return '%s/%s' %(self[0], self[1])
        elif self.denom == 1:
            return str(self.num)
        else:
            return '%s/%s' %(self.num, self.denom)
    def __cmp__(self, arg):                                                             #Comparison Function
        if self > arg:
            return -1
        elif self == arg:
            return 0
        else:
            return 1
    def __nonzero__(self):                                                            #Non ero Value check function
        if self != 0:
            return True
        else:
            return False
def main():                                                                          #Main method
    f1=Fraction(5,4)                                                               #Fraction values passing
    f2=Fraction(5,8)
    f3=Fraction(3,4)
    f4=Fraction(1,1)
    print "f1 : ",f1,"(",f1.num/f1.denom,")"                                  # print Fractions
    print "f2 : ",f2,"(",f2.num/f2.denom,")"
    print "f3 : ",f3,"(",f3.num/f3.denom,")"
    print "f4 : ",f4,"(",f4.num/f4.denom,")"
    f5=Fraction.Add(f2,f3)                                            #Addition method call
    print f5
    f6=Fraction.Sub(f2,f3)                                            #Subtraction method call
    print f6
    f7=Fraction.Mul(f2,f3)                                             #Multiplication method call
    print f7
    f8=Fraction.Div(f2,f3)                                             #Division method call
    print f8
  
    return 0
if __name__ == '__main__':                                       #main method call
    main()


Output