Remember to name your Lab 3 program your-name-lab3.rkt
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? "sundaes" "sun"
) should return true
but (string-starts-with? "sun" "sundaes"
) should return false
.
Exercise 2 Design a structure for your favorite or least favorite animal (for now I'll call it your-fav-animal
) with at least 3 fields, one of which should be weight
. Construct three instances of your animal that have, of course, different characteristics, i.e. different field values. Write a function heaver-your-fav-animal
that returns the heavier of the two your-favorite-animals
arguments. Test it by utilizing your specific animal instances.
During this lab we’re going to create a working version of the small "door simulator" program in your text. Here’s how the program works:
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:
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.
Submit your work on Moodle. Remember to include your name and the date in the header and to name it your-name-lab3.rkt
.
Log out
When you are done, close DrRacket by choosing Quit
from the File menu, and then locate the logout option on the menu bar (lower right corner). Choose Logout…
and follow any remaining prompts. Always remember to log out when you are done using the system, to ensure that no one else uses your account.