Spring Semester 2007


Homework Three: Classes and objects.
Consider the following code: It should print the value of 5.
def test():
    a = point(3, 0)
    b = point(0, 4)
    print a.distanceTo(b)
Problem One:
Define a class of objects called point that model points in a plane:

Now consider the following code: This prints 5 (first) then the square root of 2.
def test():
    u = line(point(3, 0), point(0, 4))
    print u.length()
    v = line(point(-1, 0), point(0, -1))
    print v.length()
    
Problem Two:
Define a class of objects called line that models lines in 2D.

Consider the following code: It prints 6.0 (the area of the triangle).
def test():
    t = triangle(point(0, 3), point(0, 0), point(4, 0))
    print t.area()
Problem Three:
Define a class of objects called triangle that model planar triangles. (If the three points are collinear the triangle is extremely flat, its area is 0 (zero), and that should be acceptable.)

Use Heron's formula to calculate the area of any triangle specified as above.


Consider the following code:
def test():
    c = clock("23:56")
    for i in range(10):
        c.tick()
It prints this:
>>> test()
23:57
23:58
23:59
00:00
00:01
00:02
00:03
00:04
00:05
00:06
Problem Four:
Define a class of objects called clock that models a clock.

Enough said.


Consider the following code:
def test():
    t = tigger(12, 34)
    for i in range(10):
        t.bounce()
It produces the following:
>>> test()
Tigger created at  ( 12,  34)
Tigger has just bounced to (  5,  25)
Tigger has just bounced to ( 25,  29)
Tigger has just bounced to ( 29,  85)
Tigger has just bounced to ( 85,  89)
Tigger has just bounced to ( 89, 145)
Tigger has just bounced to (145,  42)
Tigger has just bounced to ( 42,  20)
Tigger has just bounced to ( 20,   4)
Tigger has just bounced to (  4,  16)
Tigger has just bounced to ( 16,  37)
>>> 
Problem Five:
Define a class of objects called tigger that can bounce().

For examples of the transformation rule see the sample run above.


The due date of this assignment will be announced shortly. It will be something like Feb 7-9 or so.

An official due date will be posted soon on What's Due? The date listed above is just a ballpark date, to help you plan.

Updated by Adrian German for A202/A598