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

************************* PYTHON ******************* Now do the following Start

ID: 3700319 • Letter: #

Question

************************* PYTHON *******************

Now do the following Start with the program below Fill in the missing method docstrings, one for each instance method Modify the following program so that you can support addition, subtraction, multiplication, and division with scalar (int or float) values. For examplea -RationalNumber (1, 4 ) print( b) should print out the value 1/8 For now, assume that scalar values will occur always on the right-hand-side of the arithmetic operator in question Define your own exception class named unexpectedargumentType, which should subclass from TypeError and should be raised from your arithmetic operator implementations when the input parameter is of an unexpected type. For example, if the calling context provides (RationalNumber, RationalNumber) or (RationaINumber, int), or (Rationa1Number, float), the operation should proceed. Otherwise, an UnexpectedArgumentType exception should be raised from your code.

Explanation / Answer

class RationalNumber: """ Reference: https://anandology.com/python-practice-book/object_oriented_programming.html Rational Numbers with support for arthmetic operations. >>> a = RationalNumber(1, 2) >>> b = RationalNumber(1, 3) >>> a + b 5/6 >>> a - b 1/6 >>> a * b 1/6 >>> a/b 3/2 """ def __init__(self, numerator, denominator=1): """ @param numerator numerator of rational number @param denominator denominator of rational number """ self.n = numerator self.d = denominator def __add__(self, other): """ @param other rational number to add """ if not isinstance(other, RationalNumber): if not isinstance(other, int) and not isinstance(other, float): raise UnexpectedArgumentType else: other = RationalNumber(other) n = self.n * other.d + self.d * other.n d = self.d * other.d return RationalNumber(n, d) def __sub__(self, other): """ @param other rational number to subtract """ if not isinstance(other, RationalNumber): if not isinstance(other, int) and not isinstance(other, float): raise UnexpectedArgumentType else: other = RationalNumber(other) n1, d1 = self.n, self.d n2, d2 = other.n, other.d return RationalNumber(n1*d2 - n2*d1, d1*d2) def __mul__(self, other): """ @param other rational number to multiply """ if not isinstance(other, RationalNumber): if not isinstance(other, int) and not isinstance(other, float): raise UnexpectedArgumentType else: other = RationalNumber(other) n1, d1 = self.n, self.d n2, d2 = other.n, other.d return RationalNumber(n1*n2, d1*d2) def __truediv__(self, other): """ @param other rational number to divide """ if not isinstance(other, RationalNumber): if not isinstance(other, int) and not isinstance(other, float): raise UnexpectedArgumentType else: other = RationalNumber(other) n1, d1 = self.n, self.d n2, d2 = other.n, other.d return RationalNumber(n1*d2, d1*n2) def __str__(self): return "%s/%s" % (self.n, self.d) __repr__ = __str__ class UnexpectedArgumentType(TypeError): print('Argument not valid') a = RationalNumber(1, 2) b = RationalNumber(1, 3) print(a + b) print(a - b) print(a * b) print(a / b)