I had to make two classes that search a String array for specific words, and a ItemNotFoundException class. I do not think the exception class is working correctly though. I am not completely sure though because I am unsure of what the final outcome should in fact be.
Driver:
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class HW5Driver { public final static String FILE_AND_PATH = "longwords.txt"; /* * TODO: Be sure to change the FILE_AND_PATH to point to your local * copy of longwords.txt or a FileNotFoundException will result */ //Note how we deal with Java's Catch-or-Declare rule here by declaring the exceptions we might throw public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File(FILE_AND_PATH)); int wordCount = 0; ArrayList<String> theWords = new ArrayList<String>(); //read in words, count them while(input.hasNext()) { theWords.add( input.next() ); wordCount++; } //make a standard array from an ArrayList String[] wordsToSearch = new String[theWords.size()]; theWords.toArray(wordsToSearch); //start with the linear searches tryLinearSearch(wordsToSearch, "DISCIPLINES"); tryLinearSearch(wordsToSearch, "TRANSURANIUM"); tryLinearSearch(wordsToSearch, "HEURISTICALLY"); tryLinearSearch(wordsToSearch, "FOO"); //and compare these results to the binary searches tryBinarySearch(wordsToSearch, "DISCIPLINES"); tryBinarySearch(wordsToSearch, "TRANSURANIUM"); tryBinarySearch(wordsToSearch, "HEURISTICALLY"); tryBinarySearch(wordsToSearch, "FOO"); } /** * Method tryBinarySearch * precondition: wordsToSearch is a nonempty array of Strings, and target is a non-null string to search for * in our collection of strings * postcondition: Uses a BinarySearch object (which implements this style of search) to try to find the target string */ private static void tryBinarySearch(String[] wordsToSearch, String target) { //Todo: Build a LinearSearch class that inherits from SearchAlgorithm, and put it in the same directory as this class to successfully compile SearchAlgorithm bs = new BinarySearch(); try { System.out.print( target + " found at index: " + bs.search(wordsToSearch,target)); System.out.println( " taking " + bs.getCount() + " comparisons."); } catch( ItemNotFoundException e ) { System.out.println( target + ":" + e.getMessage()); } } /** * Method tryLinearSearch * precondition: wordsToSearch is a nonempty array of Strings, and target is a non-null string to search for * in our collection of strings * postcondition: Uses a LinearSearch object to try to find the target string */ private static void tryLinearSearch(String[] wordsToSearch, String target) { //Todo: Build a LinearSearch class that inherits from SearchAlgorithm, and put it in the same directory as this class to successfully compile SearchAlgorithm bs = new LinearSearch(); try { System.out.print( target + " found at index: " + bs.search(wordsToSearch,target)); System.out.println( " taking " + bs.getCount() + " comparisons."); } catch( ItemNotFoundException e ) { System.out.println( target + ":" + e.getMessage()); } } }
ItemNotFoundException class:
public class ItemNotFoundException extends Exception{ public ItemNotFoundException(){ super("Word not found."); } public ItemNotFoundException(String message){ super(message); } }
--- Update ---
LinearSearch class:
public class LinearSearch extends SearchAlgorithm{ public int search(String[] words, String wordToFind){ for(int i = 0; i < words.length ; i++){ if(words[i].equals(wordToFind)){ break; }else{ incrementCount(); } } return getCount(); } }
SearchAlgorithm class:
public abstract class SearchAlgorithm { /** * Method search: a "to-be-defined" method used to implement a specific search strategy over the given array looking for the target word * Precondition: words is a nonempty array and target is a non-null string * Postcondition: if the target word is found, return its index. * If not found, throw an ItemNotFoundException (a subclass which you have to make) * */ public abstract int search(String[] words, String wordToFind) throws ItemNotFoundException; /** * Utility Features: This class can be used to track the number of search comparisons * for use in comparing two different search algorithms */ private int count = 0; public void incrementCount() { count++; } public int getCount() { return count; } public void resetCount() { count = 0; } }