This assignment includes only one animation problem, so you should be able to write all of your answers in one Definitions window. As before, your Interactions window should still show me the results of test cases on functions by themselves (before you use them in an animation).
For all the programming assignments, be sure to follow the design recipe. Write your function contract, examples, and function definition in the Definitions Window, save it to a file, and e-mail me this file. Also test your program: since you've already included examples in the Definitions window, you should be able to hit the Run button and see all the results (along with what you said they "should be"). Save the resulting Interactions window to a text file and e-mail me this file too. Be sure to choose meaningful names for functions and parameters, and watch for opportunities to re-use functions you, I, or the textbook have already written.
Also turn in a log of how many errors of different kinds you encountered in the assignment, with brief comments describing each one ("mismatched parentheses" is self-explanatory, but more complex errors might need more description). You may do this using the PSP forms, or simply by keeping track in a text file or on paper and turning it in.
A "shape" is either a circle or a rectangle. A "circle" comprises a posn (center) and a number (radius). A "rectangle" comprises a posn (top-left) and two numbers (width and height).
Define all three of these data types, complete with input and output templates. Then develop the following functions:
perimeter : shape => number
pi
(which is still only an
approximation, but it's correct to about twelve decimal places).perimeter
function is developed in the
textbook, but it's not exactly the same as this because it works on
circles and squares, not circles and rectangles.area : shape => number
move-shape : shape number (dx) number (dy) => shape
contains? : shape posn => boolean
posn
is inside a circle
, test whether the distance from the
posn
to the center of the circle is less than the radius
of the circle. Recall the distance formula
d = sqrt((x1-x2)2 + (y1-y2)2).
Then write an animation in which the world is a
shape. show-world
should be able to handle both
possibilities, displaying either a solid green circle or a solid blue
rectangle of the specified location, size and shape.
handle-key
should move the shape up, down, left, or right
in response to arrow keys.
Test your animation in two different ways: with the
initial world a circle, and with the initial world a rectangle.
(DrScheme won't allow you to call big-bang
twice in one
definitions window, so comment out one of the two big-bang
calls.)
Extra credit: make the handle-key
function allow the user to switch between circle and rectangle.
(The letter-c key is #\c
; the letter-r key is
#\r
. Both of them are of type char
,
which you haven't seen before; you can test whether something is a
char
with the built-in char?
function, and
test whether two char
's are the same with the built-in
char=?
function.)
Error log: /25
(I'm not grading on how many or how few errors you encountered,
only on whether you recorded them adequately.)
Function or struct name | Data def'n | Define-struct | Contracts for struct functions |
Templates | Contract | Examples | Definition | Test runs |
---|---|---|---|---|---|---|---|---|
circle data type |
/5 | /5 | /5 | /10 | ||||
rectangle data type |
/5 | /5 | /5 | /10 | ||||
shape data type |
/5 | /10 | ||||||
perimeter function |
/5 | /5 | /10 | /5 | ||||
area function |
/5 | /5 | /10 | /5 | ||||
move-shape function |
/5 | /5 | /10 | /5 | ||||
contains? function |
/10 | /10 | /20 | /10 | ||||
show-world function |
/10 | /10 | /20 | /10 | ||||
handle-key function |
/10 | /10 | /20 | /10 | ||||
Extra credit handle-key function |
/5 | /5 | /10 | /5 |
Following directions | /10 |
Writing contracts from word problems | /10 |
Choosing examples | /10 |
Choosing names | /10 |
Coding | /10 |
Code re-use and function composition | /10 |