On this page:
Simulating Doors
In the Land of Submarines
Before You Go...
8.1

Lab 2 Conditional Data

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

Submission: Submit your solutions Exercises 1 and 2 to handins. These exercises will be graded on effort. This is due by Saturday 1/29 at 11:59pm.

Simulating Doors

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.

Here are some images of the three different states to get you started:

open door closed door locked door

Exercise 1 Design a data definition for the state of the world. Be sure to account for all of the cases that can occur and follow all the steps of the data design recipe.

Exercise 2 Design your world program. Remember to refer to the textbook for the steps of designing world programs. When designing complex functions remember that there should be only one task per function. If you are doing multiple tasks, you should design some helper functions!

In the Land of Submarines

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.

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

Exercise 4 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.

Exercise 5 Design a world program that, given the initial depth of a ship (in meters), shows an animation of one of Cornelius’ ships sinking down through the ocean. The ship should descend at the rates specified by Cornelius. Your program should show the different depths as different colors since the ocean gets darker as you descend. Your program should end when the ship reaches the bottom of the safely traversable ocean.

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!