Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Today's Activities
- Prliminaries
- Create an L10 project with a program named l10.AddressBook. Download the files AddressBook.java and Contact.java into the l10 package.
- Open the data type class Contact and examine the code. It's a standard data-type class. It has instance variables, a constructor, getters, setters, and a toString method. There's nothing unusual about it.
- Open the program AddressBook, examine the code, then run the program. Enter a zero when it asks how many contacts you have:
- Simple Contact List
- -------------------
- This program reads in your contacts, then lets you search them.
- How many contacts do you have? 0<CR>
- For each contact, enter their name.
- Optionally add a phone or email.
- Your Contacts Are:
- Search your contacts.
- Leave the name blank to quit.
- Enter a name to search for: Young<CR>
- Enter another name (or leave blank to quit): Bob<CR>
- Enter another name (or leave blank to quit): e<CR>
- Enter another name (or leave blank to quit): x<CR>
- Enter another name (or leave blank to quit): <CR>
- We are going to make this program do some work.
- All the code you need to write is going to be in the program. Leave the data type class unchanged.
- Programming Activity 1
- First we're going to update the readContacts method so that it actually reads in the contacts. It will prompt the user for the name, phone and email of the contact. The user must enter a name, but may leave the phone and email fields blank. For example:
- Simple Contact List
- -------------------
- This program reads in your contacts, then lets you search them.
- How many contacts do you have? 5<CR>
- For each contact, enter their name.
- Optionally add a phone or email.
- Enter a contact's name: Mark Young<CR>
- Enter the contact's phone number (optional): 902-496-8153<CR>
- Enter the contact's e-mail address (optional): myoung@cs.smu.ca<CR>
- Enter a contact's name: <CR>
- You can't have a blank name!
- Enter a contact's name: David Young<CR>
- Enter the contact's phone number (optional): 902-555-1234<CR>
- Enter the contact's e-mail address (optional): <CR>
- Enter a contact's name: <CR>
- You can't have a blank name!
- Enter a contact's name: Robert Young<CR>
- Enter the contact's phone number (optional): 902-555-2345<CR>
- Enter the contact's e-mail address (optional): <CR>
- Enter a contact's name: Andrew Deputter<CR>
- Enter the contact's phone number (optional): <CR>
- Enter the contact's e-mail address (optional): andrew.deputter@nosuch.com<CR>
- Enter a contact's name: Bob McKenzie<CR>
- Enter the contact's phone number (optional): <CR>
- Enter the contact's e-mail address (optional): <CR>
- Your Contacts Are:
- Search your contacts.
- Leave the name blank to quit.
- Enter a name to search for: Young<CR>
- Enter another name (or leave blank to quit): <CR>
- Note that the program doesn't actually print out the contacts, or show you the people you searched for. Those parts come up in the other activities.
- Programming Activity 2
- Complete the implementation of the method printContacts. It merely prints out each element of the array it's given, one per line.
- Keep in mind that our Contact class has a toString method.
- After entering the data shown in the example above, you should see the following output for your contacts:
- Your Contacts Are:
- Mark Young // 902-496-8153 // myoung@cs.smu.ca
- David Young // 902-555-1234 //
- Robert Young // 902-555-2345 //
- Andrew Deputter // // andrew.deputter@nosuch.com
- Bob McKenzie // //
- Next we'll get that search working.
- Programming Activity 3
- Complete the implementation of the method searchContacts. It is already set up to keep reading search terms (partial names) until the user leaves it blank. What it needs to do is take the search term the user entered and search thru the array for contacts with that search term as part of their name. Every matching contact gets printed.
- Note that the method you need is called contains. You need to ask each contact's name wheter it contains the search term.
- Just ask the name. Don't look for the search term in the phone number or e-mail address.
- When you're done, you should be able to execute the searches:
- Enter a name to search for: Young<CR>
- Mark Young // 902-496-8153 // myoung@cs.smu.ca
- David Young // 902-555-1234 //
- Robert Young // 902-555-2345 //
- Enter another name (or leave blank to quit): Bob<CR>
- Bob McKenzie // //
- Enter another name (or leave blank to quit): e<CR>
- Robert Young // 902-555-2345 //
- Andrew Deputter // // andrew.deputter@nosuch.com
- Bob McKenzie // //
- Enter another name (or leave blank to quit): x<CR>
- Enter another name (or leave blank to quit): n<CR>
- Mark Young // 902-496-8153 // myoung@cs.smu.ca
- David Young // 902-555-1234 //
- Robert Young // 902-555-2345 //
- Andrew Deputter // // andrew.deputter@nosuch.com
- Bob McKenzie // //
- Enter another name (or leave blank to quit): <CR>
- Submit the program class. You will be graded on the following:
- readContacts prompts for and reads a name
- ... readContacts rejects empty names
- ... using the read-test-process-read idiom
- ... readContacts reads phone and email
- ... readContacts creates a Contact with the given information
- ... and places it properly in the array
- printContacts prints every element of the array it was given
- ... without doing more work than is necessary
- searchContacts visits every element of the array
- ... names that contain the search term are printed
- ... names that don't contain the search term are not printed
- ... no useless code included
- ... loop for reading search terms has not been broken
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement