Question: Write a JUnit test class that fully tests the indexOf method below (recursiveBinarySearcher is a helper method to find the indexOf a particular element and

Write a JUnit test class that fully tests the indexOf method below (recursiveBinarySearcher is a helper method to find the "indexOf" a particular element and -1 should be returned if an element is not found).

Here is the code:

import java.util.List; import java.lang.Comparable; import java.util.ArrayList;

public class Sorters2120 {

/** * A method to find the "indexOf" a particular element. * @param searchItem to compare * @param theList to compare * @return position of searched element */ public static > int indexOf(T searchItem, List theList) { return recursiveBinarySearcher(searchItem, theList, 0, theList.size()); } // End method indexOf

/** * A recursive helper method to find the position of * a specified value within a sorted array. * @param searchItem to compare * @param theList to compare * @param first element to compare * @param last element to compare * @return -1 if the element is not found */ private static > int recursiveBinarySearcher(T searchItem, List theList, int first, int last) { if(first >= last) return -1; int middle = first + (last - first)/2; if(theList.get(middle).compareTo(searchItem) == 0) return middle; if(theList.get(middle).compareTo(searchItem) < 0) return recursiveBinarySearcher(searchItem, theList, middle+1, theList.size()); else return recursiveBinarySearcher(searchItem, theList, 0, middle); } // End method recursiveBinarySearcher

} // End class Sorters2120

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!