On this page:
Turning In Homework
Stepping, Debugging Tip
From Structure Type Definitions to Data Definitions
Templates
From Templates to Functions
Structuring the World
Challenge Problems
Before you go..

Lab 2h Structures

home work!

Purpose The purpose of this lab is to practice the use and definition of structure types. Also, we will begin to act like program designers and software engineers instead of plain programmers.

Hands-on practice is the only way to drill mechanical programming skills, which are important to your success in understanding design skills in this class. Lab is one place to practice; the finger exercises are another one. If you are ever stuck with finger exercises, see your tutors, TAs, and instructors.

image

This is the honors version of Lab 2; if you’re looking for the normal version, click here!

Turning In Homework

To turn in (and check) your homework, follow the instructions on the Online Management page.

In order to check that everything is working properly:
  • If the plug-in is installed correctly, you will see a "CS 2500 Handin" button above your definitions window in DrRacket.

  • Do a mock submission to the test assignment. Create and save a file (containing what ever you like as long as it has no syntax errors), then click the "CS 2500 Handin" button. For pair submissions, enter both your and your partner’s usernames joined by "+", and enter either person’s password.

  • Select the correct assignment name (you’re going to use test today) from the drop down menu and click "CS 2500 Handin". DrRacket will report on whether your submission was successful. If your submission fails, carefully read the error message and correct your submission appropriately.

Stepping, Debugging Tip

TA: If students are getting stuck, work through this example.

If you’re stuck on a difficult error, copy just enough of your program into a new tab such that you can reproduce the error and use the Stepper to see where your program goes off track.

Stepping shows how structures are taken apart and how images are composed. The following program does not pass its test:
(define-struct shape (name sample))
; A Shape is a structure:
;  (make-shape String Image)
 
; data examples:
(define s1 (make-shape "triangle" (triangle 30 "solid" "blue")))
(define s2 (make-shape "square" (square 40 "outline" "red")))
 
(define BACKGROUND (rectangle 100 50 "solid" "green"))
 
; Shape -> Image
; place shape on a 100 x 50 green rectangle
 
(check-expect (render s1) (overlay (shape-sample s1) BACKGROUND))
 
(define (render s)
  (overlay BACKGROUND (shape-sample s)))
Show. Step through check-expect. Step w/o check-expect.

image

From Structure Type Definitions to Data Definitions

Pick partner

TA: Remind students of the basics of pair programming.

Remember structure type definitions from class:

(define-struct name (field ...))

What do structure type definitions do?

Exercise 1 List the functions that the following sample definitions define:
(define-struct photo (image tag))
(define-struct 3d (x y z))
(define-struct ta (last given lab))

Recall that a data definition for structured data states explains in a mixture of English and BSL how to construct elements of this class of data. In particular, it specifies what kind of data each field contains. Refer to Section 5.4 of HtDP2e if you need to review.

Exercise 2 Here are some data definitions:
(define-struct item (tag price))
; An Item is a structure:
;   (make-item String PositiveNumber)
 
(define-struct phd (name grant pay-rate))
; A PhD student is (represented by) a structure:
;   (make-phd String GrantId PositiveNumber)
; A GrantId is one of:
;  "1-123"
;  "3-AB4"
;  "9-999"
Create at least two data examples per data definition.

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

Define a structure type Animal for representing information about a zoo animal.

Now formulate a data definition for your structure type definition.

Exercise 4 The Zoo keeps track of each animal’s food in a structure that contains the name of the food and its experation date as strings. Create a structure type Food, and extend your definition for Animal to include the kind of food it eats.

Templates

Switch roles

What are templates? Again, see Section 5.4 of HtDP2e if you forgot.

Exercise 5 Construct a template for functions that process Items.

Exercise 6 Construct a template for functions that process PhDs.

Exercise 7 Construct a template for functions that process Animals.

From Templates to Functions

Let’s start with a couple of exercises on designing functions that work on the above structure types.

Exercise 8 Design the function re-assign. It consumes two pieces of data: a PhD student and a grant id. The result is a new PhD whose grant field contains id regardless of what was in there before.

Exercise 9 Design distance0. The function consumes an instance of 3D:
; A 3D is a structure:
;   (make-3d Number Number Number)
; interpretation: (make-3d a b c) represents a point in
; R3, the 3-dimensional space
It computes the distance of the given point to the origin of the space.

