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

home / study / engineering / computer science / computer science questions and a

ID: 3737523 • Letter: H

Question

home / study / engineering / computer science / computer science questions and answers / java language: download the chapter 17 source code from www.pearsonhighered.com/gaddis, and ...

Question: Java Language: Download the Chapter 17 source code from www.pearsonhighered.com/gaddis, and you w...

(1 bookmark)

Java Language: Download the Chapter 17 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.

Instructions to get the file names.txt:

1)Go to the link. 2)Click on edition 3)Find Starting Out with Java: From Control Structures through Data Structures, 3/E. 4)Click on source code. 5) Extract the folder. 6)Click on the sourcecode. 7)Click on chapter 17 and names.txt should be there.

Explanation / Answer

Please find my implementation:

import java.util.*;
import java.io.*;

public class DisplayFiledataInSortedOrder {

   public static void main(String[] args) {

       // Read the text file and store them into two arrays:
       try {
           List<Integer> column1 = new ArrayList<Integer>(); // Defining an integer Array List
           List<Integer> column2 = new ArrayList<Integer>(); // 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 < arg.length; i++)
           if (arg[i] == number) count++;

       System.out.println( number + " is repeated " + count + " times in the second column.");
   } // End of occurrence method


   public static void Occurrences(Integer[] ary) { // Defining occurrenceS method
       // Finding the maximum occurrences:
       Map<Integer, Integer> m = new HashMap<Integer, Integer>();

       for (int a : ary) {
           Integer freq = m.get(a);
           m.put(a, (freq == null) ? 1 : freq + 1);
       }

       int max = -1;
       int mostFrequent = -1;

       for (Map.Entry<Integer, Integer> e : m.entrySet()) {
           if (e.getValue() > max) {
               mostFrequent = e.getKey();
               max = e.getValue();
           }
       }

       System.out.println( mostFrequent + " is the most repeated number in second column." );
   } // End of occurrenceS method


}