Question
Write a recursive method that uses your compareTo(String, String) method to find the minimum (i.e. first by alphabetical order) String in an array of Strings,
Write a recursive method that uses your compareTo(String, String) 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.
public static String findMinimum(ArrayList
Hint: Use the above method signature to call a private, helper method that is recursive - you can add more parameters to this helper method!
** This is the recursive compareTo(String, String) method mentioned above
/** * 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 * @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) { 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;
} return compareTo(s1,s2,index+1);
} //end recursive method compareTo
Note: Can you include comments for some of the line of code so that I can understand what is going on with the method written?
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