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

java programming The file attached contains all 50 state prefixes in a text file

ID: 3804875 • Letter: J

Question

java programming

The file attached contains all 50 state prefixes in a text file. The states are list in no particular order. You are to write a method that will open the text file (you should have appropriate exception handling), read the data into an array and then sort the data appropriately. Then, write a method that will appropriately search the sorted array of state abbreviations. The method will have as it's arguments, the array holding the states and the search value. The method should use a recursive binary search algorithm to carry out the search.

States.txt

FL GA SC NC VA MD NY NJ DE PA CT RI MA VT NH ME AL TN KY WV OH MI MS AR MO KS NE IN IL WI MN LA TX OK IA SD ND NM CO WY ID AZ UT NV MT CA OR WA AL HI

Explanation / Answer

StatePrefix.java:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;


public class StatePrefix {

   public static void main(String[] args) throws IOException {
       BufferedReader reader = null;
       try {
           reader = new BufferedReader(new FileReader("input.txt"));
       } catch (FileNotFoundException e) {
           System.out.println("File input.txt not found in classpath");
       }
      
       String [] prefixList;
       String strLine;
       int count=0;
      
       prefixList = new String[20];
      
       while ((strLine = reader.readLine()) != null) {
       // Store the content into an array
       Scanner s = new Scanner(strLine);
       while(s.hasNext()) {
       if (prefixList.length == count) {
       // expand list
       prefixList = Arrays.copyOf(prefixList, prefixList.length + 20);
       }
       prefixList[count] = s.next();
       count++;
       }
       s.close();
       }
       // reduce any extra length from the array
       prefixList = Arrays.copyOf(prefixList, count);

       System.out.println("Original Array:");
       printArray(prefixList);
      
       // sort the array now
       Arrays.sort(prefixList);
      
       System.out.println("After Sorting:");
       printArray(prefixList);

       System.out.println("Binary Search for CO, pos: " + binarySearch(prefixList, "CO"));
       System.out.println("Binary Search for MA, pos: " + binarySearch(prefixList, "MA"));
      
       // RC doesn't exist
       System.out.println("Binary Search for RC, pos: " + binarySearch(prefixList, "RC"));
      
       if(reader != null)
           reader.close();
   }
  
   static void printArray(String[] s) {
       for(String a:s) {
           System.out.print(a + " ");
       }
       System.out.println();
   }

   // search for string in sorted array
   static public int binarySearch(String[] inputArr, String key) {
  
int start = 0;
int end = inputArr.length - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (key.compareTo(inputArr[mid]) == 0) {
return mid + 1;
}
if (key.compareTo(inputArr[mid]) < 1) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return -1;
}
}

Input.txt:

FL GA SC NC VA MD NY NJ DE PA CT RI MA VT NH ME AL TN KY WV OH MI MS AR MO KS NE IN IL WI MN LA TX OK IA SD ND NM CO WY ID AZ UT NV MT CA OR WA AL HI


Sample Output:
Original Array:
FL GA SC NC VA MD NY NJ DE PA CT RI MA VT NH ME AL TN KY WV OH MI MS AR MO KS NE IN IL WI MN LA TX OK IA SD ND NM CO WY ID AZ UT NV MT CA OR WA AL HI
After Sorting:
AL AL AR AZ CA CO CT DE FL GA HI IA ID IL IN KS KY LA MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK OR PA RI SC SD TN TX UT VA VT WA WI WV WY
Binary Search for CO, pos: 6
Binary Search for MA, pos: 19
Binary Search for RC, pos: -1