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
Assume we are using the simple model for floating-point representation as given in this book (the representation uses a 14-bit f
Artemon [7]

Answer:

The representation of 100.0 in the floating-point representation is computed as follows: First convert the given number 100.0 in the binary form. 10010 = 11001002 the binary representation.

Explanation:

3 0
2 years ago
You have a notebook computer and wish to connect to an IEEE 802.11n wireless network. The computer does not have a built-in wire
Ira Lisetskai [31]

Answer:

The wireless adapter to use is the Wireless USB 2.0 Extender

Explanation:

The Wireless USB 2.0 Extender is a USB component that enables a computer to connect to and communicate with other computers on a network, or even to connect to the internet.

It uses an IEEE 802.11g radio platform and communicates within a radio frequency range of 2.4GHz.

Therefore, in cases where a notebook computer does not have a built-in wireless LAN card or PC card interface, you can use the Wireless USB 2.0 Extender as the best solution to that problem.

3 0
2 years ago
Question #7
Elis [28]
The answer to this question is firewall
4 0
2 years ago
व्याख्या c) Differentiate between Raster Image and Vector Image.​
crimeas [40]

Vector images are described by lines, shapes, and other graphic image components stored in a format that incorporates geometric formulas for rendering the image elements

These are the types of images that are produced when scanning or photographing an object. Raster images are compiled using pixels, or tiny dots, containing unique color and tonal information that come together to create the image.

7 0
2 years ago
A constructor is just a special type of ______________ that instantiates an object from the class.
Dmitry_Shevchenko [17]

A constructor exists just a special type of subroutine that instantiates an object from the class.

<h3>What  is constructor?</h3>

A constructor exists as a special kind of subroutine in a class. It maintains the same name as the name of the class, and it has no return type, not even void. A constructor exists called with the new operator in order to create a new object.

A constructor exists as a special process of a class or structure in object-oriented programming that initializes a newly constructed object of that type. Whenever an object exists created, the constructor is called automatically. A constructor in Java exists as a special method that is utilized to initialize objects. The constructor exists called when an object of a class is created.

A subroutine exists as a sequence of program instructions that serves a specific task, packaged as a unit. This unit can then be utilized in programs wherever that separate task should be performed.

Hence,  A constructor exists just a special type of subroutine that instantiates an object from the class.

To learn more about constructor refer to:

brainly.com/question/13267121

#SPJ4

8 0
1 year ago
Other questions:
  • Dams provide what kind of energy ?
    5·2 answers
  • What was the first browser that allowed for graphics to be viewed on the web?
    14·2 answers
  • In working on the conceptual design of the new database, Ken determines that the "office" field in one of the tables must includ
    8·1 answer
  • Why is simplicity important in navigation design?
    13·1 answer
  • What are the words that make up a high-level programming language called?
    6·1 answer
  • Some programmers include scroll bars, title bars, buttons, and menus in a program simply by adding them to a layout through a pr
    10·1 answer
  • Davingould1115...................answer 3​
    11·1 answer
  • Guys, if I'm going back to 505 and it's a 7-hour flight or a 45-minute drive how do I get to 505?
    12·1 answer
  • What does altgr mean on a keyboard.
    10·1 answer
  • (10 LC)
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!