Question
I have a binary search algorithm, it needs to test 13 cases according to the instructions below: There are 3 conditions that need to be
I have a binary search algorithm, it needs to test 13 cases according to the instructions below:
There are 3 conditions that need to be tested that are related to the positioning of the item being inserted: 1. the lower boundary of the array 2. the upper boundary of the array 3. between the upper and lower boundaries of the array
For each of the three conditions there are two possibilities: 1. the item already exists in the array 2. the item does not exist in the array
Furthermore, the array may contain: 1. an odd number of items, or 2. an even number of items.
The combinations of the above factors yield a total of 3 * 2 * 2, or 12 test categories, assuming the array is not empty. The empty array yields a 13th test category, which should also be tested.
To test your code, do the following:
1. Create a set of 13 test cases, one test case for each of the 13 test categories described above. Recall that every test case should consist of 1) inputs and 2) expected output. Each test case should consist of an array of Comparable objects and an object of the same type to be inserted into the array, as inputs, and the correct insertion point for the item to be inserted, as the expected output. You can create your tests using jUnit or some other testing library, or you can create your own test harness. A test harness is simply a program that passes the inputs of test cases to the code to be tested, and checks the actual output against the expected output. The results are displayed on the screen or written to a testing log file.
2. With your test cases in hand, test your modified binarySearch method.
Here is the code I have:
import java.io.*;
public class BinarySearch
{
public int binarySearch(Comparable[] objArray, Comparable searchObj)
{
int low = 0;
int high = objArray.length - 1;
int mid = 0;
while (low <= high)
{
mid = (low + high) / 2;
if (objArray[mid].toString().toLowerCase().compareTo(searchObj.toString().toLowerCase()) < 0)
//compatibility
{
low = mid + 1;
}
else if (objArray[mid].toString().toLowerCase().compareTo(searchObj.toString().toLowerCase()) > 0)
//compatibility
{
high = mid - 1;
}
else
{
return mid;
}
}
if(objArray[mid].toString().toLowerCase().compareTo(searchObj.toString().toLowerCase()) > 0)
return mid;
else
return mid +1;
}
public static void main (String[]args) {
int index;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Integer obj;
try {
BinarySearch bs = new BinarySearch();
Integer[] intArray = { 90,906, 999, 1009, 6009 };
obj = Integer.parseInt("90");
index = bs.binarySearch(intArray, obj);
System.out.println(" Index where number: " + obj + " is found or should be inserted = " + index);
Integer[] intArray2 = { 90,906, 999, 1009 };
obj = Integer.parseInt("90");
index = bs.binarySearch(intArray2, obj);
System.out.println(" Index where number: " + obj + " is found or should be inserted = " + index);
Integer[] intArr3 = { 90,906, 997, 999, 1009 };
obj = Integer.parseInt("99700");
index = bs.binarySearch(intArr3, obj);
System.out.println(" Index where number: " + obj + " is found or should be inserted = " + index);
Integer[] intArr4 = { 90,906, 997, 999, 1009 };
obj = Integer.parseInt("906");
index = bs.binarySearch(intArr4, obj);
System.out.println(" Index where number: " + obj + " is found or should be inserted = " + index);
Integer[] intArray3 = { 1, 3, 5, 6, 10 };
obj = Integer.parseInt("10");
index = bs.binarySearch(intArray3, obj);
System.out.println(" Index where number: " + obj + " is found or should be inserted = " + index);
Integer[] intArray4 = { 1, 3, 5, 6 };
obj = Integer.parseInt("6");
index = bs.binarySearch(intArray4, obj);
System.out.println(" Index where number: " + obj + " is found or should be inserted = " + index);
Integer[] intArra5 = { 1, 3, 5, 6, 10 };
obj = Integer.parseInt("10");
index = bs.binarySearch(intArra5, obj);
System.out.println(" Index where number: " + obj + " is found or should be inserted = " + index);
Integer[] intArray6 = { 1, 3, 5, 6 };
obj = Integer.parseInt("11");
index = bs.binarySearch(intArray6, obj);
System.out.println(" Index where number: " + obj + " is found or should be inserted = " + index);
Integer[] intArray5 = { 1, 3, 5, 6 };
obj = Integer.parseInt("5");
index = bs.binarySearch(intArray5, obj);
System.out.println(" Index where number: " + obj + " is found or should be inserted = " + index);
Integer[] intArray9 = { 1, 3, 4, 5, 6 };
obj = Integer.parseInt("4");
index = bs.binarySearch(intArray9, obj);
System.out.println(" Index where number: " + obj + " is found or should be inserted = " + index);
Integer[] intArray7 = { 1, 3, 5, 6, 10 };
obj = Integer.parseInt("7");
index = bs.binarySearch(intArray7, obj);
System.out.println(" Index where number: " + obj + " is found or should be inserted = " + index);
Integer[] intArray8 = { 1, 3, 5, 6 };
obj = Integer.parseInt("5");
index = bs.binarySearch(intArray8, obj);
System.out.println(" Index where number: " + obj + " is found or should be inserted = " + index);
Integer[] intArray10 = {};
obj = Integer.parseInt("5");
index = bs.binarySearch(intArray10, obj);
System.out.println(" Index where number: " + obj + " is found or should be inserted = " + index);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
Upon compiling the code listed above, it runs, but with errors. These are the error messages I receive:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at BinarySearch.binarySearch(BinarySearch.java:39) at BinarySearch2.main(BinarySearch2.java:167)
How can I fix the code to get rid of these error messages, while testing all the necessary conditions?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started