|
|
CS 3723
Programming Languages
Fall 2014 |
Homework 11. Python Classes
|
Week 14-15: Nov 24 - Dec 5
|
Submit following directions at:
submissions
and rules at:
rules.
Deadlines are:
- 2014-12-03 23:59:59 (that's Wed, 3 Dec 2014, 11:59:59 pm)
for full credit.
- 2014-12-08 23:59:59 (that's Mon, 8 Dec 2014, 11:59:59 pm)
for 75% credit.
|
Important Notes:
If you are using Python 3, you need to use
__truediv__ instead of __div__ below.
This homework should be harder and more
time-consuming than the next homework:
H12 Time/Date Classes.
It would be a mistake to get hung up on H11 and not get to the
easier H12.
References: References for this homework are:
Additional references from our standard source are:
Fractions:
This homework focuses on a Python implementation of fractions.
Python already has an implementation of fractions in the Python library,
but you are not to look at this. (The Python code is very long and
sophisticated. I think it would be more confusing than helpful.)
You are also not to look up other Python fractions code online.
Instead, for this homework, you are to add code to an incomplete
fractions class given below.
The partial code below comes from the first link above, which
has a detailed discussion of the example.
You must read over the material about this example.
(My discussion here is not nearly enough for this homework.)
I have rewritten this program slightly, mainly to shorten identifiers
and replace print.
Class Implementing Fractions |
Rest + Output |
# fraction.py: implement fractions
import sys
# num = top = numerator
# den = bottom = denominator
def gcd(m,n): # m can be negative
while m%n != 0:
oldm = m
oldn = n
m = oldn
n = oldm%oldn
return n
class Fraction:
def __init__(self,top,bottom):
self.num = top
self.den = bottom
sys.stdout.write("New: ")
Fraction.show(self)
sys.stdout.write("\n")
def __str__(self):
return str(self.num)+"/"+str(self.den)
def show(self):
sys.stdout.write(str(self.num) + "/" +
str(self.den))
def __add__(self,other):
newnum = self.num*other.den + \
self.den*other.num
newden = self.den * other.den
com = gcd(newnum,newden)
return Fraction(newnum//com,newden//com)
def __eq__(self, other):
firstnum = self.num * other.den
secondnum = other.num * self.den
return firstnum == secondnum
| u = Fraction(1, 2)
v = Fraction(2, 3)
w = Fraction(3, 7)
x = Fraction(-4, 11)
y = Fraction(-7, 13)
z = Fraction(-2, 5)
sys.stdout.write("u + v: " +
str(u+v) + "\n")
sys.stdout.write("u == v: "+
str(x == y) + "\n")
sys.stdout.write("w + x: " +
str(w+x) + "\n")
sys.stdout.write("u + y: " +
str(u+y) + "\n")
sys.stdout.write("x + y: " +
str(x+y) + "\n")
sys.stdout.write("x: " +
str(x) + "\n")
x = w
sys.stdout.write("x: " +
str(x) + "\n")
% python fraction.py
New: 1/2 == u
New: 2/3 == v
New: 3/7 == w
New: -4/11 == x
New: -7/13 == y
New: -2/5 == z
New: 7/6
u + v: 7/6
u == v: False
New: 5/77
w + x: 5/77
New: -1/26
u + y: -1/26
New: -129/143
x + y: -129/143
x: -4/11
x: 3/7 after: x = w
|
Additions to the Fractions class above:
(These additions are taken from the same reference above.)
- Implement the simple methods getNum and getDen
that will return the numerator and denominator of a fraction.
- Add functions to handle the other binary infix arithmetic
operators: −,
*, and
/.
These must be functions __sub__,
__mul__, and __div__, similar to __add__ above.
- Add functions for binary infix comparison operators:
!=,
<, <=,
>, >=.
These must be functions __ne__, __lt__,
__le__, __gt__, __ge__,
similar to __eq__ above.
- "In many ways it would be better if all fractions were maintained
in lowest terms right from the start. Modify the constructor for
the Fraction class so that gcd is used to reduce fractions
immediately. Notice that this means the __add__ function
no longer needs to reduce. Make the necessary modifications."
(Taken from the link above.)
Testing Your Fractions class:
- Test Code A:
This assumes it is included in the same
file with the class.
|