The program `handin21` will only submit files in the
cs21/homework/9 directory.

Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page

Some of the problems will have optional components that allow you to
further practice your skills in Python. Optional portions will not be
graded, but may be interesting for those wanting some extra challenges.

A Fraction Class

Write a class that stores a fraction as an integer numerator and a
non-zero denominator and supports basic operations on fractions such as
add, subtract, multiply, and divide. In case you forgot how to work with
fractions, Wikipedia has a quick refresher.
Below is a sample test program that shows how to create a new instance of the Fraction class and work with fractions.

def test(): f1=Fraction(1, 2) f2=Fraction(1, 3) f3=Fraction(2 ,4) print "f1 =", f1 print "f2 =", f2 print "f1 plus f2 =",f1.add(f2) print "f1 times f2 =",f1.mul(f2) print "f1 divided by f2=", f1.div(f2) print "f1 minus f2=",f1.sub(f2) print "inverse of f1=",f1.inverse() print "negation of f1=",f1.neg() print "negation of negation of f1=",f1.neg().neg() print "decimal value of f2=",f2.float() if f1 > f2: print "%s is greater than %s" % (f1, f2) if f3==f1: print "%s equals %s" % (f3, f1) try: f3=Fraction(4, 0) except ZeroDivisionError: print "Creating a fraction with a 0 denominator is illegal"

The output of running this test code is

f1 = 1/2 f2 = 1/3 f1 plus f2 = 5/6 f1 times f2 = 1/6 f1 divided by f2= 3/2 f1 minus f2= 1/6 inverse of f1= 2 negation of f1= -1/2 negation of negation of f1= 1/2 decimal value of f2= 0.333333333333 1/2 is greater than 1/3 1/2 equals 1/2 Creating a fraction with a 0 denominator is illegal

Your Fraction class must implement the following features:

- Implement the class methods
`add, sub, mul, div, inverse, and neg`. Each of these methods returns a new Fraction instance. - Implement a
`square`method that squares an instance of a Fraction object and returns the new Fraction. - Implement the method
`float`that returns a float representing the decimal equivalent of a fraction instance. - Implement the
`__str__`method to support printing of Fractions as strings. See the note regarding reduced fractions below. - Implement the
`__cmp__`method to support comparisions of two Fractions, as shown in the sample code above. Statements such ascmp(f1, f2) f1 < f2 f1 >= f2

are valid comparisions in a correct implementation. - If the user tries to
create a fraction with a 0 denominator, your class should raise an
exception. What type of exception is raised by python when you try to
divide by 0? The keyword
`raise`in python will raise an exception of a particular type. An example is`raise KeyboardInterrupt`. - Document your class methods so that users who wish to use your class
can figure out how to use it by running
`help(fraction)`. - Expand on the
`test()`function above to test your class and verify that it works correctly.

Save your class implementation in the file
`fraction.py`

Reduced Fractions

All of your fractions should be printed in reduced form, meaning that
there is no integer greater than 1 that divides both the numerator and
denominator of of your fraction. For example, 1/2 is the reduced form of
the fraction 6/12. To get your fractions in reduced form, you may want to
use the following algorithm to compute the greatest common divisor of
two integers gcd(a, b) = a : if b=0 gcd(b, a mod b) : otherwise

If the denominator of any fraction is 1, you do not need to print the denominator when you display the fraction as a string. Thus 3/1 should be printed as just 3.

Supporting +, -, /, and * operators

When adding two integers f1 + f2 f1 *= f3 f4 = -f5 float(f3)work as expected.

Optional Extensions

As noted above, these questions are NOT required to receive full credit.
There are several extensions you could add to this program. Here are a few we
thought might be interesting.
f1 = 3/4 f1+Fraction(2, 1) #returns 11/4 f1+2 #also returns 11/4 f1*8 #returns 6 as a FractionYou can use the

def myFunction(val): if type(val)==int: print "val is an integer, do integer stuff" elif type(val)==str: print "val is a string, do string stuff" else: print "assume val is a fraction"What happens if you try to add an integer and a fraction with the fraction on the right side of the addition, e.g.,

In writing your class for fractions, you probably used standard integer operations such as +, -, *, and / to compute your answers. Python is way too nice in giving you all those built-in functions. All you really need are the following:

- a function
`increment(n)`that takes an integer`n`and returns`n+1`. - a function
`decrement(n)`that takes an integer`n`and returns`n-1`. - a function
`isZero(n)`that returns`True`if`n`equals 0 and`False`otherwise. - recursion

add(a,b) #returns the sum of integers a and b multiply(a,b) #returns the product power(a,b) #returns a raised to the power of b sub(a,b) #returns a-b if b <= a and 0 otherwise greaterThan(a,b) #returns True if b is greater than aRemember, the first function you write can only call

def increment(n): return n+1 def decrement(n): #don't allow negative values of n if n > 0: return n-1 return 0 def isZero(n): return n==0

How many function calls do you make to compute something simple like multiply(2,2)? Aren't you glad python let's you write 2*2 instead?