|
|
Tutorial 8:
Farewell Scheme (June 22)
- Midterm Post-Mortem
For the first half of today's tutorial, we will go over a few of the
most common mistakes and misconceptions seen on the midterm exam.
These concerned (at a high Level):
- Induction proofs
- Removing the first element of a list
- Extensions of binary search
- Order notation
- Modularity and Class Hierarchy in Scheme
We will now cover some concepts from Lecture Module 7
(see handouts)
- Some modules
Take a look at the three files
dtrig.scm,
module-tests.scm
triangles.scm.
dtrig.scm provides the three basic trigonometry functions
(sin, cos, tan) to work with angle measures given in degrees rather than
radians. module-tests.scm provides just one function which
will take a name and a series of tests and will run the tests, using output
to indicate passing/failure. This allows one way to write tests in modules.
The file triangles.scm provides a few classes to represent
triangles. Note that this module requires the other two. Make sure
you understand how these modules are set up.
- Class hierarchy: triangles
The
triangle% base class represents any triangle and
has the four operations
sides, perimeter, area,
and shortest-side. sides returns a list of the
lengths of the triangle's three sides, and the other three do what we would
expect. Only the sides method is not implemented in the base
class.
There are three subclasses of triangle%:
scalene%, isosceles%,
and equilateral%. These represent the different types of
triangles as one might expect, and equilateral%
is a subclass of isosceles% (why?).
Override the area function in isosceles% with
a more efficient version that takes advantage of the fact that the area of
an isosceles triangle is equal to the square of the repeated side length times
the sine of the angle between them, divided by 2. Then override the
shortest-side function in equilateral% with
a more efficient version (easy).
Next, make a new class right% (for right triangles),
and add it to the module. The initialization should take the lengths of the
two sides around the 90-degree angle, and the class should override the
functions sides and area, as well as add a new
function hypotenuse. Be sure to add tests for the new class.
- Using a module
In a new file, load the triangles module and write a new function,
obtuse?, which consumes an instance of any triangle class, and
produces true if and only if that triangle is obtuse, i.e., it has an angle
larger than 90 degrees. You might want to use the fact that this is only true
if the square of the longest side is strictly greater than the sum of the
squares of the other two sides.
|
| |
|
Last modified on
Friday, 19 August 2011, at 18:05 hours.