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:
• A `point` is a pair of two coordinates (`x` and `y`).
• Every `point` can calculate and report its distance to any other `point`;
• the distance is the square root of the sum of the squared differences between the corresponding `point`s' coordinates.

 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.
• Every `line` is a pair of two `point`s.
• A `point` is a pair of two numbers as before.
• `points` should be able to determine their distance to other `points` (see above).
• `lines` are created by passing two `points` to the Line constructor.
• A `line` object must be able to report its length;
• the length of a line is the distance between its two end points.

 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.
• A `triangle` should be a set of three `lines`
• However, a `triangle` is created by specifying three `points`
• Every `triangle` should be able to calculate and report its area.
(If the three `point`s 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()`.

• `tigger`s are two-dimensional objects.
• when they bounce their `x` and `y` coordinates change
• the rule by which they change: they become the sum of the squares of their digits

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