EECS 12 Summer 2006 Homework 6

General Instructions

Please place your programs in seperate files named as described in the problem. Improperly named programs will not count. Turn in your files by placing them in the dropbox. Please be mindful of the late policy.

 Correct 80% Commented 10% Attempted 10%

Problems

Problem 0: 50 points

• Call the new one treeNode.py
• Instead of the makeTree function, that code should go into TreeNode.__init__ instead.
• Instead of the printTree function, that code should go into TreeNode.print instead.
• Instead of the dumpToList function, that code should go into TreeNode.dumpToList instead.
• Rewrite the tests so that they use the methods instead of the old functions.

Problem 1: 50 points

Create a class for managing complex numbers. If you do not remember the mechanics of complex arithmatic, you can remind yourself by searching for complex on mathworld.wolfram.com. Your file should be called complex.py and your class should be called Complex. (There is already a python type called complex, but please do not make use of it. Imagine that you are programming the offical python complex class.)

Include the following methods:

• __init__(self, real, imag). Give real and imag default values of 0.0. This method should set the class attributes real and imag.
• __sub__(self, rhs)
• __mul__(self, rhs)
• __div__(self, rhs)
• __neg__(self)
• __abs__(self)
• __str__(self)
• __repr__(self)
• conjugate(self)

Include the following test code:

print Complex(5.0,0)+Complex(7.0,0)
print Complex(0,5.0)+Complex(0,7.0)
print Complex(5.0,0)+Complex(0,7.0)
print Complex(5.0,0)-Complex(7.0,0)
print Complex(0,5.0)-Complex(0,7.0)
print Complex(5.0,0)-Complex(0,7.0)
print Complex(5.0,0)*Complex(7.0,0)
print Complex(0,5.0)*Complex(0,7.0)
print Complex(5.0,0)*Complex(0,7.0)
print Complex(5.0,0)/Complex(7.0,0)
print Complex(0,5.0)/Complex(0,7.0)
print Complex(5.0,0)/Complex(0,7.0)
print abs(Complex(5.0,0))
print abs(Complex(0,7.0))
print abs(Complex(5.0,7.0))
print -(Complex(5.0,0))
print -(Complex(0,7.0))
print -(Complex(5.0,7.0))
print Complex(5.0,0).conjugate()
print Complex(0,7.0).conjugate()
print Complex(5.0,7.0).conjugate()
print repr(Complex(5.0, 7.0))
print Complex(1.0)
print Complex(imag=1.0)

It should print out the following:

(12.0+0j)
(0+12.0j)
(5.0+7.0j)
(-2.0+0j)
(0-2.0j)
(5.0-7.0j)
(35.0+0.0j)
(-35.0+0.0j)
(0.0+35.0j)
(0.714285714286+0.0j)
(0.714285714286+0.0j)
(0.0-0.714285714286j)
5.0
7.0
8.60232526704
(-5.0+0j)
(0-7.0j)
(-5.0-7.0j)
(5.0+0j)
(0-7.0j)
(5.0-7.0j)
(5.0+7.0j)
(1.0+0.0j)
(0.0+1.0j)