CS2500 Lab 2 (honors)

Electronic homework submission

Read this page to learn how to submit your assignments electronically. Assignments will only be accepted electronically from this day forward! Repeat this from your laptop or desktop at home if you wish to submit from it.

Pair programming

In this lab, we will practice pair programming again. You will be working with your partner from last week. Like last time, each pair should only be working on one machine. Remember, in pair programming, one member of the team is the pilot and the other is the co-pilot. The pilot does the typing but the co-pilot drives the process. Even though the pilot is the only one typing, both partners should be active in trying to come up with solutions to the exercises. Make sure to switch roles when indicated in the lab!

Don't delete your work

In previous labs some students deleted their solutions for exercises after completing them—don't do this! It is common for exercises to make use of functions or templates defined in earlier exercises.

Structure definitions

For each of the following problems, extract structure definitions for the data involved. You don't need to write tests for them, but do test them informally by trying them out.

Remember structure definitions from class? (define-struct name (field ...))

  1. The Boston Zoo keeps track of information for every animal that is kept there. For each animal, they store its name, species, age, breakfast hour, and dinner hour (if they don't get fed twice a day, they try to eat the visitors...).
  1. Each Zoo attendant has a name, and is assigned to watch over exactly three different animals.

Switch Roles

  1. In addition to watching over animals, the attendants must clean a zone of the Zoo. The Zoo is divided into three zones: Birds, Reptiles, and Mammals. For each Zone, they track the cleaner's name, and the number of animals kept there. Hint: You probably need more than one structure here.

Data definitions

  1. Take your first two structure definitions from above and turn them into data definitions. Recall that a data definition for structured data specifies what kind of data each structure field contains. Refer to Section 6.4 of HtDP if you need to review!

Switch Roles


  1. Construct a template for each your data definitions. Recall that the template describes what we know about the input to a function (what accessors we can use on them). For each expression in your template, record the kind of data to which it evaluates. If you need to review templates for compound data, look in Section 6.5 of HtDP.

From templates to functions

Consider the templates you built from the data definitions and design the following functions based on them. Be sure to formulate examples and tests!
  1. Using your data definition, structure definition, and template from above, develop a function that updates an animal's age by adding 1 to it. (This will be useful on the animal's birthday).
  1. Using your data definition, structure definition, and template from above, develop a program that takes an animal and the current hour, and returns whether it's time for it to eat.

Switch Roles

  1. In preparation for next April Fool's Day, the system manager of the Zoo wants you to design a function that takes an animal description and returns the same structure with years converted to dog years. (There are 7 dog years in 1 human year.)
  1. Using your data definition, structure definition, and template from above, develop a program that determines the total age of the animals a given attendant has been assigned to.

“DR uber alles”

From now on, we will always use the design recipe when designing programs.

Structures and unions

In the following exercises, give names to each example so you can reuse them later.

  1. A studio album has a name and a year of publication. A live album has a name, a year of recording and a year of publication. A music album is either a live or a studio album. Write a (single) data definition to describe a music album. Produce two examples of a music album, one for each kind. Write a template for a function that consumes a music album.
  1. There are two kinds of unpublished music albums from the perspective of a music label: those that are completed yet and those that aren't. Both completed and uncompleted albums have a serial number, but those albums that are completed also have a name and a release date. Design a data definition to describe an unpublished music album. Produce two examples of an unpublished music album, one for each kind. Write a template for a function that consumes an unpublished music album.

Switch Roles

  1. Develop a function that consumes an unpublished music album, a date of release and a name. If the album is not completed it constructs an instance of a completed album using the information given as input to the function. If the album is already completed, return it unchanged.

Structuring the World

  1. In the following exercises, the World should be represented as a struct with two posns. The first posn represents the current position of a blue circle, and the second posn represents the current position of a red circle. When the user clicks the mouse the red circle will immediately move to where they clicked, and over time the blue circle will move to meet it.
  1. Design a function mouse-click to react to mouse events. It consumes a World, two numbers (x and y coordinates), and a MouseEvent as described in on-mouse. When the MouseEvent is "button-down", this function should return a new World where the second posn (the red circle) has been moved to the position of the mouse click, and the first posn (the blue circle) remains unchanged. On any other mouse event, the original World is returned unchanged.

Have you been writing tests?

  1. Design a function tick to react to clock events. The purpose of the function is to gradually bring the two circles together. The function consumes a World and produces a new World where each of the coordinates of the first posn are increased or decreased by one so that they get closer to the coordinates of the second posn. For example if the original World is (1,3),(5,1) then your function should return the new World (2,2),(5,1).

Switch Roles

  1. Design a function world-draw that consumes a World and returns a 300 × 300 scene with a solid blue circle of radius 15 at the position represented by the first posn and a solid red circle of radius 10 at the position represented by the second posn. The red circle should appear on top of the blue circle if they overlap.
  1. Create an animation where you click the mouse to put a red circle somewhere in the canvas, and then a blue circle moves along the canvas trying to reach the red circle. Start the blue circle wherever you want—hard code it into the initial World.
Depending on how much time is left, choose one of the following two problems:

The little challenge problem

This exercise asks you to modify the animation you made earlier, and provides some guidance for doing so. If you don't finish it in lab, it's a good idea to complete it at home.
  1. Extend the definition of the world to include a symbol that is one of "red", "yellow", "green". Modify world-draw to use this symbol when drawing the circle for the mouse. Write a function cycle-color to cycle through the colors ("red""yellow", "yellow""green", "green""red"). Use this function and modify mouse-click to cycle the color when the user clicks.

The big challenge problem

Some of the TAs (oddly, they've asked to remain anonymous) are working on a game that they call Chip, the Cheap Sheep. So far, they've put together a few frames of animation for it:

0 1 2 3

Your goal is to create a simple proof-of-concept game engine: Chip will run from offscreen to the point the user clicks on.

  1. Create a function which-chip that takes a number and returns the corresponding image from the sequence above. (You can either drag the images from this page into DrRacket, or save them and use Insert > Insert Image... from the DrRacket menu bar.)

    This can be done several ways... which do you think is best?
  1. Write a data and structure definition for your World. You'll want to be able to know Chip's coordinates, the coordinates he is running to, and which frame of the animation he is currently on.

    While you're at it, write a template for functions that take a world, and define a variable world0 that is your initial world. Start as if the user clicked in the center and Chip is just offscreen.
  1. Write a function move-chip that takes a World and returns a World, moving Chip to the left by some amount. (It looks like he's running pretty fast!)

    This assumes he starts at the y coordinate of his destination, and only has to go left. If gets to his destination, have him stop moving.
  1. Now, make Chip respond to mouse clicks. Write a mouse-click function that, in response to a mouse event, creates a new World, with the mouse coordinates as Chip's new destination, and with Chip teleported offscreen (presumably at the same y coordinate as his destination), so that he can run to the point the user clicked on. Be sure to only react to the mouse events "button-down".
  1. Write a function draw-chip that draws the correct frame of Chip into an empty scene of size 400 × 400.
  1. Write a function next-chip that increments the current frame in the world, wrapping the number so it stays within the range 0–3:

    0 → 1 → 2 → 3 → 0 → 1 → 2 → 3 → 0 → 1 → 2 …

    You might find the remainder function helpful.
  1. Write a function tick that takes your World and calls both move-chip and next-chip to update the World.
  1. Put all this together with a big-bang and see Chip the Cheap Sheep run!
      (big-bang world0
                (on-tick tick .1)
                (to-draw draw-chip)
                (on-mouse mouse-click))

If you have extra time, play around. Make a bouncing ball for Chip to chase. Be creative.