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

Lab 2 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

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.

Explain to students that submissions will fail when they attempt to turn in a solution
  • in the wrong language,

  • that does not pass check syntax,

  • that contains lines wider than 102 characters.

Show them ;;, space bar, ctrl-u 99-, which inserts a comment of width 102; students are welcome to choose a narrower width. Also show them Edit | Find longest line.

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.

Templates

Switch partners

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

Exercise 4 Construct a template for functions that process Items.

Exercise 5 Construct a template for functions that process PhDs.

Exercise 6 Construct a template for functions that process Animals.

Stepping, Debugging Tip

TA: sync class after they have solved a couple of the following problems.

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.

From Templates to Functions

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

Exercise 7 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 8 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 partners 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 9 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 10 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 11 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.

Extra Problems

If you finish the problems before lab is over, tackle the finger exercises for problem set 2. If you get stuck, ask for help.

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!