Question: Implement the Searcher class's binarySearch ( ) generic method in the Searcher.java file. Access Searcher.java by clicking on the orange arrow next to BinarySearch.java at

Implement the Searcher class's binarySearch() generic method in the Searcher.java file. Access Searcher.java by clicking on the orange arrow next to BinarySearch.java at the top of the coding window. The method performs a binary search on the sorted array (first parameter) for the key (third parameter). binarySearch() returns the key's index if found, -1 if not found.
Compare an array element to the key using the compare() method of the comparer object passed as binarySearch()'s last parameter. comparer.compare(a, b) returns an integer:
greater than 0 if a > b
less than 0 if a < b
equal to 0 if a == b
A few test cases exist in the main method to test binarySearch() with both string searches and integer searches. Clicking "Run program" will display test case results, each starting with "PASS" or "FAIL". Ensure that all tests are passing before submitting code.
Each test in the main method only checks that binarySearch() returns the correct result, but does not check the number of comparisons performed. The unit tests in the submit mode check both binarySearch()'s return value and the number of comparisons perfimport java.io.*;
import java.util.*;
public class BinarySearch {
public static void main(String[] args){
// Perform sample searches with strings
String[] sortedFruits ={ "Apple", "Apricot", "Banana", "Blueberry",
"Cherry", "Grape", "Grapefruit", "Guava", "Lemon", "Lime",
"Orange", "Peach", "Pear", "Pineapple", "Raspberry", "Strawberry"
};
int sortedFruitsSize = sortedFruits.length;
String[] fruitSearches ={ "Nectarine", "Mango", "Guava", "Strawberry",
"Kiwi", "Apple", "Raspberry", "Carrot", "Lemon", "Bread"
};
int fruitSearchesSize = fruitSearches.length;
int[] expectedFruitSearchResults ={-1,-1,7,15,-1,0,14,-1,8,-1
};
StringComparer stringComparer = new StringComparer();
PrintSearches stringResults = new PrintSearches();
stringResults.print(sortedFruits, sortedFruitsSize, fruitSearches,
fruitSearchesSize, stringComparer, expectedFruitSearchResults, true);
// Perform sample searches with integers
Integer[] integers ={11,21,27,34,42,58,66,71,72,85,88,91,98
};
Integer[] integerSearches ={
42,23,11,19,87,98,54,66,92,1,14,21,66,87,83
};
int[] expectedIntegerSearchResults ={
4,-1,0,-1,-1,12,-1,6,-1,-1,-1,1,6,-1,-1
};
IntComparer intComparer = new IntComparer();
PrintSearches integerResults = new PrintSearches();
integerResults.print(integers, integers.length, integerSearches,
integerSearches.length, intComparer, expectedIntegerSearchResults,
false);
}
}ormed.
Need to come back with all of them passing

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!