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
Write a shell (text-based) program, called first_word.py, that opens the file stuff.txt and prints out the first word of every l
postnew [5]

Answer:

See explaination

Explanation:

Please check below for the code for first_word.py file which will print first word of an input file stuff.txt. So the open function will open file in read mode. We had maintained a count variable so that we can skip printing first word of first line. Also the command line.strip() checks whether string is empty or not so that we will not get index error while calling split function over line to get first word.

#!/usr/local/bin/python

stuff = open("stuff.txt", "r");

count = 0;

for line in stuff:

count+=1;

if (count>1 and line.strip()):

print(line.split(maxsplit=1)[0])

3 0
2 years ago
A good first step to understanding any kind of text is to :
Vitek1552 [10]

Answer:

find the main idea

Explanation:

6 0
2 years ago
Which of the following is NOT a way the media communicates unrealistic body images?
Leokris [45]

The media communicates unrealistic body image through various ways, which are mentioned in the available options for the question. It definitely digitally alters images that they use for their various publications, and it also provides forums to discuss actor and actresses, including their looks.

The media also often criticizes high-profile individual’s current body condition – whether they are too underweight or too overweight. Thus, the best answer for the question would be (D) promoting all body types.

6 0
3 years ago
Read 2 more answers
Which of the following is a good choice to help you stay organized when you are away from the office? A. Personal Information ma
Free_Kalibri [48]

Answer:

A

Explanation:

6 0
2 years ago
What is cloud storage?​
Aleks [24]

Cloud storage is a service model in which data is maintained, managed, backed up remotely and made available to users over a network (typically the Internet).

3 0
2 years ago
Other questions:
  • A good reference point for determining the position of a line or curb in front of you is your __________ . A. Hood ornament B. L
    10·1 answer
  • Computers store temporary Internet files in the Recycle Bin. These files take up space and slow down a computer. Which tool can
    10·1 answer
  • Translation of a file into a coded Format that occupies less space than the original file is called
    6·1 answer
  • Four major parts of the keyboard
    12·2 answers
  • True or False: The major advantage of Arrays over ArrayLists in Java is the fact that while ArrayLists are fixed in size, an Arr
    15·1 answer
  • Question 1 (1 point)
    5·2 answers
  • A variable is assigned a value on line 328 of a program. Which of the following must be true in order for the variable to work?
    12·2 answers
  • Guys how can i video my self from my laptop <br> my laptop is (lenovo)
    8·2 answers
  • Do OBS mic filters apply everywhere? Such as discord and other apps similar. Or is it just on streaming?
    15·1 answer
  • What is the official name of an application on a desktop or a laptop?.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!