Question
HELP IN JAVA: Write a telephone lookup program. Read a data set of 1000 names and telephone numbers from files that contain contact information in
HELP IN JAVA:
Write a telephone lookup program. Read a data set of 1000 names and telephone numbers from files that contain contact information in a random order. (There will be no duplicates.) Use insertion sort for sorting. Handle lookups by name and lookups by phone number. Use a binary search for both lookups.
The program has to do two things: read in the data and handle user queries.
The data is in two separate files: one for the names and one for the phone numbers. There is one name on each line and one phone number on each line. Corresponding lines in the two files correspond to a single contact. For example, line 738 of the phone number file contains the phone number belonging to the person whose name is on line 738 of the names file.
The program should prompt the user with this message: Please enter a name or phone number, or * to quit. The user may enter a name or phone number, and the program prints the name and the phone number (separated by a space) for the corresponding contact. If a match cannot be found, the program should print No contact found. Repeat this until the user enters *.
This problem has two main steps: Read the data, and process user queries. Therefore, we can break this down into two methods, and call them both from the main method:
public static void main(String[] args)
{
readData();
processQueries();
}
The problem says to use binary search. Therefore, the data has to be sorted. Since we need to be able to search by name or by phone number, we have to have the data sorted by both name and phone. This is impossible in one array, so the best way to do this is to create two arrays, one for the contacts sorted by name and one for contacts sorted by phone. Both arrays will have the same data, but in a different order.
You can use your generic binary search method from class, but for that to work, the class has to implement Comparable. Since we have two different ways to compare, we have two different implementations of compareTo. The only way to handle this situation is to have two different classes. Lets call them ContactByName and ContactByPhone. Each one will have two instance variables, the name and the phone number. Each one will implement Comparable so we can use the generic binary search (and the generic sort we will write later.) The compareTo method can just call compareTo on the strings we want to compare.
class ContactByName implements Comparable
{
private final String name;
private final String phone;
public ContactByName(String name, String phone)
{
this.name = name;
this.phone = phone;
}
@Override
public int compareTo(ContactByName o)
{
return this.name.compareTo(o.name);
}
}
Implement the ContactByPhone class similarly.
Now that we have the classes, we can create the two arrays:
ContactByName[] sortedByName = new ContactByName[1000];
ContactByPhone[] sortedByPhone = new ContactByPhone[1000];
We can implement the readData method. This method has to do the following steps:
Open the two files for reading. (Use a scanner wrapped around a file input stream.)
Loop while the files have more to read:
Read the next line from each file. These two strings are the name and phone number of a new contact.
Create a new ContactByName and add it to the array.
Create a new ContactByPhone and add it to the other array.
Next, we have to sort the arrays. The instructions said to use insertion sort. Insertion sort works by looking at each item of the array in turn and inserting it into the already-sorted portion of the array. In other words, at step i we know that positions 0 through i-1 are already sorted, and we have to put the next element into its correct position. This means sliding everything else in the array down by one position. Start out the code like this:
public static
{
for (int i=1; i { //Now positions 0 through i-1 are already sorted. //So find the place for the item at position i. T ci = list[i]; //[add inner loop here] } } Note that we start the loop at 1, because the item at position 0, by itself, is already sorted. The contact at position is called ci. Now write an inner loop that runs from i-1 down toward 0. We are looking for the slot to put item ci. Each time we move in the inner loop, we shift the item to the right, so there will be a slot to put ci when we find the right slot. The inner loop can stop either when we find an element less than ci, or when we reach index 0 in the array. Since there are two ways to stop, this loop should be a while loop. Complete the sort method. Lets test the sort before we move on. We can sort the array and then print it out to see if its in order. To make printing easier, lets implement a toString method in each contact class. @Override public String toString() { return name + " " + phone; } Now we can easily write a printAll method: public static { for (T c:list) System.out.println(c); } Lets run the main method like this to test the sort: public static void main(String[] args) { ContactByName[] sortedByName = new ContactByName[1000]; ContactByPhone[] sortedByPhone = new ContactByPhone[1000]; readData(sortedByName, sortedByPhone); sortData(sortedByName, sortedByPhone); printAll(sortedByName); //printAll(sortedByPhone); //processQueries(sortedByName, sortedByPhone); } The output should look like this for the data sorted by names. You should also check the data sorted by phone numbers. Aaden Black 195-170-2381 Aarav Li 565-832-9840 Abbey Mcpherson 814-314-1284 Zechariah Wheeler 759-849-6834 Zion Baxter 945-921-6305 Zoey Bowman 990-500-6554 The final portion of this problem is to read queries from the user and look up each one in the appropriate array. The processQueries method has to do the following: Print the prompt as described in the problem statement. Set up a scanner to read from the user. Read a line from the user. While the input is not *, Check whether the first character is a digit. If so, query the phone number list. If not, query the name list. Read the next line from the user. We need two more methods: one to process a phone number query, and one to process a name query. They both have the same structure: public static String queryByName(String name, ContactByName[] sortedByName ) { //Get result from binarySearch [add call here] //Check the result if (result != -1) return sortedByName[result].toString(); else return "No contact found"; } If you are using the binary search from class, it returns the position in the array where the item is found, or -1 if it is not found. The binary search method is expecting a ContactByName object to search for. All we have is the name itself, but we can easily construct an object like this: new ContactByName(name,null) The null phone number wont match anything, but that doesnt matter, because for the ContactByName class, compareTo only checks the name. When you complete the queryByName and queryByPhone methods, your code should be complete. Test it with several queries. Here is a sample run you can try to replicate: Please enter a name or phone number, or * to quit Davin Taylor Davin Taylor 580-274-8598 512-256-3102 Vaughn Cortez 512-256-3102 *
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started