O ¨ÓLc@s#dZGd„dƒZd„ZdS(uAClass and testbed defining a Fraction. Sean Ho for CMPT140 demo.cBs}|EeZdZddd„Zd„Zd„Zd„Zd„Zd„Zd „Z d „Z d „Z d „Z d „Z dS(u©A Fraction object has an integer numerator and denominator, and represents their ratio. Instance attributes: __numer, __denom. Public methods: getN(), getD()iicCs,t|ƒ|_t|ƒ|_|jƒdS(u·Create a new Fraction, optionally with the specified integer numerator and denominator. pre: n, d must be integers, d != 0 post: creates a new Fraction object.N(uintu_Fraction__numeru_Fraction__denomu_Fraction__reduce(uselfunud((u%/home/seanho/twu/python/oofraction.pyu__init__ scCsd|j|jfS(u$String representation of a Fraction.u(%d/%d)(u_Fraction__numeru_Fraction__denom(uself((u%/home/seanho/twu/python/oofraction.pyu__str__scCsd|j|jS(u"Float approximation of a Fraction.gð?(u_Fraction__numeru_Fraction__denom(uself((u%/home/seanho/twu/python/oofraction.pyu __float__scCs|jS(uAccessor: numer(u_Fraction__numer(uself((u%/home/seanho/twu/python/oofraction.pyugetNscCs|jS(uAccessor: denom(u_Fraction__denom(uself((u%/home/seanho/twu/python/oofraction.pyugetDscCs|jdkrt‚ndS(uoEnsure constraints are met: denom != 0. pre: none post: none raises: ZeroDivisionError.iN(u_Fraction__denomuZeroDivisionError(uself((u%/home/seanho/twu/python/oofraction.pyu__check#scsP‡fd†‰ˆ|j|jƒ}|j|_|j|_|jƒdS(u&Simplify the Fraction to lowest terms.cs!|dkr|Sˆ|||ƒS(unEuclid's Greatest Common Denominator. pre: a, b: integers post: returns GCD of (a, b).i((uaub(ugcd(u%/home/seanho/twu/python/oofraction.pyugcd-s N(u_Fraction__numeru_Fraction__denomu_Fraction__check(uselfug((ugcdu%/home/seanho/twu/python/oofraction.pyu__reduce+s cCsQt|jƒ|jƒ|jƒ|jƒ|jƒ|jƒƒ}|jƒ|S(uwAdds self to other Fraction and returns result as a new Fraction object. pre: other must be a Fraction.(uFractionugetNugetDu_Fraction__reduce(uselfuotherus((u%/home/seanho/twu/python/oofraction.pyu__add__:s  cCs=t|jƒ|jƒ|jƒ|jƒƒ}|jƒ|S(u|Multiples self by other Fraction and returns result as a new Fraction object. pre: other must be a Fraction.(uFractionugetNugetDu_Fraction__reduce(uselfuotherup((u%/home/seanho/twu/python/oofraction.pyu__mul__Ds cCs|t|jƒ|jƒƒS(uzDivides self by other Fraction and returns result as a new Fraction object. pre: other must be a Fraction.(uFractionugetDugetN(uselfuother((u%/home/seanho/twu/python/oofraction.pyu __truediv__MscCs|tdƒ|S(uuSubtracts other from self and returns result as a new Fraction object. pre: other must be a Fraction.iÿÿÿÿ(uFraction(uselfuother((u%/home/seanho/twu/python/oofraction.pyu__sub__SsN(u__name__u __module__u__doc__u__init__u__str__u __float__ugetNugetDu_Fraction__checku_Fraction__reduceu__add__u__mul__u __truediv__u__sub__(u __locals__((u%/home/seanho/twu/python/oofraction.pyuFractions        uFractioncCsddddddddd d g }x$|D]}t|d t|ƒƒq+Wyd }t|ƒ}Wntk r‚t|d ƒYnXtd|dd|ƒdS(uTestbed for Fraction class.u Fraction()u Fraction(2)u Fraction(3,2)u Fraction(4,6)u Fraction(2.5)ufloat(Fraction(3,5))uFraction(1,3) + Fraction(1,6)uFraction(7,3) - Fraction(1,3)uFraction(2,3) * Fraction(9,8)uFraction(4,3) / Fraction(2,5)u=u Fraction(2,0)ucorrectly raised ZDEuERROR!ushould have raised ZDEu but yieldedN(uprintuevaluZeroDivisionError(utestsutestuZDEtestuf1((u%/home/seanho/twu/python/oofraction.pyuruntestsYs(   N(u__doc__uFractionuruntests(((u%/home/seanho/twu/python/oofraction.pyusT