Using python: Suppose a class called Fraction has been defined. The following st
ID: 3812457 • Letter: U
Question
Using python:
Suppose a class called Fraction has been defined. The following statements appear in the end-user’s main program. Describe All of the methods that MUST be defined in the Fraction class to allow this code to work.
NOTE: Do NOT write the methods themselves, but rather describe which ones would need to be implemented to make this customer’s code work:
from modFraction import Fraction
f1 = Fraction(4,5 ); f2 = Fraction( 3,7) ; f3 = f1 + f2 #computes the sum of the fractions
print(s3
Explanation / Answer
f1 = Fraction(4,5 ); and f2 = Fraction( 3,7); => for these methods to work, we need a constructor in the class.. taking 2 arguments. First argument will denote the integral part, while second may denote fractional part.
for f3 = f1 + f2 to work => there need to be defined a magic method __add__(self, other) which will return the result in fraction class object format.
print(f3) => for this method to work, there need to be a __str__(self): method, which returns a string.. that string willl be consumed by print() method.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.