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.

  1. If it isn’t open already, open your U3L2-ContactList project.

  2. Implement the code for the the “Add Contact” button.

    1. 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 ask and wait 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.

    2. Make the script ask the user for contact data and then add that data to the contact list using the contact constructor.

      Collecting Data from the User

      The sprites’ questions might be covered up by the contact list watcher on the stage. You can use hide variable and show variable to hide the contact list 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 ask and wait and answer 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.

      contact with name: (name) address: (address) phone: (phone)

      But another way that makes for cleaner code is to use abstraction: create helper blocks (such as ask name and ask phone) 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 contact constructor.

      contact with name: (ask name) address: (ask address) phone: (ask phone)

      Each of these helper blocks will ask and wait the user a specific question (like, “What is the contact’s address?”) and then report the answer the user gives.

      Maybe even better, you could first build a more general ask () helper block (yet another abstraction) to use in the other helper blocks instead of rewriting the ask and wait and answer script every time. (You may have built a block like this in an earlier project.)

    3. Test and debug. Add a few made-up people to your contact list. 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
  1. Develop a “Clear List” button that resets the contact list to an empty list, which is essential while programming whenever you change the structure of the ADT.

    1. 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?

    2. Debug. Make sure this feature behaves as expected for either user choice.