Question: Write a class, ArraySearch that provides three methods: int find_for (E [] haystack, E needle); int find_other (E [] haystack, E needle); int first_duplicate (E
Write a class, ArraySearch
int find_for (E [] haystack, E needle);
int find_other (E [] haystack, E needle);
int first_duplicate (E [] data);
The two find methods search the haystack array to see if any of its elements is the same (as determined by the equals method) as the needle. If there is a match, find returns the index of the first match. Otherwise, find throws the exception java.util.MissingResourceException.
find_for must be implemented using a for loop. find_other must be implemented using methods from the Java standard library, so that your code for find_other has no explicit loops.
The method first_duplicate searches the data array to see if any two different elements of the array are the same, again as determined by the equals method. The return value is the index of the first duplicate found, or -1 if each of the elements of the array is unique, i.e., no element of the array is equal to any other element of the array.
You will also have to write code to test your ArraySearch class.
Algorithm Analysis: Write up a runtime analysis of each of the three methods, giving the big-O of each, and explaining how you got your results.
Use the timing code in Loops.java to verify the code behaves as expected.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
