CSC 171
Homework 3

Assigned Feb 24, due Mar 3

Turning in animation assignments

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).

Programming

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.

  1. 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:

    1. perimeter : shape => number
      Note: the perimeter of a circle is 2 π r; the perimeter of a rectangle is 2 (width + height). For π, you can use the approximation 3.14, or the built-in DrScheme variable pi (which is still only an approximation, but it's correct to about twelve decimal places).
      Also note: a 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.
    2. area : shape => number
      Note: the area of a circle is πr2; the area of a rectangle is width * height.
    3. move-shape : shape number (dx) number (dy) => shape
      Produces a shape just like the old one but moved by dx in the x dimension and dy in the y dimension.
    4. contains? : shape posn => boolean
      Tells whether the posn is strictly inside the shape.
      Hint: you may need one or more auxiliary functions, and you'll need a lot of test cases.
      Another hint: To tell whether a 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.)

Grading standards

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

General skills:

Following directions /10
Writing contracts from word problems /10
Choosing examples /10
Choosing names /10
Coding /10
Code re-use and function composition /10

Total:         /450


Last modified:
Stephen Bloch / sbloch@adelphi.edu