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

The problem is says. Download the Chapter 16 source code from www.pearsonhighere

ID: 3639597 • Letter: T

Question

The problem is says. Download the Chapter 16 source code from www.pearsonhighered.com/gaddis, and you will a find a text file of names.txt. This file contains a list of 65 most popular female first names in the USA. Write a program that reads these names into a String array. Use the Quicksort algorithm to sort the names in ascending order. Then allow the user to search for a name in the array. Use binary search algorithm to perform the search. I already have the quicksort and binary search algorithims for other problems and they work with objects.

Explanation / Answer

import java.io.*; import java.util.*; class DisplayFiledataInSortedOrder { public static void main(String[] args) throws Exception { Scanner input=new Scanner(System.in); System.out.println("Enter File name with path: "); String filename=input.next(); File f=new File(filename); BufferedReader br=new BufferedReader(new FileReader(f)); StringBuffer buffer=new StringBuffer(); String str; while((str=br.readLine())!=null){ buffer.append(str); buffer.append(" "); } ArrayList list=new ArrayList(); StringTokenizer st = new StringTokenizer(buffer.toString().toLowerCase()); while(st.hasMoreTokens()) { String s = st.nextToken(); list.add(s); } HashSet set = new HashSet(list); List arrayList = new ArrayList(set); Collections.sort(arrayList); for (Object ob : arrayList) System.out.println(ob.toString()); } } or import java.util.*; //By Mehdi Davoudi import java.lang.*; import java.io.*; public class list { public static void main(String[] args) { // Read the text file and store them into two arrays: try { List column1 = new ArrayList(); // Defining an integer Array List List column2 = new ArrayList(); // Defining an integer Array List Scanner myfile = new Scanner(new FileReader("c:/java/data.txt")); // Reading file using Scanner while (myfile.hasNext()) { // Read file content using a while loop column1.add(myfile.nextInt()); // Store the first integer into the first array list column2.add(myfile.nextInt()); // Store the next integer into the second array list } myfile.close(); // close the file System.out.println("column 1 elements are: " + column1); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] System.out.println("column 2 elements are: " + column2); // [12, 15, 6, 4, 3, 6, 12, 8, 8, 9, 13] //Getting a number(1-3) from user: Scanner cases = new Scanner(System.in); System.out.println("Enter a number between 1-3: "); int num = cases.nextInt(); Integer[] temp = new Integer[column2.size()]; switch (num) { case 1: Scanner case1 = new Scanner(System.in); System.out.println("Enter a number from first column to see its pair in second column: "); int numb = case1.nextInt(); System.out.println(column2.get(numb)); break; case 2: Occurrence(column2.toArray(temp)); break; case 3: Occurrences(column2.toArray(temp)); break; default: System.out.println("the number is not 1 or 2 or 3!"); } } catch (Exception e) { // we defined it just in the case of error e.printStackTrace(); // shows the error } } // End of MAIN public static void Occurrence(Integer[] arg) { // Defining occurrence method int count = 0; //Getting a number from user input: Scanner userin = new Scanner(System.in); System.out.println("Enter an integer number: "); int number = userin.nextInt(); // Finding the occurrences: for (int i = 0; i max) { mostFrequent = e.getKey(); max = e.getValue(); } } System.out.println( mostFrequent + " is the most repeated number in second column." ); } // End of occurrenceS method }