In Java The data file “BoyNames.txt” contains a list of the most popular names g
ID: 3837115 • Letter: I
Question
In Java
The data file “BoyNames.txt” contains a list of the most popular names given to boys born in the United States from 2000 to 2009.
(Put the following names into a file called BoyNames.txt)
Jacob
Michael
Joshua
Matthew
Daniel
Christopher
Andrew
Ethan
Joseph
William
Write a menu-driven program (“NameSearch.java”) as follows: Read the data file to populate an array / array list. Sort the name list in alphabetic order (Using Java SelectionSort technique). Prompt the user to enter a name to search, then use the binary search algorithm to search the array /array list. The method should return the index if found, -1 otherwise. Output a message indicating whether the name was among the most popular.
(Sample of the Menu)
1 --- Output the names
2 --- Sort the names
3 --- Search a name using binary search algorithm
0 --- Exit
Explanation / Answer
HI, Please find my implementation.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class NameSearch {
public static void selectionSort(ArrayList<String> data) {
// just return if the array list is null
if (data == null)
return;
// just return if the array list is empty or only has a single element
if (data.size() == 0 || data.size() == 1)
return;
// declare an int variable to hold value of index at which the element
// has the smallest value
int smallestIndex;
// declare an int variable to hold the smallest value for each iteration
// of the outer loop
String smallest;
// for each index in the array list
for (int curIndex = 0; curIndex < data.size(); curIndex++) {
/* find the index at which the element has smallest value */
// initialize variables
smallest = data.get(curIndex);
smallestIndex = curIndex;
for (int i = curIndex + 1; i < data.size(); i++) {
if (smallest.compareTo(data.get(i)) > 0) {
// update smallest
smallest = data.get(i);
smallestIndex = i;
}
}
/* swap the value */
// do nothing if the curIndex has the smallest value
if (smallestIndex == curIndex)
;
// swap values otherwise
else {
String temp = data.get(curIndex);
data.set(curIndex, data.get(smallestIndex));
data.set(smallestIndex, temp);
}
}
}
public static void display(ArrayList<String> list){
for(String s : list)
System.out.println(s);
}
public static void main(String[] args) throws FileNotFoundException {
// opening file
Scanner fileScanner = new Scanner(new File("BoyNames.txt"));
Scanner keyboard = new Scanner(System.in);
// creating array list
ArrayList<String> list = new ArrayList<>();
// reading names from file
while(fileScanner.hasNext())
list.add(fileScanner.next());
String key;
int index;
while(true){
System.out.println("1 --- Output the names");
System.out.println("2 --- Sort the names");
System.out.println("3 --- Search a name using binary search algorithm");
System.out.println("0 --- Exit");
int option = keyboard.nextInt();
switch(option){
case 1:
display(list);
break;
case 2:
selectionSort(list);
break;
case 3:
System.out.print("Enter name to be search: ");
key = keyboard.next();
index = Collections.binarySearch(list, key);
if(index < 0)
System.out.println(key+" not avaialble in list");
else
System.out.println(key+" is avaialble at index "+index+" in list");
break;
case 0:
break;
default:
System.out.println("Invalid option!!!");
}
if(option == 0)
break;
System.out.println();
}
}
}
/*
Sample run:
1 --- Output the names
2 --- Sort the names
3 --- Search a name using binary search algorithm
0 --- Exit
1
Jacob
Michael
Joshua
Matthew
Daniel
Christopher
Andrew
Ethan
Joseph
William
1 --- Output the names
2 --- Sort the names
3 --- Search a name using binary search algorithm
0 --- Exit
2
1 --- Output the names
2 --- Sort the names
3 --- Search a name using binary search algorithm
0 --- Exit
3
Enter name to be search: Jacob
Jacob is avaialble at index 4 in list
1 --- Output the names
2 --- Sort the names
3 --- Search a name using binary search algorithm
0 --- Exit
0
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.