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

help me solve this java assaignmet r9 Sorting and Searching Arrays 6. Phone Numb

ID: 3741102 • Letter: H

Question

help me solve this java assaignmet

r9 Sorting and Searching Arrays 6. Phone Number Lookup Recall that Programming Exercise 7 in Chapter 8 asked you to design a program with two parallel arrays: a string array named people and a String array named phonexumbers. The program allows you to search for a person's name in the people array. If the name is found, it displays that person's phone numbe Modify the program so it uses the binary search algorithm instead of the sequen- tial search algorithm. r. 686169000-ETO

Explanation / Answer

Assuming that the array names(people) is given as input in sorted lexicographically (in alphabetical order), which is a mandatory requirement in order to perform binary search, the following is the java code:

import java.io.*;

import java.util.*;

public class TestClass {

public static void main(String[] args) {

Scanner in=new Scanner(System.in);

System.out.println("Enter the number of people : ");

int n = in.nextInt();

String[] people = new String[n];

String[] phoneNumbers = new String[n];

int i,j;

System.out.println("Enter the name of the person and phone number in the next line respectively : ");

for(i=0;i<n;i++)

{

people[i] = in.next();

phoneNumbers[i] = in.next();

}

System.out.println("Enter the name of the person whose phone number is to be found : ");

String nameToFind = in.next();

int beg=0,end=n-1,mid;

//Binary Search

while (beg<=end)

{

mid=(beg+end)/2;

if(people[mid].equals(nameToFind))

{

System.out.println("The phone number is: "+phoneNumbers[mid]);

break;

}

else if(people[mid].compareTo(nameToFind)>0)

{

end = mid-1;

}

else if(people[mid].compareTo(nameToFind)<0)

{

beg = mid+1;

}

}

if(beg>end)

System.out.println("Name not found");

}

}

The above code prompts the user to input the number of people and enter the name and phone number respectively.