On this page:
Turning In Homework
Stepping, Debugging Tip
From Structure Type Definitions to Data Definitions
Templates
From Templates to Functions
Structuring the World
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.

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!