On this page:
Design Recipe
World Programs
Before You Go...
Stretch Goals:   Yellow Submarine
6.6

Lab 2 World Programs, and Design

lab!

Purpose: The purpose of this lab is to practice designing and implementing a small world program with enumerated data.

Textbook references: Chapter 3: How To Design Programs Chapter 4: Intervals, Enumerations, and Itemizations

Design Recipe

Exercise 1 Design the function string-starts-with? which takes two Strings and returns a Boolean indicating whether the first string begins with the second. Be sure to follow all the steps of the design recipe for functions. When you are testing your function, make sure you test the case where the first string is shorter than the second. For example (string-starts-with? "fundies" "fun") should return true but (string-starts-with? "fun" "fundies") should return false.

Exercise 2 Design the function optimum which takes three numbers: a, b, and c. It uses these numbers to find the maximum or minimum of the quadratic equation with the given coefficients. To do this you will need to use the following formula: optimum = (-b)/(2a). You should produce a reasonable value when a = 0 (since dividing by zero will give you an error in DrRacket).

World Programs

During this 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. Note that these steps break with our usual convention of top down programming because the larger program is at the bottom instead of at the top. However, you should continue to design your individual functions using top down programming as much as possible.

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:

open door closed door locked door

Exercise 3 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 4 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: Design your handlers

Exercise 5 Design the function that draws the DoorState. Which handler does this belong to? Remember to follow the design recipe for functions.

Exercise 6 Design the function which changes the DoorState based on user input. Which handler does this belong to? Remember to follow the design recipe for functions.

Step 5: Put it all together!

Exercise 7 Design the function door-simulator which, given an initial DoorState, runs the door simulation using big-bang. Remember to follow the design recipe for functions. Give it a try! Be sure you can’t lock an open door or open a locked door.

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 love to teach and you will learn. It’s symbiotic!

Stretch Goals: Yellow Submarine

It is 1620 and Cornelius Drebbel has invented a new device. It’s like a boat but it goes underwater! He wants to show everyone his remarkable invention so they know how great it is. He is asking you to design a program that shows a ship sinking down under the waves. 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.

Step 1: What stays the same?

Exercise 8 Define three constants, TOP-SPEED, MID-SPEED, and LOW-SPEED which define how many meters/second Cornelius’ ship can move at each depth.

Exercise 9 Define any other constants you might need for your program. Keep in mind that you will need to show the different ocean depths in different colors.

Step 2: What changes?

Exercise 10 Design a data definition for a SeaLevel. This data definition should encompass the three non-overlapping adjacent intervals mentioned above. We will use this data definition as the world state for our program. It will allow us to determine which speed the ship should be moving as it descends through the murky depths of the ocean. Remember to follow all the steps of the data design recipe.

Step 3: Which handlers do we need?

Exercise 11 Write down the signature and purpose statement for each handler function you will need. Every big-bang program needs a to-draw clause. What other clause will be useful to change the location of the ship as time passes? Which clause will help us stop the program when the ship reaches the bottom of the traversable ocean?

Step 4: Design your handlers

Exercise 12 Design the function that draws the ship at the correct location on the screen. Your program should show the entire traversable ocean, with the different depths as different colors since the ocean gets darker as you descend. Which handler does this belong to? Remember to follow the design recipe for functions.

Exercise 13 Design the function that moves the ship as time passes. The ship should descend at the rates specified by Cornelius. Which handler does this belong to? Remember to follow the design recipe for functions.

Exercise 14 Design the function which determines whether or not the program should stop. Your program should end when the ship reaches the bottom of the safely traversable ocean. Which handler does this belong to? Remember to follow the design recipe for functions.

Step 5: Put it all together!

Exercise 15 Design the function sinking-ship which, given the initial state of Cornelius’ submarine, uses big-bang to show the animation of the ship descending through the ocean. Remember to follow the design recipe for functions.