Question

Make the following modifications/enhancements to Version 0 of the Phonebook application: The phonebook should now contain...

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

this is phonebook version 0

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).
 * 
 * @author Moshe Lach
 */
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;
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the modified phone book application along with the updated documentation below.

Below are the screenshots of the code for reference:

Below is the code of the class Phonebook0:

import java.io.File;
import java.util.Scanner;

/**
* A modified/enhanced phonebook application.
* First, it fills parallel arrays with last names, first names and phone numbers.
* Then, it prompts the user to either look up a number of reverse look up a name or quit
* For look up, it prompts the user to enter the last name and first name to look up the corresponding number.
* For reverse look up, it prompts the user to enter a phone number to look up the corresponding last and first names.
* It keeps doing this until the user indicates they want to stop (by entering the quit - q option when prompted).
*
* @author Moshe Lach
*/
public class Phonebook0 {
/**
* Sets up the arrays and calls the read method; then calls the lookup / reverse look up 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.txt";
final int MAX_SIZE = 100;
String[] firstName = new String[MAX_SIZE];
String[] lastName = new String[MAX_SIZE];
String[] phoneNumber = new String[MAX_SIZE];
int numberOfEntries = read(fileName, lastName, firstName, phoneNumber);
Scanner keyboard = new Scanner(System.in);
      
       String userInput = "";
       int numLookups = 0, numReverseLookUps = 0;   // To keep track of number of lookups and reverse lookups
do {
           /**
           * Prompting the user to lookup or reverse lookup or quit
           */
           System.out.print("lookup, reverse-lookup, quit (l/r/q)? ");
userInput = keyboard.next();
           /**
           * If user wants to look up
           */
           if(userInput.equals("l")) {
               numLookups++;
               System.out.print("last name? ");
               String inputLastName = keyboard.next();
               System.out.print("first name? ");
               String inputFirstName = keyboard.next();
               String number = lookup(inputLastName, inputFirstName, lastName, firstName, phoneNumber, numberOfEntries);
               if (number != null)
                   System.out.println(inputFirstName + " " + inputLastName + "'s phone number is " + number);
               else
                   System.out.println("-- Name not found");
           }
           /**
           * If user wants to reverse look up
           */
           else if(userInput.equals("r")) {
               numReverseLookUps++;
               System.out.print("phone number (nnn-nnn-nnnn)? ");
               String inputPhoneNumber = keyboard.next();
               String name = reverseLookup(inputPhoneNumber, phoneNumber, lastName, firstName, numberOfEntries);
               if (name != null)
                   System.out.println(inputPhoneNumber + " belongs to " + name);
               else
                   System.out.println("-- Phone number not found");
           }
} while(!userInput.equals("q"));
      
       System.out.println(numLookups + " lookups performed");
       System.out.println(numReverseLookUps + " reverse lookups performed");
keyboard.close();
}


/**
* Reads last names, first names and phone numbers from a file into three arrays.
* Keeps count of how many records have been read in.
*
* @param fileName The name of the file in memory to read from
* @param lastName The array of last names
* @param firstName The array of first 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[] lastName, String[] firstName, String[] phoneNumber) throws Exception {
Scanner sc = new Scanner(new File(fileName));
int counter = 0;
while (sc.hasNext() && counter < lastName.length) {
lastName[counter] = sc.next();
firstName[counter] = sc.next();
phoneNumber[counter] = sc.next();
counter++;
}
sc.close();
return counter;
}


/**
* Given a last name and first 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 last name the user wants to look up
* @param firstname The first name the user wants to look up
* @param lastNames The array of last names
* @param firstNames The array of first 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 firstname, String[] lastNames, String[] firstNames, String[] numbers, int numEntries) {
for (int i = 0; i < numEntries; i++)
if (lastname.equals(lastNames[i]) && firstname.equals(firstNames[i]))
return numbers[i];
return null;
}
  
   /**
* Given a phone number, this method looks for the corresponding last name and first name (String objects).
* If the names are found in the database, a reference to the new String object containing both the first nad last names is returned.
* Otherwise, a null reference is returned, indicating that the desired names aren't been found.
*
* @param number The phone number the user wants to look up
* @param numbers The array of phone numbers
* @param lastNames The array of last names
* @param firstNames The array of first names
* @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 new String object containing the desired last name and first name (assuming it has been found),
* or a null reference (in case we don't find the names in the database)
*/
   public static String reverseLookup(String number, String[] numbers, String[] lastNames, String[] firstNames, int numEntries) {
for (int i = 0; i < numEntries; i++)
if (number.equals(numbers[i]))
return lastNames[i] + ", " + firstNames[i];
return null;
}
}

Below is the screenshot of the output console:

Hope this helps you and also hoping for a positive review.

Add a comment
Know the answer?
Add Answer to:
Make the following modifications/enhancements to Version 0 of the Phonebook application: The phonebook should now contain...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a telephone lookup PYTHON program. Read a data set of names and telephone numbers from...

    Write a telephone lookup PYTHON program. Read a data set of names and telephone numbers from a file that contains the numbers in random order. Handle lookups by name and also reverse lookups by phone number. Use a binary search for both lookups. Use the following data set: Bob|555-1234 Joe|555-2345 John|555-3456 Luke|555-4567 Mark|555-5678 Matthew|555-6789 The program should prompt the user as follows:       L)ookup Name, Lookup N)umber or Q)uit?       Enter the name:          Or       Enter the Number:...

  • DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to...

    DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...

  • Language Java Step 1: Design a class called Student. The Student class should contain the following...

    Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...

  • Write a simple telephone directory program in C++ that looks up phone numbers in a file...

    Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the...

  • Please provide the full code...the skeleton is down below: Note: Each file must contain an int...

    Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...

  • Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What...

    Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What This Assignment Is About? Classes (methods and attributes) • Objects Arrays of Primitive Values Arrays of Objects Recursion for and if Statements Selection Sort    Use the following Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc.) Use upper case for constants. • Use title case (first letter is upper case) for classes. Use lower case with uppercase...

  • Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray();...

    Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray(); char[] reverse = new char[original.length]; for(int i =0; i<s.length(); i++){ reverse[i] = original[original.length-1-i]; } return new String(reverse); } /**  * Takes in a string containing a first, middle and last name in that order.  * For example Amith Mamidi Reddy to A. M. Reddy.  * If there are not three words in the string then the method will return null  * @param name in...

  • In this assignment, we will be making a program that reads in customers' information, and create...

    In this assignment, we will be making a program that reads in customers' information, and create a movie theatre seating with a number of rows and columns specified by a user. Then it will attempt to assign each customer to a seat in a movie theatre. 1. First, you need to add one additional constructor method into Customer.java file. Method Description of the Method public Customer (String customerInfo) Constructs a Customer object using the string containing customer's info. Use the...

  • Rework this project to include a class. As explained in class, your project should have its...

    Rework this project to include a class. As explained in class, your project should have its functionalities moved to a class, and then create each course as an object to a class that inherits all the different functionalities of the class. You createclass function should be used as a constructor that takes in the name of the file containing the student list. (This way different objects are created with different class list files.) Here is the code I need you...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT