![]() |
![]() Spring Semester 2007 |
Consider the following code: | It should print the value of 5. |
Problem One:def test(): a = point(3, 0) b = point(0, 4) print a.distanceTo(b)
Define a class of objects calledpoint
that model points in a plane:
- A
point
is a pair of two coordinates (x
andy
).- Every
point
can calculate and report its distance to any otherpoint
;- 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. |
Problem Two:def test(): u = line(point(3, 0), point(0, 4)) print u.length() v = line(point(-1, 0), point(0, -1)) print v.length()
Define a class of objects calledline
that models lines in 2D.
- Every
line
is a pair of twopoint
s.- A
point
is a pair of two numbers as before.points
should be able to determine their distance to otherpoints
(see above).lines
are created by passing twopoints
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). |
Problem Three:def test(): t = triangle(point(0, 3), point(0, 0), point(4, 0)) print t.area()
Define a class of objects calledtriangle
that model planar triangles.(If the three
- A
triangle
should be a set of threelines
- However, a
triangle
is created by specifying threepoints
- Every
triangle
should be able to calculate and report its area.point
s are collinear thetriangle
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 |
Define a class of objects calledclock
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) >>> |
Define a class of objects calledtigger
that canbounce()
.
tigger
s are two-dimensional objects.- when they bounce their
x
andy
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. |