06.0 Python‎ > ‎Python Notes‎ > ‎Python OOPs‎ > ‎

Fractions OOPs Python

Create a Fractions Class that will add, subtract multiply and divide fractions.


Python has methods that perform all arithmetic operations: +, -, *, / etc.

After implementing these methods in the Fraction class, the Fraction class will be able to add, subtract, multiply, divide any two fractions. These method will return a Fraction

Operator

method

comment

+

__add__


-

__sub__


*

__mul__


/

__truediv__


**

__exp__





Example:

If the  fractions are 2/ 3 and 1/6 then your program should produce the following fractions:

2/3 + 1/6 = 15/18

2/3 - 1/6 = 9/18                                                                   

2/3 x 1/6 = 2/18                                                                   

2/3 ÷ 1/6 = 12/3    


Modifications :

  • Modify your program so that the resulting fractions are expressed in the lowest terms.

  • if a fraction is negative, the negative sign must accompany the numerator

  • Create a GCF class to reduce the fraction into its lowest terms.  See GCF file for the algorithm


Starter Code



.


Comments