7.5

### Lab 2 Designing Functions, Data, and Programs

Purpose: The purpose of this lab is to practice designing functions, data definitions, and larger programs.

References: Chapter 3: How To Design Programs Chapter 4: Intervals, Enumerations, and Itemizations Design Recipe (course webpage)

### Designing Data

Recall from lecture that there are four steps of the data design recipe: definition, interpretation, examples, and template. In the following exercises we ask you to design data for several scenarios which means you should follow all four of these steps.

Exercise 1 The city council is updating the program that controls the traffic lights. They have asked for your help designing data to represent a traffic light. A traffic light can be red, yellow, or green.

Exercise 2 It is 1620 and Cornelius Drebbel has invented a new device. It’s like a boat but it goes underwater! For the first 100 meters the ship will sink at a rate of 5 meters/second. For the next 200 meters the ship will sink at a rate of 4 meters/second. And then, for the next 100 meters it will sink very carefully at a rate of only 2 meters/second. After that Cornelius has decided the ocean is too dangerous to traverse. Design data to represent a sea level. Your data definition should account for the fact that Cornelius’ ship will have different behavior at different levels.

### Designing Functions

Recall from lecture that there are four steps of the function design recipe: signature, purpose statement, tests, and code (which has to follow the template). In the following exercises we ask you to design functions which means you should follow all four of these steps.

Exercise 3 Using your traffic light data definition from above, design the function next-light which produces the next color in the sequence. Recall that a traffic light goes from green to yellow, yellow to red, and from red back to green again.

Exercise 4 Using your sea level data definition from above, design the function move-ship which takes in the current depth of Cornelius’ ship and produces its new depth after 1 second of travel. Be sure to refer to the description above to check that your ship moves at the correct rate in each part of the ocean.

### Designing Programs

During this portion of the lab we’re going to design a small "door simulator" program. Here’s how the program works:
• A door can either be open, closed, or locked. Initially your program will take in a representation of one of these states.

• The user can open a closed door by pressing the "o" key on their keyboard. You cannot open a locked door, and attempting to open an already open door will do nothing.

• The user can close an open door by pressing the "c" key on their keyboard. Attempting to close an already closed (or closed and locked) door will do nothing.

• The user can lock a closed door by pressing the "l" key on their keyboard. Attempting to lock an open door or an already locked door will do nothing.

• The user can unlock a locked door by pressing the "u" key on their keyboard. Attempting to unlock a closed door that is already unlocked, or an open door, will do nothing.

When designing this program we’re going to ask a series of questions. You should ask yourself these questions whenever you are designing a program using big-bang.

Step 1: What stays the same?

One thing that stays the same in this program (and many others) is the images. We can define images of open, closed, and locked doors to use throughout our program. Here are some examples of what those images might look like:

Exercise 5 Insert these images into your program and define them as constants. Use names that tell you something about the image such as DOOR-CLOSED-IMAGE.

Step 2: What changes?

The only thing changing in this program is the state of the door: whether it is open, closed, or locked. Remember that we want to separate data from the images of that data, so we can’t just use the images from the previous step as our data. A good way to keep track of the state of the door would be the following data definition:

 ; A DoorState is one of: ; - "closed" ; - "locked" ; - "open"

Step 3: Which handlers do we need?

As always we will need a to-draw clause. Based on the explanation of the program, it changes when we press a certain key. What does that tell us about the handlers we need?

Exercise 6 Write down the signatures of the handler functions you will need. If you are having trouble you can take a look at the documentation and if you are still confused, feel free to ask a staff member to help.

Step 4: Main Function

Now that we’ve decided on our handler functions, we can put together our main function which will call big-bang. We write this function first because in this course we follow a top down programming approach. That means larger functions go at the top of our program and smaller functions go underneath.

Exercise 7 Design the main function door-simulator which, given an initial DoorState, runs the door simulation using big-bang. Recall that we cannot test functions that use big-bang because we don’t know when the user will stop the program or how they will interact with it. However, you should follow all the other steps of the design recipe for functions.