Use the skeleton to implement a Fraction class (in the file fraction.py
) containing two integers: numerator
and denominator
.
The file test_fraction.py
can be edited and used to test the created class.
-
Add a member function to give a print representation for the class, e.g.,
print(Fraction(1, 2))
should yield in something like:1/2
. -
Add a member function to add to fractions, e.g.,
print(Fraction(1, 2) + Fraction(2, 3))
should yield7/6
. Remember: the return type should be chosen correctly, e.g., the addition of two fractions results in a new fraction, not a string representation of the result! -
Add a member function to invert a fraction, e.g.,
print(Fraction(1, 2).invert())
should yield2/1
. -
Add a member function to give a decimal representation of a fraction, e.g.,
print(Fraction(1, 2).to_float())
should yield0.5
. -
Add a member function that returns the integer part of a fraction, e.g.,
Fracion(7, 6).integer()
should yield1
. -
Add member functions to subtract, multiply and divide fractions. Hint: can we reuse any of the member functions?
-
Add a member function to simplify a fraction, e.g.,
print(Fraction(2, 6).simplify())
should yield1/3
. Hint: use a function to calculate the greatest common divisor. -
Can we improve our arithmetic functions with our newly created simplification method?
-
Add some comparison operators, e.g.,
==
and<
. -
How would you deal with negative fractions?
-
Can you add integer multiplication functionality, e.g.,
print(Fraction(1, 2) * 2)
should yield2/2
. What about commutativity? -
How can we prevent things like:
Fraction(0.4, 0.2)
?