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"); } } } }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

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.
Transcribed Image Text: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.
Expert Solution
steps

Step by step

Solved in 4 steps with 6 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning