1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
denis-greek [22]
3 years ago
14

1. Write a telephone lookup program. Read a data set of 1, 000 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.

Computers and Technology
1 answer:
Fed [463]3 years ago
3 0

Answer:

Kindly note that, you're to replace "at" with shift 2 as the brainly text editor can't take the symbol

Explanation:

PhoneLookup.java

import java.io.FileReader;

import java.io.IOException;

import java.util.Scanner;

public class PhoneLookup

{

  public static void main(String[] args) throws IOException

  {

     Scanner in = new Scanner(System.in);

     System.out.println("Enter the name of the phonebook file: ");

     String fileName = in.nextLine();

     LookupTable table = new LookupTable();

     FileReader reader = new FileReader(fileName);

     table.read(new Scanner(reader));

   

     boolean more = true;

     while (more)

     {

        System.out.println("Lookup N)ame, P)hone number, Q)uit?");

        String cmd = in.nextLine();

       

        if (cmd.equalsIgnoreCase("Q"))

           more = false;

        else if (cmd.equalsIgnoreCase("N"))

        {

           System.out.println("Enter name:");

           String n = in.nextLine();

           System.out.println("Phone number: " + table.lookup(n));

        }

        else if (cmd.equalsIgnoreCase("P"))

        {

           System.out.println("Enter phone number:");

           String n = in.nextLine();

           System.out.println("Name: " + table.reverseLookup(n));

        }

     }

  }

}

LookupTable.java

import java.util.ArrayList;

import java.util.Collections;

import java.util.Scanner;

/**

  A table for lookups and reverse lookups

*/

public class LookupTable

{

  private ArrayList<Item> people;

  /**

     Constructs a LookupTable object.

  */

  public LookupTable()

  {

      people = new ArrayList<Item>();

  }

  /**

     Reads key/value pairs.

     "at"param in the scanner for reading the input

  */

  public void read(Scanner in)

  {

     while(in.hasNext()){

         String name = in.nextLine();

         String number = in.nextLine();

         people.add(new Item(name, number));

     }

  }

  /**

     Looks up an item in the table.

     "at"param k the key to find

     "at"return the value with the given key, or null if no

     such item was found.

  */

  public String lookup(String k)

  {

     String output = null;

     for(Item item: people){

         if(k.equals(item.getName())){

             output = item.getNumber();

         }

     }

     return output;

  }

  /**

     Looks up an item in the table.

     "at"param v the value to find

     "at"return the key with the given value, or null if no

     such item was found.

  */

  public String reverseLookup(String v)

  {

      String output = null;

         for(Item item: people){

             if(v.equals(item.getNumber())){

                 output = item.getName();

             }

         }

         return output;

  }

}

Item.java

public class Item {

  private String name, number;

 

  public Item(String aName, String aNumber){

      name = aName;

      number = aNumber;

  }

 

  public String getName(){

      return name;

  }

 

  public String getNumber(){

      return number;

  }

}

input.txt

Abbott, Amy

408-924-1669

Abeyta, Ric

408-924-2185

Abrams, Arthur

408-924-6120

Abriam-Yago, Kathy

408-924-3159

Accardo, Dan

408-924-2236

Acevedo, Elvira

408-924-5200

Acevedo, Gloria

408-924-6556

Achtenhagen, Stephen

408-924-3522

Kindly check the attached output image below.

You might be interested in
Creative Commons Question -- At home, you created a really great digital song, audio jingle, or music track using some software
galina1969 [7]
C. It's honestly up to the user, upload so others can use it or make it so its copyrighted and make some cash :P
6 0
2 years ago
4. Which of the following root keys contains the data for a system’s non-user-specific configurations? a. HKEY_LOCAL_MACHINE b.
seropon [69]

Answer:

The system’s non-user-specific configurations are stored in the HKEY_LO-

CAL_MACHINE root key of the Registry.

Explanation:

6 0
3 years ago
What two things should you do before starting the design process
Charra [1.4K]

Answer: B and C

Explanation: Analyze the audience

                      Identify the problem

8 0
2 years ago
Choose the correct term to complete the sentence.
Diano4ka-milaya [45]

Answer:

Object-oriented programming

Explanation:

When used in object-oriented programming , a class is a factory for creating object. An object is a collection of data and behavaiors that represent some entity(real or abstract).

6 0
2 years ago
DO NOT JOIN ANY Z O O M MEETINGS FROM THIS PERSON! IS A TRAP Please help me get them banned!!!!!
s2008m [1.1K]

Answer:

how do i help?

Explanation:

7 0
3 years ago
Read 2 more answers
Other questions:
  • Debugging is not testing, but always occurs as a consequence of testing. <br> A) TRUE<br> B) FALSE
    13·1 answer
  • Which of the following statements is true?
    12·1 answer
  • The __________ assumption necessary for a linear programming model to be appropriate means that the contribution to the objectiv
    6·1 answer
  • Create detailed pseudocode for a program that calculates how many days are left until Christmas, when given as an input how many
    15·1 answer
  • A distortion of information about the demand for a product as it passes from one entity to the next across the supply chain is c
    10·1 answer
  • Can a idler gear increase or decrease torque?
    7·1 answer
  • Ask the user to input a word. Then, print the first letter, the last letter, and the length of the word on the same line with no
    6·1 answer
  • Write a function max_magnitude() with two integer input parameters that returns the largest magnitude value. Use the function in
    6·1 answer
  • What are the characteristics of computer. Explain any one​
    15·2 answers
  • Which keyboard shortcut would you press to copy cells that are selected in a spreadsheet?
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!