Q3Comprehension5 Marks30 Apr 2023
Passage
Based on the above data, answer the given subquestions.
rational is a class that represents positive rational numbers. Recall that a rational number x is of the form p/q, where the greatest common divisor of (p, q) is equal to 1, with q != 0. In this case, we will be modeling only positive rationals as a class.
```
class Rational:
def __init__(self, num, den):
self.num = num
self.den = den
self.reduce()
def print_info(self):
if self.den == 1:
print(f'{self.num}')
else:
print(f'{self.num}/{self.den}')
def reduce(self):
for i in range(min(self.num, self.den), 1, -1):
if self.num % i == 0 and self.den % i == 0:
self.num = self.num // i
self.den = self.den // i
break
def divide(self, k):
self.den = self.den * k
self.reduce()
```
A new method add should be introduced into the class to add two rational numbers. This method should accept another rational number, say rat, as argument and return the sum of the current rational number and rat. Select the correct implementation of this method. Sample behavior of this method is given below:
Input:
a = Rational(10, 2)
b = Rational(10, 3)
c = a.add(b)
c.print_info()
Output:
25/3