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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education