Warning

This page is located in archive.

See the general homework guidelines!

You are going to write a simple class to represent fractions in Python. Place it in the module called `fraction.py`

Your class should:

- be called Fraction.
- have a constructor which stores the numerator and the denominator (assume both integers):

def __init__(self, numerator, denominator): self.numerator = numerator # top self.denominator = denominator # bottom

- the constructor should raise the
`ZeroDivisionError`

error and print an error message in case the denominator is zero. - have
`__str__()`

method implemented. This allows for the objects of this class to be typed to string, i.e., you can use the Fraction objects in the`print()`

function. Also see Section 15.7 of Wentworth. There are several requirements:

# the output should be in the form: numerator/denominator f1 = Fraction(2,3) print(f1) >>> 2/3 # a reduced fraction should be printed - a fraction in which the numerator and denominator are integers that have no other common divisors than 1. f2 = Fraction(12,18) print(f2) >>> 2/3 # print only numerator in case the denominator is 1 f3 = Fraction(8,2) print(f3) >>> 4 # print initial ''-'' in case the fraction is negative. No other representations of negative fractions will be accepted. f4 = Fraction(-2,3) print(f4) >>> -2/3 f5 = Fraction(2,-3) print(f5) >>> -2/3 f6 = Fraction(-2,-3) print(f6) >>> 2/3

- Implement methods
`__add__()`

,`__sub__()`

,`__mul__()`

,`__truediv__()`

allowing Fraction objects to be used in a standard set of arithmetic operations (aka overloading built-in operators). Also see here Operator overloading:

f1 = Fraction(12,18) f2 = Fraction(4,9) print("f1 = " + str(f1)) print("f2 = " + str(f2)) print("f1 + f2 = " + str(f1+f2)) print("f1 - f2 = " + str(f1 - f2)) print("f1 * f2 = " + str(f1 * f2)) print("f1 / f2 = " + str(f1 / f2)) >>> f1 = 2/3 >>> f2 = 4/9 >>> f1 + f2 = 10/9 >>> f1 - f2 = 2/9 >>> f1 * f2 = 8/27 >>> f1 / f2 = 3/2

- Overload the following comparison operators:
`__eq__()`

(equal),`__lt__()`

(less than),`__le__()`

(less than or equal),`__gt__()`

(greater than),`__ge__()`

(greater than or equal). All the operations have to be meaningful, i.e., follow the rules for fraction comparison! Also see here: Operator overloading. (Note: In Python3, it is not necessary to implement the inequality operator when`__eq__()`

is defined.)

Subtask | Max. points |

Constructor - takes 2 arguments (ints), raise error if denominator is 0 | 2 |

`__str__()` method | 2 |

overloading arithmetic operators | 2 |

overloading comparison operators | 2 |

clean documented code (function decomposition, following naming conventions, doc strings, etc.) | 4 |

Total | 12 |

courses/be5b33prg/homework/regular/05_fraction_class.txt ยท Last modified: 2020/11/26 14:42 by nemymila