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
Why can’t binary data be represented directly in XML, for example, by representing it as Unicode byte values? XML elements can c
notsponge [240]

Answer:

I don't know this answer ask another one

5 0
3 years ago
What will happen on our music in our society now if these movement doesn't exist like impressionism, expressionism, electronic a
Mrrafil [7]

Expressionist music is a more abstract interpretation of traditional Western tones that aims to elicit strong emotions. Impressionist music, on the other hand, is all about capturing the mood of the moment.

<h3>What is Expressionist?</h3>

Expressionism is a modernist progression that began in Northern Europe around the turn of the twentieth century, initially in poetry and painting.

Its distinguishing feature is to present the world solely from a subjective point of view, distorting it dramatically for emotional effect in order to elicit moods or ideas.

Expressionist music is a more abstract interpretation of traditional Western tones, with the goal of eliciting strong emotions.

The goal of Impressionist music, on the other hand, is to capture the mood of the moment.

Thus, this will happen on the given scenario.

For more details regarding Expressionist, visit:

brainly.com/question/2189275

#SPJ1

3 0
1 year ago
Suppose we have the list: list = [1,2,3,4,5,6]
mixer [17]
1, 2, 3, 10, 5, 6 is the answer
6 0
2 years ago
LA SUMA DE DOS NÚMEROS ES 10 Y LA SUMA DE SUS CUADRADOS 58, HALA LOS NÚMEROS
natta225 [31]

Answer:

3 y 7

Explanation:

Los números cuya suma es 10 y suma de cuadrados es 58 son los números 3 y 7

Sea "a" y "b" los dos números entonces su suma es 10:

a + b = 10

1. a = 10 - b

La suma de sus cuadrados es 58:

2. a² + b² = 58

Sustituyo la ecuación 1 en la ecuación 2:

(10 -b)² + b² = 58

100 - 20b + b² + b² = 58

2b² - 20b + 100 - 58 = 0

b² - 10b + 21 = 0

(b - 7)*(b-3) = 0

b = 7 o b = 3

Tenemos un número que será "a" y el otro que será "b", entonces los números son 7 y 3

6 0
2 years ago
Dan wants to check the layout of his web page content. What is the best way in which he can do this?
KatRina [158]
The answer is b I hope it helped
4 0
3 years ago
Read 2 more answers
Other questions:
  • Write three tasks students can preform in a digital classroom?
    9·2 answers
  • Design and implement your own simple class to represent any household item of your choice (toaster, fan, hair dryer, piano ...)
    11·1 answer
  • Interpretations of the AICPA Code of Professional Conduct are dominated by the concept of: Question 4 options: 1) independence.
    5·1 answer
  • What is the name given to a collection of related fields such as ID number,name and address?
    12·1 answer
  • All of the following are organization habits except:
    10·1 answer
  • How do I create a powershell script to make a Windows user account that is a non-admin user
    7·1 answer
  • Write a program to compute the maximum and minimum value of three numbers:
    10·1 answer
  • what would you have to do to delete a document from your computer so that it could not possibly be read by anyone else?
    10·1 answer
  • What does project manager do?
    11·1 answer
  • 1) Which of the following would you NOT see on a Windows 10 Start menu?
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!