Adding Contact Data

On this page, you will develop code for "Add Contact" and "Clear List" buttons in your contact list program.

  1. If it isn't open already, open your U3L2-ContactList project.
  2. Develop the "Add Contact" button for the user.
      CRD-2.C
    1. Looking at the previous page, write down the questions your program is going to ask the user. The answers to these questions will be the program input.
    2. : Input and Output
      CRD-2.C.1, CRD-2.C.4, CRD-2.C.6, CRD-2.D.1, CRD-2.D.2

      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.

    3. Make the script ask the user for contact data and then add that data to the contact list using the contact constructor.
    4. Collecting Data from the User

      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 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)
      • 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 their answer to the contact constructor.
        contact with name: (ask name) address: (ask address) phone: (ask phone)
        Each of these helper blocks will ask the user a specific question (like, "What is the contact's address?") and then report the user's answer.

      You could first build a more general answer to prompt () helper block (yet another abstraction) to use in the other helper blocks instead of rewriting the ask and answer script every time.

    5. Test and debug. Add a few made-up people to your contact list. Make sure everything works.
  3. 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

  4. 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 ask the user, "Enter c to clear the list, or enter anything else to cancel."
    2. Debug. Make sure this feature behaves as expected for either user choice.
  5. Save your work
Most of the programs you've written have one script that's in charge, using loop blocks to keep running either forever or just for a long time. This project is different: Nothing happens until you click a button (recall from Unit 1 that this is called an event), which starts a script that just runs briefly, to do whatever action the button requires. This kind of program structure is called event-driven programming.
  1. The sprites' questions might be covered up by the contact list watcher on the stage. Use hide variable and show variable to hide the contact list watcher until the end of each sprite's script.

In this activity, you're going to refine the handling of people's names. When you look up a particular person, you want to see their name as you've been seeing it on this page, first name first: "Alphie Preston." But suppose you want to sort your contacts. Typically that's done by last name, so the list would look something like

So you need to be able to show a name first name first for ordinary display, but last name first for sorting.

Before you begin, save your project again, under the name U3L2-TIF. This will keep your work on these problems separate from the regular problems.

  1. The first step is to create an abstract data type for names. You'll need a constructor that takes first name and last name as inputs, and reports a list of them to represent the name part of a contact.
  2. Reading a name: The next step is to modify the program you wrote above so that it asks separately for the first name and the last name, constructs a name (that is, a list) using the constructor you just wrote, and uses that list as the name input to the contact with name block. This is an abstract data type (name) inside another abstract data type (contact).
  3. Writing a name: Your name from contact block will now report a list, as it should. But some other parts of the program, such as the part that displays a contact to the user, want to display the name as a text string: "Betsy Anderson." The part that sorts the contact list also wants a string: "Anderson, Betsy." Write two blocks name, display form, from contact 'list input slot and name, sort form, from contact 'list input slot that join the two parts of the name with a space and/or a comma as needed.
This three-step process (create an ADT, use it to read values of that type, use it to write values of that type) is very common in programming. You'll follow the same three steps again in the Take It Further on page 4.

Save your work. On page 4 you're going to find a contact by letting the user enter any part of the name, and display the entire contact. Use the name abstraction you've just created when you do that.