java not c++ Your assignment is to create a dictionary (phonebook) using hashing
ID: 3765637 • Letter: J
Question
java not c++
Your assignment is to create a dictionary (phonebook) using hashing.
The Key will be a two digit phone number, the associated data will be a name (a String). The size of the hash table is to be 11 (a prime number). Your main program must provide a means of inputting a Key (the two digit phone number) and a name (a String) from a user to add to the table. It must also allow entries to be deleted using the phone number. It must also display the name given the number (you need not provide a mechanism to display the phone number given the name). You do not need to increase the size of the hash table (but may do so if you wish), but your code must handle trying to put too many items into the phonebook (more than 11 unless you use a linked list in which case you will not have this problem). Display an error message if you do not expand the table and cannot add an entry because it is full. You must handle collisions, but you can determine how to do so (for example linear, exponential, or rehash, or use chaining from the table).
Notes:
1. When you program first starts to execute, it must display the message “Phonebook by your name”.
2. Then the program is to repeatedly as if the user wants to do additions, subtractions, searches, or to terminate the program.
3. Do not use the Java built in methods, create your own.
4. Put all methods in a single “.java” file.
5. Make certain to provide a Word document explaining how a user is to use your phonebook.
Explanation / Answer
PhoneDirectory.java
/*
* chegg expert answer
Phone directory using hashing
*/
package com.student;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class PhoneDirectory {
public static void main(String[] args) {
String name, number = null; // Name and number of an entry in the directory
// (used at various places in the program).
HashMap<String,String> phoneBook; // Phone directory data structure.
// Enties are name/number pairs.
phoneBook = new HashMap<String,String>();
/* Create a data variable a data in phone direcotory
*/
String data=null;
System.out.println("Miky Andrew");
if ( data==null ) {
System.out.println("No phone book data file found.");
System.out.println("A new one will be created.");
}
else {
System.out.println("Reading phone book data...");
try {
Scanner scanner = new Scanner( data );
while (scanner.hasNextLine()) {
String phoneEntry = scanner.nextLine();
int separatorPosition = phoneEntry.indexOf('%');
if (separatorPosition == -1)
throw new IOException("File is not a phonebook data file.");
name = phoneEntry.substring(0, separatorPosition);
number = phoneEntry.substring(separatorPosition+1);
phoneBook.put(name,number);
}
}
catch (IOException e) {
System.out.println("Error in phone book data");
System.out.println("This program cannot continue.");
System.exit(1);
}
}
/* Read data from the user and carry them out, until the
* user gives the "Exit from program" ch.
*/
Scanner in = new Scanner( System.in );
boolean modified = false; // Have any changes been made to the directory?
mainLoop: while (true) {
System.out.println(" Select the action that you want to perform:");
System.out.println(" 1. search phone number.");
System.out.println(" 2. Add or modify a phone number.");
System.out.println(" 3. Remove an entry from your phone directory.");
System.out.println(" 4. List the contacts from phone directory.");
System.out.println(" 5. Exit from the program.");
System.out.println("Enter action number (1-5): ");
int ch;
if ( in.hasNextInt() ) {
ch = in.nextInt();
in.nextLine();
}
else {
System.out.println(" ILLEGAL RESPONSE. YOU MUST ENTER A NUMBER.");
in.nextLine();
continue;
}
switch(ch) {
case 1:
System.out.print(" Enter the name whose number you want to look up: ");
name = in.nextLine().trim().toLowerCase();
number = phoneBook.get(name);
if (number == null)
System.out.println(" SORRY, NO NUMBER FOUND FOR " + name);
else
System.out.println(" NUMBER FOR " + name + ": " + number);
break;
case 2:
System.out.print(" Enter the name: ");
name = in.nextLine().trim().toLowerCase();
if (name.length() == 0)
System.out.println(" NAME CANNOT BE BLANK.");
else if (name.indexOf('%') >= 0)
System.out.println(" NAME CANNOT CONTAIN THE CHARACTER "%".");
else {
System.out.print("Enter phone number: ");
number = in.nextLine().trim();
if(number.length()!=2)
{
System.out.println("please enter two digit phone number only ");
System.exit(1);
}
else if (number.length() == 0)
System.out.println(" PHONE NUMBER CANNOT BE BLANK.");
else {
phoneBook.put(name,number);
modified = true;
}
}
break;
case 3:
System.out.print(" Enter the name whose entry you want to remove: ");
name = in.nextLine().trim().toLowerCase();
number = phoneBook.get(name);
if (number == null)
System.out.println(" SORRY, THERE IS NO ENTRY FOR " + name);
else {
phoneBook.remove(name);
modified = true;
System.out.println(" DIRECTORY ENTRY REMOVED FOR " + name);
}
break;
case 4:
System.out.println(" LIST OF ENTRIES IN YOUR PHONE BOOK: ");
for ( Map.Entry<String,String> entry : phoneBook.entrySet() )
System.out.println(" " + entry.getKey() + ": " + entry.getValue() );
break;
case 5:
System.out.println(" Exiting program.");
break mainLoop;
default:
System.out.println(" ILLEGAL ACTION NUMBER.");
}
}
}
}
output
run:
Miky Andrew
No phone book data file found.
A new one will be created.
Select the action that you want to perform:
1. search phone number.
2. Add or modify a phone number.
3. Remove an entry from your phone directory.
4. List the contacts from phone directory.
5. Exit from the program.
Enter action number (1-5):
2
Enter the name: john
Enter phone number: 12
Select the action that you want to perform:
1. search phone number.
2. Add or modify a phone number.
3. Remove an entry from your phone directory.
4. List the contacts from phone directory.
5. Exit from the program.
Enter action number (1-5):
2
Enter the name: andrew
Enter phone number: 10
Select the action that you want to perform:
1. search phone number.
2. Add or modify a phone number.
3. Remove an entry from your phone directory.
4. List the contacts from phone directory.
5. Exit from the program.
Enter action number (1-5):
2
Enter the name: russel
Enter phone number: 67
Select the action that you want to perform:
1. search phone number.
2. Add or modify a phone number.
3. Remove an entry from your phone directory.
4. List the contacts from phone directory.
5. Exit from the program.
Enter action number (1-5):
4
LIST OF ENTRIES IN YOUR PHONE BOOK:
andrew: 10
russel: 67
john: 12
Select the action that you want to perform:
1. search phone number.
2. Add or modify a phone number.
3. Remove an entry from your phone directory.
4. List the contacts from phone directory.
5. Exit from the program.
Enter action number (1-5):
3
Enter the name whose entry you want to remove: andrew
DIRECTORY ENTRY REMOVED FOR andrew
Select the action that you want to perform:
1. search phone number.
2. Add or modify a phone number.
3. Remove an entry from your phone directory.
4. List the contacts from phone directory.
5. Exit from the program.
Enter action number (1-5):
1
Enter the name whose number you want to look up: john
NUMBER FOR john: 12
Select the action that you want to perform:
1. search phone number.
2. Add or modify a phone number.
3. Remove an entry from your phone directory.
4. List the contacts from phone directory.
5. Exit from the program.
Enter action number (1-5):
5
Exiting program.
BUILD SUCCESSFUL (total time: 1 minute 4 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.