CSC 171
Homework 4

Assigned Mar 3, due Mar 10

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. Develop the following functions on lists (some of which we've looked at in lab):
    1. contains-doll?, which takes in a list of strings representing toys, and returns a boolean indicating whether (at least) one of them is "doll".
    2. contains-even?, which takes in a list of numbers and returns a boolean indicating whether (at least) one of them is even. (Hint: You can use the built-in even? function to test whether a single number is even.)
    3. all-even?, which takes in a list of numbers and returns a boolean indicating whether all of them are even. (Hint: The base case for empty lists may not be what you think.)
    4. contains-matching-string?, which takes in a string and a list of strings, and returns a boolean indicating whether (at least) one of the strings in the list matches the specified string.
    5. count-even, which takes in a list of numbers, and returns how many of them are even.
    6. count-over-100, which takes in a list of numbers, and returns how many of them are over 100.
    7. count-matching-strings, which takes in a string and a list of strings, and returns how many of them match the given string.
    8. count-over, which takes in a number and a list of numbers, and returns how many of them are over the given number.
  2. Recall the exercise from homework 3 about shapes, which were defined to be either circles or rectangles. We'll continue where it left off.

    1. Develop a function show-shapes which takes in an image and a list of shapes, and superimposes all the shapes on that image at their respective locations and sizes. Assume all circles are solid and green, and all rectangles are solid and blue.

    2. Develop an animation in which the world is a list of shapes. For now, they won't move (that's homework 5). Every time the user presses the letter c key, a circle is added to the front of the list, at a random location with a random radius between 1 and 10. Every time the user presses the letter r key, a rectangle is added to the front of the list, at a random location with random width and height between 1 and 10. Every time the user presses the - key, the first shape in the list (i.e. the most recently-added one) is removed, unless there are no shapes, in which case nothing happens.

      (The letter-c key is #\c; the letter-r key is #\r; the - key is #\-. All three 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 Contract Examples Definition Test runs
contains-doll? function /5 /5 /10 /5
contains-even? function /5 /5 /10 /5
all-even? function /5 /5 /10 /5
contains-matching-string? function /5 /5 /10 /5
count-even function /5 /5 /10 /5
count-over-100 function /5 /5 /10 /5
count-matching-strings function /5 /5 /10 /5
count-over function /5 /5 /10 /5
show-shapes function /5 /5 /10 /5
Animation of list of shapes /15 /15 /30 /15

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:         /335


Last modified:
Stephen Bloch / sbloch@adelphi.edu