Hint Your math friend reminds you that the distance is computed as the square root of the sum of the squares of the coordinates.

Switch roles Templates are outlines for functions, and as the above exercises emphasize, they can be developed without knowing the purpose of the function. Let’s use the templates for Animals to code.

Exercise 10 When an Animal has a birthday, the zoo needs a function that takes an animal and returns a new animal with the same contents except with 1 added to its age.

Exercise 11 To ensure the safety of zoo visitors, design a function that consumes an Animal and the current hour and returns whether it’s mealtime.

Exercise 12 In preparation for next April Fool’s Day, the system manager of the zoo wants you to design a function that takes an Animal and returns a new Animal with the same data contents except with its age converted to dog years. Note: there are 7 dog years in 1 human year.

Okay, no one thinks the system manager is a good prankster but you are working for him, and you have no choice but to follow his orders.

Structuring the World

We’d like to create an animation where one image appears where a mouse clicks, and another image slowly moves from it’s original location to meet it.

Switch roles

Exercise 13 A World can be represented as a structure with two posns. The first posn represents the current position of the first image, and the second posn represents the current position of the second. Create this World, and find some images to use!

Exercise 14 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 first posn (the position of the first image) has been moved to the position of the mouse click, and the second posn remains unchanged. On any other mouse event, the original World is returned unchanged.

Have you been writing tests?

Exercise 15 Design a function tick to react to clock events. The purpose of the function is to gradually bring the two images together. The function consumes a World and produces a new World where each of the coordinates of the second posn are increased or decreased by one so that they get closer to the coordinates of the first posn.

Exercise 16 Design a function world-draw that consumes a World and returns a scene (big enough to fit your images) with your two images at the first and second posn. If your images overlap, have the second image appear over the first.

Exercise 17 Create an animation where you click the mouse to put your first image somewhere in the canvas, and then the second image moves along the canvas trying to reach the first. Start the second image wherever you want—hard code it into the initial World.

Challenge Problems

An animation studio is working on a game called "Chip, the Cheap Sheep". They have drawn up a few frames of animation, but they don’t know how to put them together into a working game.

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. Remember to use the design recipe when designing functions!

Exercise 18 Create a function named which-chip that takes a number and returns the corresponding image from the sequence above.

You should be able to drag the images from the webpage into DrRacket, once they are there you should know what to do. If not, save them to the desktop, and use "Insert">"Insert Image..." from the DrRacket menu bar.

Exercise 19 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 value named world0 that is your initial world (start by assuming the user clicked in the center and Chip is just offscreen).

Switch roles

Exercise 20 Define a function named draw-chip that takes a world and places the image of Chip’s current frame at his current coordinates into an empty-scene of size 400x400 (use your which-chip function!).

Exercise 21 Write a function named move-chip that takes a world and returns a world where Chip is moved to the left by some amount (it looks like he’s going pretty fast!).

This assumes he starts at the y-coordinate of his destination and only has to go left. When he gets to his destination, have him stop moving.

Exercise 22 Create a function named next-chip that takes a world and returns a new world incrementing the frame field and wrapping the number so it cannot be greater than 3 (you might find the remainder function useful, look it up in the Help Desk!).

Exercise 23 Now, make Chip respond to mouse clicks. Write a clack function that takes a world, x and y coordinates, and a mouse event and returns a new world. The new world will have the mouse’s coordinates as Chip’s new destination and Chip teleported offscreen (presumably at the same y-coordinate as his destination) so that he can run to the point the user clicked on.

Remember that mouse events are just strings. Be sure to ignore all mouse events except "button-down".

Exercise 24 Put all this together with tock and big-bang to see Chip the Cheap Sheep run!

(define (tock world)
  (move-chip (next-chip world)))
 
(big-bang world0
  (on-draw draw-chip)
(big-bang world0
  (on-draw draw-chip)
  (on-tick tock)
  (on-mouse clack)))

Before you go..

If you had trouble finishing any of the exercises in the lab or homework, or just feel like you’re struggling with any of the class material, please feel free to come to office hours and talk to a TA or tutor for additional assistance. We aren’t hungry zoo animals!