Page 3: Adding Birthdays
Unit 3, Lab 2, Page 3
On this page, you will develop new abstract data types (ADT) to representdates values, and you will adapt your contact ADT to use it to store each contact’s birthday.

As we add more complexity to contacts—birthday, work address, and so on—the location of each data point (such as the phone number) inside a contact might change. This is where an abstract data type helps. If you were to move the phone number as you refine your contact ADT, you could fix every single block that uses the phone number, or if you have an ADT, you could just fix the one selector that chooses the phone number from the list. With that one change, all the other blocks using that selector will get the data they need automatically.
Notice that name, address, and text are strings, but birthdate is a list. You can combine elements of different types in a list.
If it isn’t open already, open your U3L2-ContactList project.
Create an ADT to manage three values for each date: month, day, and year.
Create a
dateconstructor that accepts this data as input and then reports a date as a list of three items.

Write three selector blocks to retrieve the specific data (month, day, or year) from the inputted date.

Debug any problems with the constructor or selectors.
Integrate your
dateADT into thecontactADT.Edit the
contactconstructor block definition to accept a birthdate as a list by using thedateconstructor.

Create a
birthdate from contactselector to retrieve the contact birthdate from the inputted contact.

Adapt your “Add Contact” button to input this new type of contact into your list.
As when you originally prompted the user for the information to make a contact, you could use several script variables and
and
blocks.Or you can continue the pattern of creating an abstraction to take care of asking the user for a birthdate: a
block that reports a list constructed with the dateconstructor with the user’s answers to these prompts as inputs:What is the contact’s birth month?
What is the contact’s birth day?
What is the contact’s birth year?
Then you can use
as just another reporter than provides an input to the contactconstructor:
Debug. Clear your
, and add a new few contacts with birthdays. Fix any problems.
Take It Further…
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 in this lab, 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
Abdel, Gamal
Anderson, Betsy
Preston, Alphie
So you need to be able to show a name first name first for ordinary display, but last name first for sorting.
The first step is to create yet another abstract data type, this time to represent 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.
Prompting the user for a name: The next step is to modify the program you wrote on page 2 so that it asks the user separately for the first name and the last name, and then constructs a name using the constructor you just wrote, and uses that value as the name input to the
contactconstructor. This you will have a value of one abstract data type (name) as one of the values inside the value of another abstract data type (contact). If you have already written an
block, then you just need to change that block to ask separately for the first and last name and then contstruct and report a name value and your original code to construct contacts will still work.Displaying a name: Your
name from contactblock will now report a name value, i.e. 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
and
that join the two parts of the name with a space and/or a comma as needed.
This three-step process of creating an ADT, then using the constructor to make values of the new type, and then using the selectors to implement code that uses the parts of the ADT values in different ways should be very familiar by now. You did it for contacts on pages 1 and 2, and again for dates on this page.