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
choli [55]
3 years ago
5

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:
morpeh [17]3 years ago
4 0

Answer:

See explaination

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.

atparam 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.

atparam k the key to find

atreturn 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.

atparam v the value to find

atreturn 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

Note: Replace all the "at" with at symbol

You might be interested in
Which of these functions may be used with positional arguments? Select four options.
jeyben [28]

Answer:

AVERAGE

COUNT

SUM

MAX

Explanation:

The functions that may be used with positional arguments include:

AVERAGE

COUNT

SUM

MAX

The basic functions formulae include SUM, AVERAGE, COUNT, MAX and MIN.

SUM: This is the sum total of all the numbers in the rows.

COUNT: This is the used to count the range of cells that contains numbers.

AVERAGE: This is the average of all the numbers in the rows.

MIN: This is the lowest number in the rows.

MAX: This will return the highest number that is found in the set of values.

It should be noted that the COUNTIF is not used with positional arguments.

8 0
3 years ago
A eclipse occurs at a full moon when the moon passes between earth and the sun
sergeinik [125]

Answer:

When the moon aligns with the sun

Explanation:

common sense

3 0
2 years ago
Porque es importante la tecnología?​
galina1969 [7]

Explanation:

La tecnología se refiere a la colección de herramientas que hacen más fácil usar, crear, administrar e intercambiar información. El desarrollo de alta tecnología ha ayudado a conquistar las barreras de comunicación y reducir la brecha entre la gente de todo el mundo.

3 0
3 years ago
How do i stop my computer from automatically connecting to a wireless network
Karolina [17]

Answer:

:p

Explanation:      

Should be in wireless network settings.

or

If you have a home wifi or network, lock into that (via connecting and clicking connect automatically or clicking stay connected- depending on the network band and type as well as your computer model and make)

Hope this helps :P    

Have a nice day (or night)

8 0
3 years ago
Which of the following barriers to oral communication is not the fault of the sender or receiver? being unprepared noise not pay
vredina [299]
All of the given choices are indeed common barriers to oral communication. But among them, "noise" is the only one which may not be the fault of the sender of the receiver. Noise can be generated by nearby sources independent of the sender or receiver.
6 0
3 years ago
Read 2 more answers
Other questions:
  • Human factors is the study of:
    5·1 answer
  • What is a googleplex?
    7·1 answer
  • What are the features of Cobol language that make it suitable for programming business applications.
    6·1 answer
  • ___________ is a document that describes the locations of variables in the data file and lists the assignments of codes to the a
    12·1 answer
  • A finally clause will execute:
    8·1 answer
  • Explain the main components of a computer system​
    12·1 answer
  • How do you open an application on the macOS?
    11·2 answers
  • Differentiate between patent and copyright.
    6·2 answers
  • How do you save a document in a format so that any reader can view it?
    5·1 answer
  • What option can be used by a system administrator to ensure that dynamic updates are made only by known clients when you have a
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!