Page 2: Adding Contact Data
Unit 3, Lab 2, Page 2
Like the flashcard project, this program is event-driven. There’s no one script that runs when the user clicks the green flag and keeps running forever or until the program is done. In this project nothing happens until the user clicks a button (an event) which starts a script that runs briefly, to do whatever action the button requires.
On this page, you will develop code to make the “Add Contact” and “Clear List” buttons in your contact list program work.
If it isn’t open already, open your U3L2-ContactList project.
Implement the code for the the “Add Contact” button.
Looking at the contstructor you defined on the previous page, write down the questions your program is going to ask the user. The answers to these questions will be the input you need to add a contact.
Input and Output
We use “input” loosely to mean the empty boxes in a block that get filled with values. But input also means information entered into a program by the user, as in the
block. Program input can also come from data tables, sounds, pictures, video, or other programs.Similarly, program output means any data sent from your program to the user or to any device. Typically, the output depends on the input.
Make the script ask the user for contact data and then add that data to the
using the contactconstructor.Collecting Data from the User
The sprites’ questions might be covered up by the
watcher on the stage. You can use
and
to hide the
watcher until the end of each sprite’s script.You’ll want to ask the user for each piece of data (contact name, address, and phone number) separately.
You could use several
and
blocks together with script variables to request and then store each piece of user input until you are ready to report them all together using contact.
But another way that makes for cleaner code is to use abstraction: create helper blocks (such as
and
) that each ask the user for one piece of data and report the answer. Then you can use those reporters directly as the inputs to the contactconstructor.
Each of these helper blocks will
the user a specific question (like, “What is the contact’s address?”) and then report the
the user gives.Maybe even better, you could first build a more general
helper block (yet another abstraction) to use in the other helper blocks instead of rewriting the
and
script every time. (You may have built a block like this in an earlier project.)Test and debug. Add a few made-up people to your
. Make sure everything works.
You can use these examples or make up your own:
| name | address | phone |
|---|---|---|
| Betsy Anderson | 123 Main St. #4, New York, NY 10001 | 212-555-1234 |
| Alphie Preston | 149 E. 16th Ave., Sunnyvale, CA 94089 | 408-555-6789 |
| Gamal Abdel | 369 Center St., Boston, MA 02130 | 617-555-1098 |
Develop a “Clear List” button that resets the
to an empty list, which is essential while programming whenever you change the structure of the ADT.Make the script ask the user for confirmation (so the user won’t accidentally delete their list).
For example, clicking, “Clear List” might prompt the user, “Enter c to clear the list, or enter anything else to cancel.” Can you write a predicate block that asks the user a question and then reports true or false depending on what they answer and use it for this?
Debug. Make sure this feature behaves as expected for either user choice.