Write a recursive method that uses your compareTo(String, String) method to find
ID: 3887722 • Letter: W
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, given the array and the number of strings in the array.
public static String findMinimum(ArrayList<String> stringArray)
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?
Explanation / Answer
Hi, the idea is to compare the first element to the others in list recursively like this,
I wrote a driver program to test it too,
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
ArrayList<String> list=new ArrayList<String>();//Creating arraylist
list.add("Ravi");//Adding object in arraylist
list.add("Vijay");
list.add("Ravi");
list.add("Ajay");
System.out.println(findMinimum(list));
}
public static String findMinimum(ArrayList<String> stringArray) {
if (stringArray.size() == 1)
return stringArray.get(0);
else if (compareTo(stringArray.get(0), stringArray.get(1),stringArray.get(0).length())==-1)//means 1st index string is min
{
stringArray.remove(1);
return findMinimum(stringArray);
}
else//passing the rest of the array
{
stringArray.remove(0);
return findMinimum(stringArray);
}
}
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
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.