Question: I need to edit the code below using these instructions please. Do not delete anything from the original code you just have to add the
I need to edit the code below using these instructions please. Do not delete anything from the original code you just have to add the lookup process and what the instructions ask. Please and thank you very much
INSTRUCTIONS:
Make the following modifications/enhancements to Version 0 of the Phonebook application:
- The phonebook should now contain a first name as well as a last name. The format of the entries in the file should be:
last-name first-name phone-number
- The lookup process now prompts for both a last and a first name
- A reverse lookup should also be provided, allowing a name to be obtained by supplying the phone number.
- Rather than continuing until the user signals end-of-file (at the keyboard), the user enters a 'q' to indicate they are done
- After the user is done, the number of lookups and reverse lookups performed are printed
Even though we introduce the terms state and behavior in the context of classes, the terms are still relevant here: the state by which we mean variables is enhanced via the introduction of a first name. Similarly, the behavior . i.e., methods is enhanced through the introduction of a reverse lookup, as well as an enhanced name lookup (first and last).
The name of your class should be Phonebook.
For example, if the file phonebook.text contains:
Arnow David 123-456-7890 Harrow Keith 234-567-8901 Jones Jackie 345-678-9012 Augenstein Moshe 456-789-0123 Sokol Dina 567-890-1234 Tenenbaum Aaron 678-901-2345 Weiss Gerald 789-012-3456 Cox Jim 890-123-4567 Langsam Yedidyah 901-234-5678 Thurm Joseph 012-345-6789
Here is a sample execution of the program. User input is in bold. Your program should replicate the prompts and output:
lookup, reverse-lookup, quit (l/r/q)? l last name? Arnow first name? David David Arnow's phone number is 123-456-7890 lookup, reverse-lookup, quit (l/r/q)? r phone number (nnn-nnn-nnnn)? 456-789-0123 456-789-0123 belongs to Augenstein, Moshe lookup, reverse-lookup, quit (l/r/q)? l last name? Weiss first name? Jerrold -- Name not found lookup, reverse-lookup, quit (l/r/q)? l last name? Weiss first name? Gerald Gerald Weiss's phone number is 789-012-3456 lookup, reverse-lookup, quit (l/r/q)? r phone number (nnn-nnn-nnnn)? 111-123-4567 -- Phone number not found lookup, reverse-lookup, quit (l/r/q)? q 3 lookups performed 2 reverse lookups performed
THE CODE I MUST EDIT:
import java.io.File; import java.util.Scanner; /** * A basic phonebook application. * First, it fills parallel arrays with last names and phone numbers. * Then, it prompts the user to enter a last name to look up the corresponding number. * It keeps doing this until the user indicates they want to stop (Control-Z on Windows; Control-D on Unix). * */ public class Phonebook0 { /** * Sets up the arrays and calls the read method; then calls the lookup method for as long as the user wishes. * * @param args The array of command-line arguments */ public static void main(String[] args) throws Exception { String fileName = "numbers.text"; final int MAX_SIZE = 100; String[] name = new String[MAX_SIZE]; String[] phoneNumber = new String[MAX_SIZE]; int numberOfEntries = read(fileName, name, phoneNumber); Scanner keyboard = new Scanner(System.in); System.out.print("Enter a last name to look up: "); while (keyboard.hasNext()) { String lastName = keyboard.next(); String number = lookup(lastName, name, phoneNumber, numberOfEntries); if (number != null) System.out.println(lastName + "'s number is " + number); else System.out.println(lastName + "'s number isn't in the database"); System.out.print("Enter a last name to look up: "); } keyboard.close(); } /** * Reads last names and phone numbers from a file into two arrays. * Keeps count of how many records have been read in. * * @param fileName The name of the file in memory to read from * @param name The array of last names * @param phoneNumber The array of phone numbers * * @return The number of records that were read into the arrays from the file */ public static int read(String fileName, String[] name, String[] phoneNumber) throws Exception { Scanner sc = new Scanner(new File(fileName)); int counter = 0; while (sc.hasNext() && counter < name.length) { name[counter] = sc.next(); phoneNumber[counter] = sc.next(); counter++; } sc.close(); return counter; } /** * Given a last name, this method looks for the corresponding phone number (a String object). * If the number is found in the database, a reference to that String object is returned. * Otherwise, a null reference is returned, indicating that the desired number hasn't been found. * * @param lastName The name the user wants to look up * @param names The array of last names * @param numbers The array of phone numbers * @param numEntries The number of records we actually have in the database (that is, the arrays might * not be completely full, so this variable indicates how many elements of the array have been filled) * * @return A reference to a String object containing the desired phone number (assuming it has been found), * or a null reference (in case we don't find the name in the database) */ public static String lookup(String lastName, String[] names, String[] numbers, int numEntries) { for (int i = 0; i < numEntries; i++) if (lastName.equals(names[i])) return numbers[i]; return null; } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
