Question: Write a JUnit test class that fully tests each of these two methods below ( compareTo & findMinimum ). Here is the code: import java.util.ArrayList;

Write a JUnit test class that fully tests each of these two methods below (compareTo & findMinimum).

Here is the code:

import java.util.ArrayList;

public class Recursion {

/** * Recursive method to compare two Strings using alphabetical * order as the natural order (case insensitive). * @param String s1 to compare * @param String s2 to compare * @param int index to compare * @return an integer with the following convention: * if s1 comes before s2 then it returns -1 * if s1 == (or indistinguisable from) s2 then it returns 0 * if s1 comes after s2 then it returns 1 */ public static int compareTo(String s1, String s2, int index) {

//base case if(s1.length() == index && s2.length() == index) { return 0; } if(s1.length() == index && s2.length() > index) { return -1; }

if(s1.length() > index && s2.length() == index) { return 1; }

if(s1.charAt(index)< s2.charAt(index)) { return -1; }

else if(s1.charAt(index)> s2.charAt(index)) { return 1;

}

//recursion return compareTo(s1,s2,index+1);

} //end recursive method compareTo

/** * @param ArrayList of Strings called stringArray * @return Result of helper method which contains the minimum String in an array of Strings */ public static String findMinimum(ArrayList stringArray) {

return helper(stringArray);

} //end findMinimum method

/** * Recursive helper method that uses the compareTo(String, String, int) method * to find the minimum (i.e. first by alphabetical order) String in * an array of Strings, given the array and the number of strings in * the array. * @param ArrayList of Strings called stringArray * @return Minimum String in an array of Strings */ private static String helper(ArrayList stringArray) {

//test to see if there is only one element in ArrayList if(stringArray.size()==1) { return stringArray.get(0); }

else { //using compareTo() to compare the first two elements int value = compareTo(stringArray.get(0),stringArray.get(1),0);

//removing the "larger" element if(value < 0) { stringArray.remove(stringArray.get(1)); }

else if(value == 1) { stringArray.remove(0); }

else { stringArray.remove(stringArray.get(0)); }

//method's recursive call to itself return helper(stringArray); }

} //end recursive helper method

} //end class

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!