Question: Hi, I'm doing an assignment where I alter an algorithm to meet certain conditions, which I believe I've done correctly. However, the second part is
Hi,
I'm doing an assignment where I alter an algorithm to meet certain conditions, which I believe I've done correctly. However, the second part is to test the implementation and meet certain conditions, which I'm really struggling with how to do and have never done before. This is the part of the problem I'm struggling with followed by what I have so far:
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.
What You Need to Turn In
1. the .java source code for your modified binarySearch method
2. the set of test cases you used to test your method (this is importantif you dont turn in your test cases you wont get full points ). For this part you need to turn in a table listing the following information: each of the 13 test categories
a.each of the 13 test categories
b. the array of items you used for each test
c. the item you tested for insertion for each test
d. the index you expected the algorithm to return
e. the index the algorithm actually returned
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) { low = mid + 1; } else if (objArray[mid].toString().toLowerCase().compareTo(searchObj.toString().toLowerCase()) > 0) { 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) { Integer[] intArray = {1,2,3,4,5,6}; int storeIndex; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the number to search for or insert: "); Integer obj; try{ obj = Integer.parseInt(reader.readLine()); binarySearch newBS = new binarySearch(); storeIndex = newBS.binarySearch(intArray, obj); System.out.println("Index where number "+obj+" is found or should be inserted is "+storeIndex); } catch (NumberFormatException e) { e.printStackTrace(); } catch (IOException e) {
e.printStackTrace(); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
