Java Programming Your task in this assignment is to use the

Java Programming

Your task in this assignment is to use the quicksort algorithm to sort the women\'s names in the file. The file accompanying the problem is a binary file. You are to read the contents of the binary file into either a string array or an ArrayList. If you are using an array, you will have to determine the size of the list to create the correct array.

The binary file has a list of names that your program is to sort and display in ascending alphabetical order.

Finally give the user an opportunity to search for any name in the list using the most efficient search algorithm discussed in this chapter. Use of dialogboxes is encouraged for a nicer looking user interface (UI).

The package name should be names.

Solution

This is the utility class which reads the file, sorts the names and searches for the requested name.

Use this utility class to perform the function and call from main class.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;


public class NamesUtil {
  
   private static final String EOF = null;
   private String[] names;
  
   /**
   * Reading the file into array of String
   *
   * @param path
   * @param fileName
   * @throws IOException
   */
   public void readFile(String path, String fileName) throws IOException {
       File file = new File(path, fileName);
       FileInputStream fis = new FileInputStream(file);
       BufferedReader br = new BufferedReader(new InputStreamReader(fis));
      
       String line = \"\";
       ArrayList<String> namesRead = new ArrayList<String>();
       names = new String[(int)file.length()];
      
       while((line = br.readLine()) != EOF) {
           namesRead.add(line);
       }
      
       names = (String[]) namesRead.toArray();
   }
  
   /**
   * Sorting the names in ascending order
   *
   * @return sortedNames
   */
   public String[] sort() {
       String[] sortedNames = null;
       if(names != null) {
           sortedNames = names.clone();
           for(int i = 0; i < sortedNames.length; i++) {
               for(int j = i; j < sortedNames.length; j++) {
                   if(sortedNames[j].compareTo(sortedNames[i]) > 0) {
                       String temp = sortedNames[i];
                       sortedNames[i] = sortedNames[j];
                       sortedNames[j] = temp;
                   }
               }
           }
        }
      
       return sortedNames;
   }
  
   /**
   * Binary Search to search the name;
   *
   * @param name
   * @return index
   */
   public int search(String name) {
       String[] sortedNames = null;
       int first, last, middle;
       if(names != null) {
           sortedNames = sort();
           first = 0;
           last = sortedNames.length - 1;
           middle = (first + last) / 2;
          
           while(first <= last) {
               if(sortedNames[middle].compareTo(name) < 0) {
                   first = middle + 1;
               }
               else if(sortedNames[middle].compareTo(name) > 0) {
                   last = middle - 1;
               }
               else
                   return middle;
           }
        }
      
       return -1;
   }

}

Java Programming Your task in this assignment is to use the quicksort algorithm to sort the women\'s names in the file. The file accompanying the problem is a b
Java Programming Your task in this assignment is to use the quicksort algorithm to sort the women\'s names in the file. The file accompanying the problem is a b

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site