Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

************************* PYTHON ******************* 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,

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

image text in transcribed

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): """  """ self.n = numerator self.d = denominator def __add__(self, other): 
 """  """ if not isinstance(other, RationalNumber):
 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): """ 
  """
 if not isinstance(other, RationalNumber): 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): 
 """  """
 if not isinstance(other, RationalNumber): other = RationalNumber(other) n1, d1 = self.n, self.d n2, d2 = other.n, other.d return RationalNumber(n1*n2, d1*d2) def __div__(self, other): 
 """  """
 if not isinstance(other, RationalNumber): 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__
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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learn To Program Databases With Visual Basic 6

Authors: John Smiley

1st Edition

1902745035, 978-1902745039

More Books

Students also viewed these Databases questions

Question

5. Prepare for the role of interviewee

Answered: 1 week ago

Question

6. Secure job interviews and manage them with confidence

Answered: 1 week ago