Search for question
Question

5. Write a class Fraction to represent a fraction. It has a constructor to accept a numerator

and denominator. The denominator should be 1 by default. The constructor should

automatically simplify the fraction. So if 2 and 4 are passed to the constructor the fraction

should be 1/2. Implement a method that takes another Fraction, adds it to the current

fraction. The addition function should be chain able, i. e. if function name is add, the we

should be able call f.add (1/2).add (1/3). Leverage the add function to implement a +

function that can add two fractions and returns the result, (1/2 + 1/3 should return 5/6).

Use helper methods as needed, with appropriate access levels. The numerator and

denominator attributes should not be accessible outside the class. Implement a to_s to

return the fraction as a string in proper format (like "1/2").

Fig: 1