Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA HELP Write a telephone lookup program. Read a data set of 1000 names and te

ID: 3709500 • Letter: J

Question

JAVA HELP Write a telephone lookup program. Read a data set of 1000 names and telephone numbers from files that contain contact information. Your program has to work like a caller ID system, where given a number it displays the name. (There will be no duplicate names or numbers.) Use selection sort for sorting. Use a binary search for lookup.

The program has to do two things: read in the data and handle user queries.

1. The data is in two files. For each person, the name is listed in one file and the phone number is listed in the other. The first phone number belongs to the first person, the second number to the second person etc. Create a Contact class (name and phone number are the only attributes), make Contact objects and put them in an array.

2. The program should prompt the user with this message: “Please enter a phone number, or * to quit.” The user enters a phone number, and the program prints the name and the phone number (separated by a space) for the corresponding contact. If a match cannot be found, the program should print “Unknown number.” Repeat this until the user enters *.

names.txt

Eduardo Ashley

Aryana Foley

Emmy Richmond

Alfredo Meadows

Chaz Bailey

Kael Hurst

Jayden Ho

Alissa Armstrong

Andre Warner

Emelia Duffy

Lilah Oneal

Reece Saunders

Leslie Long

Carleigh Roy

Lauryn Kelley

Yahir Gibson

Jon Farrell

Noel Keller

Prince Rush

Mohamed Ferguson

Kenny Welch

Vaughn Cortez

Davin Taylor

Ellis Krause

Camren Ewing

Gustavo Larson

Adyson Sutton

Eric Carroll

Sadie Ryan

Dale Montoya

Braylen Lopez

Karen Blankenship

Cristofer Gamble

Destiney Goodwin

Diya Bradley

Xander Holden

Colt Petty

Kason Clements

Amirah Norton

Griffin Woodard

Mathias Meza

Kamren Haynes

Yaretzi Dunlap

Maxim Ware

Joshua Blackburn

Talan Daniels

Arturo Barron

Jessie Flowers

Amare Riddle

Jamie Lucas

Juliette Gentry

Maxwell Wilkinson

Elizabeth Joseph

Mila Mullins

phonenumbers.txt

486-685-8308

711-493-6467

871-805-7191

914-344-2341

972-724-5804

688-676-8778

533-157-4369

773-441-5964

982-299-7011

738-457-5223

584-697-4026

640-546-8525

676-413-4555

346-798-2986

197-558-7174

981-219-5396

629-886-0613

264-695-7432

578-443-3637

143-383-2607

177-653-7196

512-256-3102

580-274-8598

103-408-1270

794-811-5176

517-936-8730

557-544-6007

259-206-0803

130-288-9748

508-125-7545

402-978-6192

456-224-8998

834-480-4026

104-999-3709

929-108-4589

806-359-7192

979-845-1137

115-268-7117

688-208-6629

852-965-4901

201-932-8537

714-617-0074

365-176-3853

508-226-6881

525-361-5968

577-969-7112

767-364-8075

909-304-2026

251-860-7513

313-916-2862

981-632-5070

834-570-4414

405-745-8109

663-575-4411

Explanation / Answer

Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
NOTE: Please make sure you place your input file in the correct folder. If using eclipse, the file should be in the project directly and NOT INSIDE src folder.


Contact.java
---------------
public class Contact {
private String name;
private String phone;

public Contact(String name, String phone)
{
this.name = name;
this.phone = phone;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}


}


ContactDriver.java
-------------------


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ContactDriver {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);
String nameFile, phoneFile;

Contact[] contacts = new Contact[1000];
int n = 0;

System.out.print("Enter the filename containing names: ");
nameFile = keyboard.next();

System.out.print("Enter the filename containing phone numbers: ");
phoneFile = keyboard.next();

try {
n = loadFiles(nameFile, phoneFile, contacts);
sortOnPhone(contacts, n);

while(true)
{
System.out.print("Enter phone number to lookup (enter * to quit): ");
String number = keyboard.next();

if(number.equals("*"))
break;

int index = binarySearchPhone(contacts, n, number);
if(index == -1)
System.out.println("Sorry! " + number + " is not found in the contact list.");
else
System.out.println(number + " belongs to " + contacts[index].getName());
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}

}


private static int loadFiles(String nameFile, String phoneFile, Contact[] contacts) throws FileNotFoundException
{
Scanner file1 = new Scanner(new File(nameFile));
Scanner file2 = new Scanner(new File(phoneFile));
int n = 0;

while(file1.hasNextLine())
{
contacts[n] = new Contact(file1.nextLine(), file2.nextLine());
n++;
}

file1.close();
file2.close();
return n;
}

private static void sortOnPhone(Contact[] contacts, int n)
{
int minIdx;

//selection sort
for(int i = 0; i < n; i++)
{
minIdx = i;
for(int j = i+1; j < n; j++)
{
if(contacts[j].getPhone().compareTo(contacts[minIdx].getPhone()) < 0)
minIdx = j;
}

if(i != minIdx) //should swap?
{
Contact temp = contacts[i];
contacts[i] = contacts[minIdx];
contacts[minIdx] = temp;
}
}
}

private static int binarySearchPhone(Contact[] contacts, int n, String search)
{
int first = 0, last = n-1;
int mid;
while(first <= last)
{
mid = (first + last) /2;
if(contacts[mid].getPhone().equals(search)) //found, return mid
return mid;
else if(search.compareTo(contacts[mid].getPhone()) < 0)
last = mid - 1;
else
first = mid + 1;
}

return -1; //not found
}

}

output
======
Enter the filename containing names: names.txt
Enter the filename containing phone numbers: phonenumbers.txt
Enter phone number to lookup (enter * to quit): 738-457-5223
738-457-5223 belongs to Emelia Duffy
Enter phone number to lookup (enter * to quit): 533-157-4369
533-157-4369 belongs to Jayden Ho
Enter phone number to lookup (enter * to quit): 111-222-3333
Sorry! 111-222-3333 is not found in the contact list.
Enter phone number to lookup (enter * to quit): *