using python: Write the entire method that must be included in the Fraction clas
ID: 3812462 • Letter: U
Question
using python:
Write the entire method that must be included in the Fraction class to allow the end user to write both of these statements. Please assume the constructor (__init__) method has already been written. f4 = Fraction(3,4) + Fraction(1,2)
f5 = Fraction(2,5) + 10
Suppose MSUMPerson has the following attributes:
an attribute called nextIDNum is initialized to 100 and keeps track of the next available ID so that when an object is created, they receive a unique ID that hasn’t already been used.
an attribute called idNum which will contain the ID for each individual person object that is created.
An attribute called population which is initialized to 0 and keeps track of the total number of MSUMPerson objects that have been created.
Complete the code for the class so that it has ONLY a constructor method
Explanation / Answer
Here is the method for Fraction class to add two fractions:
def __add__(self, other):
if type(other) is int:
other = Fraction(other, 1)
newnum = self.num * other.den +
self.den * other.num
newden = self.den * other.den
return Fraction(newnum, newden)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.