Page 3: Adding Birthdays
Unit 3, Lab 2, Page 3
Purpose (do/learn; why this/here): Extend the ADT by adding a date
ADT, adapting the contact
ADT to accept it, and adding more helper blocks.
Can we cut all the commented out text on this page? –MF, 7/22/19
MARY: change the DATE ADT to use the correct ADT format. –MF, 5/23/20
BRIAN: as a low priority (that is, after ALL the solutions for all units and all optional projects are done), you want to add a “parse a date” TIF. –MF, 5/23/20
On this page, you will develop an abstract data type (ADT) to store and access dates, 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.
AAP-1.D.5
Notice that name, address, and text are strings, but birthdate is a list. You can combine elements of different types in a list.
-
Create an ADT to manage three data points for each date: month, day, and year.
-
Create a
date
constructor 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.
-
Create a
-
Integrate your
date
ADT into thecontact
ADT.-
Edit the
contact
constructor block definition to accept a birthdate as a list by using thedate
constructor.
-
Create a
birthdate from contact
selector to retrieve the contact birthdate from the inputted contact.
-
Adapt your “Add Contact” button to input this new type of contact into your list.
Adding Contacts with Birthdays
You could use several ask and answer blocks together with script variables.
Another way is to use abstraction and create another helper, that reports a list of the user’s answers to these prompts:
- What is the contact’s birth month?
- What is the contact’s birth day?
- What is the contact’s birth year?
-
Edit the
-
Debug. Clear your
contact list
, and add a new few contacts with birthdays. Fix any problems. -