Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this task you will use modify the hash table you created in Task 1 to lookup

ID: 3814277 • Letter: F

Question

For this task you will use modify the hash table you created in Task 1 to lookup telephone numbers. The input into your program will be a simple text file that contains the names and phone numbers. When the program begins it should read the file and load the data into the hash table. The data should be hashed by the name not the number. Basically, you are using the name for the key and the phone number for the value. You should also have functionality that will allow a name to be input and the phone number retrieved and displayed. This should also include an appropriate message when the user does not exist in the table. For this assignement you are going to set the table size to be 53 because it is a nice prime number. In addition, you can expect to have names that will hash to the same value so you must make sure your collision detection scheme is working properly. The hash table you created in task 1 works with doubles this means that you are going to have to modify it so that it will work with strings. A simple hash routine would be to sum all of the characters in the string and modulus that value with the length of the hash table. It would be best to simply overload the functions that you have already created. As stated you should have functionality that will allow the user to input a name and output the corresponding phone number. This functionality should also output the hash value. Your program should look something like this:

Explanation / Answer

//phone.txt save in the current directory

jack,(555)916-1213
jane,(666)213-3456
kacj,(444)718-3214
james,(222)145-2314
peter,(111)134-5698
samuel,(555)456-8910
matthew,(444)134-1324
john,(222)123-4521
anej,(222)167-9087
ramk,(111)234-9670
mark,(222)165-6896
collins,(444)345-9130
david,(333)143-7890

//Telephone.java

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;


public class Telephone {

   public static void main(String[] args) {
       //Hashtable of type key as Hash table index out of 53
       //and Value as Name and Phone number
       Hashtable<Integer, String> phone = new Hashtable<Integer, String>(53);
       try{
       //Creating FileReader for reading file contains data
       FileReader fr = new FileReader("phone.txt");
       BufferedReader br = new BufferedReader(fr);
  
       String person;
       //Reading each line from file and storing into hash table
       while((person=br.readLine())!=null){
           String data[] = person.split(",");
           int i=0;
           int sum=0;
           //Summation of characters of name
           while(data[0].length()>i){
               sum=sum+data[0].charAt(i);
               i++;
           }
           //Index for Hashtable out of 53
           //h(x) = sum % 53
           int loc = sum%53;
           //Checking if key contains index
           //Linear probing for collision
           if(phone.containsKey(loc)){
               int j=loc;
               while(j<53 && phone.containsKey(j)){
                   if(j==52){
                       j=0;
                   }
                   else{
                       j++;
                   }
               }
               //Keeping into Hashtable
               phone.put(j, data[0]+","+data[1]);
           }else{
               phone.put(loc, data[0]+","+data[1]);
           }
       }

       //Printing from Hashtable
       System.out.println("*****Telephone Directory****");
       Set<Integer> st = phone.keySet();
       Iterator<Integer> ir = st.iterator();
       //Iterating Hashtale
       while(ir.hasNext()){
           Integer k = ir.next();
           System.out.println(k+" "+phone.get(k));
       }
      
       //Reading a name to search in Hashtable
       Scanner s = new Scanner(System.in);
       System.out.println("Enter the name :");
       String name = s.next();
       //Till uSer stop entering name
       while(true){
           //Getting Each entry from hashtable
           Set<Entry<Integer, String>> set = phone.entrySet();
           Iterator<Entry<Integer, String>> itr = set.iterator();
           //Iterating Entry to search in hashtable
           while(itr.hasNext()){
               //Getting Key and Value
               Entry<Integer, String> entry = itr.next();
               String ss[] = entry.getValue().split(",");
               //Checking the name as ss[0]
               //ss[1] contains phone number
               if(ss[0].equalsIgnoreCase(name)){
                   System.out.println(ss[0]+"("+entry.getKey()+")"+" "+ss[1]);
                   break;
               }
           }
           //Searching for another record
           System.out.println("Do you wanna Look up for another number:<Y/N>:");
           if(s.next().equalsIgnoreCase("Y")){
               System.out.println("Enter the name:");
               name = s.next();
           }else{
               break;
           }
       }
      
       }catch(Exception e){
           e.printStackTrace();
       }
   }

}

Output:

*****Telephone Directory****
51 james,(222)145-2314
45 david,(333)143-7890
44 anej,(222)167-9087
43 jane,(666)213-3456
39 kacj,(444)718-3214
38 jack,(555)916-1213
20 matthew,(444)134-1324
15 collins,(444)345-9130
14 peter,(111)134-5698
11 samuel,(555)456-8910
7 john,(222)123-4521
4 mark,(222)165-6896
3 ramk,(111)234-9670
Enter the name :
james
james(51) (222)145-2314
Do you wanna Look up for another number:<Y/N>:
Y
Enter the name:
jack
jack(38) (555)916-1213
Do you wanna Look up for another number:<Y/N>:
Y
Enter the name:
kacj
kacj(39) (444)718-3214
Do you wanna Look up for another number:<Y/N>:
Y
Enter the name:
jane
jane(43) (666)213-3456
Do you wanna Look up for another number:<Y/N>:
Y
Enter the name:

anej
anej(44) (222)167-9087
Do you wanna Look up for another number:<Y/N>:
N

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote