Вы находитесь на странице: 1из 1

SK1U/1 Introduction to Computer Science & Programming

12/14/2011
Classwork/Homework
Matthew Carlberg
FRACTION CLASS CONTINUED
A basic implementation oI the Fraction class has been uploaded to Dropbox ~ Python Resources
~ Unit 4 Code ~ Fraction.py. In this Iile, in addition to the Fraction class, there are also
Iunctions Ior Iinding the greatest common Iactor oI two numbers and the least common multiple
oI two numbers. The comments in the code describe how these two Iunctions are used.

Today, we will add a Iew more Iunctions:

1. Add the Iunction mult(selI, other) to our Fraction class to deIine what it means to
do Iraction1*Iraction2. This is called overloading an operator.

Test Code with Desired Results:
~~~aFraction(2,3)
~~~b Fraction(2,5)
~~~c a*b
~~~print(c)
4/15

2. Add the Iunction div(selI, other) to our Fraction class to deIine what it means to do
Iraction1/Iraction2. This is called overloading an operator.

Test Code with Desired Results:
~~~aFraction(2,3)
~~~b Fraction(2,5)
~~~c a/b
~~~print(c)
10/6

3. Add the Iunction reduce(selI) to our Fraction class, which reduces the Iraction to lowest
terms. Basic idea: Iind the greatest common Iactor oI the numerator and denominator
and divide both the numerator and denominator by the greatest common Iactor.

Test Code with Desired Results:
~~~aFraction(4,6)
~~~a.reduce()
~~~print(a)
2/3 #note 2.0/3.0 is NOT an acceptable answer here

4. (optional, 2 bonus) Add the Iunction add(selI, other) to our Fraction class to deIine
what it means to do Iraction1 Iraction2. Develop your own test code.

This assignment is adapted Irom the Python projects oI Gary Rubinstein.
(http://www.garyrubinstein.com/)

Вам также может понравиться