Final Project

Instructions

  1. Write a Java program to make a mini-telephone directory using a hash table with separate chaining. The hash table is implemented with class TelephoneBook, which stores an array of seven (7) LinkedList objects. Each LinkedList is made of PersonNode nodes.
  2. Your program will be evaluated by the following criteria: project-rubric.xls.
  3. The name of your program should be LastnameFirstnameProject.java. You should have at least four (4) classes: LastnameFirstnameProject (with the main() method), PersonNode, LinkedList and TelephoneBook. Put all of the classes in the same LastnameFirstnameProject.java file.
  4. Make sure you use the object-oriented concepts we learned in class this semester. Do NOT use static variables or methods in your TelephoneBook, LinkedList, or PersonNode classes. The static methods are only the main() method and supporting methods in your LastNameFirstNameProject class. You should create objects with your TelephoneBook, LinkedList, and PersonNode classes, and call your methods with these objects. For example:
        TelephoneBook book = new TelephoneBook(); // create object with TelephoneBook class
        book.insert("NAMI","123-4567"); // use object book to call insert() method
        
  5. Use class PersonNode to store the name and telephone number of each person, and the link to the next PersonNode. The code for your PersonNode class should look like this:
    	class PersonNode{
            //data field for name
            //data field for telephone number
            //data field for next PersonNode
            //constructor
            //toString() method
            //getName() method
            //getTelephoneNumber() method
            //getNext() method
            //setNext() method
    	}
    	
  6. In the class TelephoneBook, a one (1) dimensional array of 7 LinkedList objects storing PersonNode objects is used to implement the hash table.
  7. In the class LinkedList, the add() method only adds to the front of the LinkedList.
  8. In the classs LinkedList, the get() method returns a PersonNode object based on the name of the person. In other words, the input (parameter) to the get method is the name of the person. The get() method searches through the linked list of PersonNodes and returns the PersonNode with the matching name.
  9. In the class LinkedList, the remove() method removes based on the name of the person. In other words, the input (parameter) to the remove method is the name of the person. The remove() method searches through the linked list of PersonNodes and deletes the PersonNode with the matching name.
  10. The code for your LinkedList class should look like this:
    
    	class LinkedList{
            //data field for PersonNode head
            //constructor
            //toString() method
            //add() method
            //get() method
            //remove() method
    	}
    	
  11. For the TelephoneBook class, include these methods: TelephoneBook(), insert(), retrieve(), delete(), and a toString(). For the data field, create an array of seven (7) LinkedList objects.
    	class TelephoneBook{
            //array of 7 LinkedList elements 
            //constructor
            //insert() method
            //retrieve() method
            //delete() method
            //toString() method
    	}
    	
    1. For the TelephoneBook, the insert() method has a name and telephone as parameters. You hash the name to determine which of the 7 LinkedList objects to add to. There is nothing to return.
    2. For the TelephoneBook, the retrieve() method has a name as a parameter. You hash the name to determine which of the 7 LinkedList objects to get from. Return the matching PersonNode.
    3. For the TelephoneBook, the delete() method has a name as a parameter. You hash the name to determine which of the 7 LinkedList objects to remove from. Return the matching PersonNode.
    4. For the TelephoneBook, the toString() method has no parameters. Loop through the array of the 7 LinkedList objects. Return a string.
  12. The class TelephoneBook implements a hash table with separate chaining.
  13. Use the hashCode() method from the Java String class. The hashCode() method already exists. You do not have to write it yourself. For details, see the hashCode() method in the Java String class in the Java API: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#hashCode()
  14. For example, here are two names that have the same value for the hash function: h(key) = key.hashCode() % 7. Below is code that shows how to use method hashCode() and modulus (%) to calculate the hash function. The "key" of the hash function is the name of the person. You should use the hash function in the insert(), retrieve(), and delete() methods to locate the array index of the element.
            Integer stringHash = "BUBBA".hashCode(); // stringHash is 63550158
            Integer myHash = stringHash%7; // myHash is 0, so it is stored in LinkedList at table[0]
            stringHash = "SUE".hashCode(); // stringHash is 82467
            myHash = stringHash%7; // myHash is 0, so it is stored in LinkedList at table[0]
            stringHash = "CAT".hashCode(); // stringHash is 66486
            myHash = stringHash%7; // myHash is 0, so it is stored in LinkedList at table[0]
     
  15. Below is example input and output. You can use JOptionPane for I/O as well. The output file should be in the same format as the input file.
  16. Your program should work with 2 commandline arguments. If there are 2 commandline arguments, the 1st commmandline argument (args[0]) is the input file and the 2nd commmandline argument (args[1]) is the output file. Output an error message if there are not exactly 2 commandline arguments.
  17. Write your original comments every 3-5 lines of code.
  18. Make sure your code follows the ICS 211 Java Coding Standard, in particular the Java documentation (Javadoc) comments that go above each method.

