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

Use Python to extend the Fraction Class below Start with this Python code: Fract

ID: 3561749 • Letter: U

Question

Use Python to extend the Fraction Class below

Start with this Python code: 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 to find solutions for the list 1-5 below

Explanation / Answer

As with decimal, new values can be created in several ways. One easy way is to create them from separate numerator and denominator values:

The lowest common denominator is maintained as new values are computed.

Another way to create a Fraction is using a string representation of <numerator> / <denominator>:

Strings can also use the more usual decimal or floating point notation of [<digits>].[<digits>].

There are class methods for creating Fraction instances directly from other representations of rational values such as float or decimal.

Notice that for floating point values that cannot be expressed exactly the rational representation may yield unexpected results.

Using decimal representations of the values gives the expected results.

Arithmetic

Once the fractions are instantiated, they can be used in mathematical expressions as you would expect.