1.Implement the class Contact which will be used to save basic information about a contact.
2.Implement a utility class PhoneBookUtils which includes static methods to be used by the PhoneBook class when manipulating the contact list.
3.Implement the class PhoneBook, which uses a map data structure to maintain a list of contacts, their emails and their phone numbers.
4.Implement the private inner class PhoneBookIterator which implements Iterable.
5.Students in the honors section will have an additional class to implement for full credit.
6.Write test cases to test each method in the four classes you have implemented.
7.Download and use the tester module to ensure that your program is correct.
8.Prepare the assignment for submission and submit it.
For this project, we will implement an electronic phone book. Like in previous projects, we will build from the ground up by making the phone book classes. We will build up to a tester program which populates the contact list (by reading from an input file) and implement the different methods to check the validity of the application. We will incorporate two more programming techniques into this project: maps (as data structures and recursion.
We will use a HashMap (from the Collections Framework) to store contact information. If we just wanted a list, we could use ArrayList, but by using a map data structure we can maintain a mapping between a key and a value. The key would be a contact's name, while a value entry will contain other information related to the contact (email/phone). Remember that a map does not allow duplicate keys and therefore we get the guarantee that there is only one entry per contact in our data structure. We will also implement recursive algorithms for sorting the list of contacts and also when searching for a contact (recursive insertion sort/binary search).
Finally, in this project, you are expected to write your own test methods to test your code! We will provide you with a sample tester program which you can use for the purpose of testing your own methods. Make sure to write enough test cases for the different scenarios for each method. For instance, what would happen if we try to add a duplicate contact to the hash map? Or what would happen if we invoke the getContactList on an empty list? These border cases must be covered when you are testing your code. Be sure to test your code thoroughly.
RULES
public class contact implements Comparable< contact > : This class maintains information for a single contact, namely the following 3 instance variables are defined: name, phone and email, all of type String. Note that this class will be used when defining an entry type for the hash map used in the PhoneBook class. Furthermore, this class implements Comparable which makes instances of this class to be comparable against others. This is useful when we need to call any of the generic methods in the Collection framework which requires a type that is comparable, such as the static method Collections.sort defined in the Collections utility class. Additionally, this class will provide the following public methods:
> Contact c = new Contact ("Arthur Moddy", "est@temp.com", "703-555-5555");
> System.out.println(c); Arthur Moddy, email: est@temp.com, phone: 703-555-5555.
public final class PhoneBookUtils : this is a utility class, which means that we do not intend to instantiate it at all. Instead, we will use the static helper methods it provides while writing other classes. Examples of other utility classes are java.lang. Math, java.util.Arrays, or even the class we wrote in the first project. Utility classes such as this one are often declared final such that they cannot be subclassed. We will use it to define generic methods with the idea that we could reuse the same methods across a number of different applications.
In our phone book application, the utility class implements the following static methods:
> import java.util.*;
> Map< String, Integer > map = new HashMap< String, Integer >();
> map.put("x",1); map.put("y",2); map.put("z",3);
> map {x=1, y=2, Z=3}
> System.out.print (PhoneBookUtils.mapToString (map));
1
2
3
> Integer[] ints = { 3, 8, 5, 4, 1, 9, -2 };
> List< Integer > intList = new ArrayList< Integer > (Arrays.asList (ints));
> System.out.println(java.util.Arrays.toString (ints));
[3, 8, 5, 4, 1, 9, -2]
> System.out.println(intList);
[3, 8, 5, 4, 1, 9, -2]
> System.out.print (PhoneBookUtils.listToSortedList (intList));
-2
1
3
4
5
8
9
> System.out.println(intList);
[-2, 1, 3, 4, 5, 8, 9]
> PhoneBookUtils.insertionSort (ints, 0);
> System.out.println(java.util.Arrays.toString (ints));
[-2, 1, 3, 4, 5, 8, 9]
> System.out.println(PhoneBookUtils.binarySearch (ints, 0, ints.length, 9));
9
> System.out.println (PhoneBookUtils.binarySearch (ints, 0, ints.length, 45));
null
> String[] strs = { "Hhh", "CCC", "Fff", "Ttt", "Aaa", "Ddd" };
> List< String > strList = new ArrayList< String > Arrays.asList (strs));
> System.out.println(java.util.Arrays.toString (strs));
[Hhh, Ccc, Fff, Ttt, Aaa, Ddd]
> System.out.println(strList);
[Hhh, Ccc, Fff, Ttt, Aaa, Ddd]
> System.out.print (PhoneBookUtils.listToSortedList (strList));
Aaa
Ccc
Ddd
Fff
Hhh
Ttt
> System.out.println(strList);
[Aaa, Ccc, Ddd, Fff, Hhh, Ttt]
> Phone BookUtils.insertionSort (strs, 0);
> System.out.println(java.util.Arrays.toString (strs));
[Aaa, Ccc, Ddd, Fff, Hhh, Ttt]
> System.out.println (PhoneBookUtils.binarySearch (strs, 0, strs.length, "Fff"));
Fff
> System.out.println (PhoneBookUtils.binarySearch (strs, 0, strs.length, "Bebe"));
null
public class PhoneBook implements Iterable< contact > : Our phonebook class stores a list of contacts using a private HashMap data structure. As discussed in the Overview section, the hashmap would hold values ("entries") which are Contact objects, referenced by keys which are the lowercase version of the contact's name. This class provides the required functionality for maintaining the list of contacts: adding a new contact, updating a contact information, removing a contact, generating a list of all contacts, and sorting contacts by last name. This class can also read a list of contacts from an input file and can create an iterator to the list to be used when traversing the contact list. The PhoneBook class makes use of the available static methods which are available in the PhoneBookUtils class. Here is an example of how this class works:
> Phone Book blackbook = new PhoneBook();
> blackbook.addContact ("Lance Farmer", "sem.egestas@ctus.ca", "1-425-180-9073");
> blackbook.addContact ("Baxter Cantu", "arcu.iculis@iaculis.net","1-606-427-1676");
> blackbook.addContact ("Arthur Moody", "est@temporerat.com", "1-170-451-6998");
> blackbook.addContact ("Petra Wiley", "Morbi.m@duiquis.com", "1-357-588-7644");
> blackbook.addContact ("Forrest Beach", "vulpute@Nuldum.co.uk", "1-117-275-2165");
> blackbook.getContact Info ("Arthur Moody")
"Arthur Moody, email: est@temporerat.com, phone: 1-170-451-6998."
> blackbook.getEmail("Forrest Beach")
"Forrest Beach: vulpute@Nuldum.co.uk."
> blackbook.getPhone ("lAnce FaRmEr") // note capitalization
"Lance Farmer: 1-425-180-9073."
> System.out.println (blackbook.getEmail ("Donald Trump"));
null
> System.out.println (blackbook.getContactList());
Forrest Beach, email: vulputate@Nullainterdum.co.uk, phone: 1-117-275-2165.
Baxter Cantu, email: arcu.iaculis@iaculis.net, phone: 1-606-427-1676.
Petra Wiley, email: Morbi.metus@duiquis.com, phone: 1-357-588-7644.
Arthur Moody, email: est@temporerat.com, phone: 1-170-451-6998.
Lance Farmer, email: sem.egestas@urnanecluctus.ca, phone: 1-425-180-9073.
> System.out.println (blackbook.getsortedContactList())
Arthur Moody, email: est@temporerat.com, phone: 1-170-451-6998.
Baxter Cantu, email: arcu.iaculis@iaculis.net, phone: 1-606-427-1676.
Forrest Beach, email: vulputate@Nullainterdum.co.uk, phone: 1-117-275-2165.
Lance Farmer, email: sem.egestas@urnanecluctus.ca, phone: 1-425-180-9073.
Petra Wiley, email: Morbi.metus@duiquis.com, phone: 1-357-588-7644.
> System.out.println (blackbook.getsortedContactListAlt())
Arthur Moody, email: est@temporerat.com, phone: 1-170-451-6998.
Baxter Cantu, email: arcu.iaculis@iaculis.net, phone: 1-606-427-1676.
Forrest Beach, email: vulputate@Nullainterdum.co.uk, phone: 1-117-275-2165.
Lance Farmer, email: sem.egestas@urnanecluctus.ca, phone: 1-425-180-9073.
Petra Wiley, email: Morbi.metus@duiquis.com, phone: 1-357-588-7644.
> System.out.println (blackbook. searchContactList ("Petra Wiley"))
Petra Wiley, email: Morbi.metus@duiquis.com, phone: 1-357-588-7644.
All methods which search for a contact by name (including all getters and setters) should be case insensitive, meaning that the method should first convert the name parameter to lowercase before searching for it in the hashmap. This class will have the following public methods:
Lance Farmer
sem.egestas@urnanecluctus.ca
1-425-180-9073
Lawrence Garcia
diam@amagna.ca 1-665-672-6398 Petra Williamson euismod.in@convallisligulaDonec.co.uk
1-352-344-9237
public class PhoneBookIterator< E > implements Iterator< E >: An iterator implementation in Java is just a class which implements the Iterator interface. Since we are going to use this class inside a PhoneBook we make this class an inner class (or nested class), which just means that we define it inside of the PhoneBook class. We may make inner classes public or private, but for this project we will use public so that we can see the implementation.
This iterator class will maintain two instance variables: a private ArrayList structure (which becomes the iterable object once the iterator is instantiated), and a private integer variable which keeps track of the current position of the iterator. It includes the following public methods:
Here is a sample run to show how this class works:
> java.util.Iterator< Contact > it = blackbook. iterator();
> while (it.hasNext()) System.out.println (it.next());
Arthur Moody, email: est@temporerat.com, phone: 1-170-451-6998.
Baxter Cantu, email: arcu.iculis@iaculis.net, phone: 1-606-427-1676.
Forrest Beach, email: vulpute@Nuldum.co.uk, phone: 1-117-275-2165.
Lance Farmer, email: sem.egestas@ctus.ca, phone: 1-425-180-9073.
Petra Wiley, email: Morbi.m@duiquis.com, phone: 1-357-588-7644.
> it.next()
java.util. No SuchElementException at PhoneBook $ PhoneBookIterator.next (PhoneBook.java:169)