Example Output

  1. I/O with 2 commandline arguments: input-project.csv output-project.csv
    Reading from input file: input-project.csv
    
    Personal Telephone Book Hash Table
    
    MAIN MENU
    1. Insert telephone number
    2. Retrieve telephone number
    3. Delete telephone number
    4. Display telephone book
    5. End program
    Enter choice (1-5): 4
    
    DISPLAY TABLE
    table[0] => null
    table[1] => LEE 777-7777 => BAKER 111-1111 => null
    table[2] => KAM 654-7890 => LONG 987-1234 => DAVIS 999-9999 => null
    table[3] => FOX 666-6666 => null
    table[4] => CASTRO 888-8888 => CARTER 222-2222 => null
    table[5] => KING 555-5555 => HALL 123-4566 => null
    table[6] => null
    
    MAIN MENU
    1. Insert telephone number
    2. Retrieve telephone number
    3. Delete telephone number
    4. Display telephone book
    5. End program
    Enter choice (1-5): 1
    
    Enter person's name: BUBBA
    Enter person's telephone number: 111-2222
    
    MAIN MENU
    1. Insert telephone number
    2. Retrieve telephone number
    3. Delete telephone number
    4. Display telephone book
    5. End program
    Enter choice (1-5): 1
    
    Enter person's name: SUE
    Enter person's telephone number: 222-3333
    
    MAIN MENU
    1. Insert telephone number
    2. Retrieve telephone number
    3. Delete telephone number
    4. Display telephone book
    5. End program
    Enter choice (1-5): 1
    
    Enter person's name: CAT
    Enter person's telephone number: 555-7777
    
    MAIN MENU
    1. Insert telephone number
    2. Retrieve telephone number
    3. Delete telephone number
    4. Display telephone book
    5. End program
    Enter choice (1-5): 4
    
    DISPLAY TABLE
    table[0] => CAT 555-7777 => SUE 222-3333 => BUBBA 111-2222 => null
    table[1] => LEE 777-7777 => BAKER 111-1111 => null
    table[2] => KAM 654-7890 => LONG 987-1234 => DAVIS 999-9999 => null
    table[3] => FOX 666-6666 => null
    table[4] => CASTRO 888-8888 => CARTER 222-2222 => null
    table[5] => KING 555-5555 => HALL 123-4566 => null
    table[6] => null
    
    MAIN MENU
    1. Insert telephone number
    2. Retrieve telephone number
    3. Delete telephone number
    4. Display telephone book
    5. End program
    Enter choice (1-5): 2
    
    Enter person's name: LEE
    The telephone number is: 777-7777
    
    MAIN MENU
    1. Insert telephone number
    2. Retrieve telephone number
    3. Delete telephone number
    4. Display telephone book
    5. End program
    Enter choice (1-5): 2
    
    Enter person's name: DAVIS
    The telephone number is: 999-9999
    
    MAIN MENU
    1. Insert telephone number
    2. Retrieve telephone number
    3. Delete telephone number
    4. Display telephone book
    5. End program
    Enter choice (1-5): 3
    
    Enter person's name: LEE
    Deleting: LEE 777-7777
    
    MAIN MENU
    1. Insert telephone number
    2. Retrieve telephone number
    3. Delete telephone number
    4. Display telephone book
    5. End program
    Enter choice (1-5): 3
    
    Enter person's name: LONG
    Deleting: LONG 987-1234
    
    MAIN MENU
    1. Insert telephone number
    2. Retrieve telephone number
    3. Delete telephone number
    4. Display telephone book
    5. End program
    Enter choice (1-5): 3
    
    Enter person's name: HALL
    Deleting: HALL 123-4566
    
    MAIN MENU
    1. Insert telephone number
    2. Retrieve telephone number
    3. Delete telephone number
    4. Display telephone book
    5. End program
    Enter choice (1-5): 4
    
    DISPLAY TABLE
    table[0] => CAT 555-7777 => SUE 222-3333 => BUBBA 111-2222 => null
    table[1] => BAKER 111-1111 => null
    table[2] => KAM 654-7890 => DAVIS 999-9999 => null
    table[3] => FOX 666-6666 => null
    table[4] => CASTRO 888-8888 => CARTER 222-2222 => null
    table[5] => KING 555-5555 => null
    table[6] => null
    
    MAIN MENU
    1. Insert telephone number
    2. Retrieve telephone number
    3. Delete telephone number
    4. Display telephone book
    5. End program
    Enter choice (1-5): 5
    
    Wrote to output file: output-project.csv
    	

Click to validate the HTML code

Click to validate the CSS code