I need help with this Java problem to output as it's explained in this image below: import 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); } } import java.util.*; // StringComparer inherits from Comparator and so provides the // ability to compare two String objects. public class StringComparer implements Comparator { @Override public int compare(String a, String b) { return a.compareTo(b); } } import java.util.*; // IntComparer inherits from Comparator and so provides the // ability to compare two integers. public class IntComparer implements Comparator { @Override public int compare(Integer a, Integer b) { return a - b; } } Help edit only Searcher.java code below import java.util.*; public class Searcher { // Returns the index of the key in the sorted array, or -1 if the // key is not found. public static int binarySearch(T[] array, int arraySize, T key, Comparator comparer) { // Your code here return -1; } } import java.util.*; public class PrintSearches { public void print(T[] sortedArray, int sortedArraySize, T[] searchKeys, int searchKeysSize, Comparator comparer, int[] expectedResults, boolean keyInQuotes) { // If keyInQuotes is true, " characters surround the key in output // statements. Otherwise empty strings surround the key. String extra = keyInQuotes ? "\"" : ""; // Iterate through array of search keys and search for each for (int i = 0; i < searchKeysSize; i++) { // Get the key to search for var searchKey = searchKeys[i]; // Perform the search Searcher searcher = new Searcher(); int index = searcher.binarySearch(sortedArray, sortedArraySize, searchKey, comparer); // Compare actual result against expected int expected = expectedResults[i]; if (index == expected) { System.out.print("PASS: Search for key "); System.out.print(extra); System.out.print(searchKey); System.out.print(extra); System.out.print(" returned "); System.out.print(expected); System.out.print("."); System.out.print("\n"); } else { System.out.print("FAIL: Search for key "); System.out.print(extra); System.out.print(searchKey); System.out.print(extra); System.out.print(" should have returned "); System.out.print(expected); System.out.print(", but returned "); System.out.print(index); System.out.print("."); System.out.print("\n"); } } } }
I need help with this Java problem to output as it's explained in this image below:
import 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<String> stringResults = new PrintSearches<String>();
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<Integer> integerResults = new PrintSearches<Integer>();
integerResults.print(integers, integers.length, integerSearches,
integerSearches.length, intComparer, expectedIntegerSearchResults,
false);
}
}
import java.util.*;
// StringComparer inherits from Comparator<String> and so provides the
// ability to compare two String objects.
public class StringComparer implements Comparator<String> {
@Override
public int compare(String a, String b) {
return a.compareTo(b);
}
}
import java.util.*;
// IntComparer inherits from Comparator<Integer> and so provides the
// ability to compare two integers.
public class IntComparer implements Comparator<Integer> {
@Override
public int compare(Integer a, Integer b) {
return a - b;
}
}
Help edit only Searcher.java code below
import java.util.*;
public class Searcher<T> {
// Returns the index of the key in the sorted array, or -1 if the
// key is not found.
public static <T> int binarySearch(T[] array, int arraySize, T key,
Comparator<T> comparer) {
// Your code here
return -1;
}
}
import java.util.*;
public class PrintSearches<T> {
public void print(T[] sortedArray, int sortedArraySize, T[] searchKeys,
int searchKeysSize, Comparator <T> comparer, int[] expectedResults,
boolean keyInQuotes) {
// If keyInQuotes is true, " characters surround the key in output
// statements. Otherwise empty strings surround the key.
String extra = keyInQuotes ? "\"" : "";
// Iterate through array of search keys and search for each
for (int i = 0; i < searchKeysSize; i++) {
// Get the key to search for
var searchKey = searchKeys[i];
// Perform the search
Searcher<T> searcher = new Searcher<T>();
int index = searcher.binarySearch(sortedArray, sortedArraySize,
searchKey, comparer);
// Compare actual result against expected
int expected = expectedResults[i];
if (index == expected) {
System.out.print("PASS: Search for key ");
System.out.print(extra);
System.out.print(searchKey);
System.out.print(extra);
System.out.print(" returned ");
System.out.print(expected);
System.out.print(".");
System.out.print("\n");
}
else {
System.out.print("FAIL: Search for key ");
System.out.print(extra);
System.out.print(searchKey);
System.out.print(extra);
System.out.print(" should have returned ");
System.out.print(expected);
System.out.print(", but returned ");
System.out.print(index);
System.out.print(".");
System.out.print("\n");
}
}
}
}
![Binary search generic method
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
performed.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F4e135d5c-f867-4afd-9f0d-b505f4f19664%2F942b04b2-5d1f-4240-85e5-1f7b931ee699%2F3n9vdn8_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 4 steps with 6 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)