Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a recursive method that uses your compareTo(String, String) method to find

ID: 3887245 • 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
public static int compareTo(String s1, String s2, int len1, int len2, int index) {

       if(len1 == index && len2 == index) {
           return 0;
       }
       if(len1 == index && len2 > index) {
           return -1;
       }

       if(len1 > index && len2 == 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,len1,len2,index+1);

   } //end recursive method compareTo

Explanation / Answer

Hi, Please find below the required code. Let me know if you have any doubts.

I have tested it with a set of sample values and displayed the output below the code. You may test with other values as well.

MinFinder.java:

import java.util.ArrayList;

public class MinFinder {

public static void main(String[] args) {

//Some values just for testing the code

ArrayList<String> stringArray=new ArrayList<String>();

stringArray.add("def");

stringArray.add("sdg");

stringArray.add("bac");

stringArray.add("xyz");

stringArray.add("pop");

System.out.println("Min string in array is "+ findMinimum(stringArray));

}

public static int compareTo(String s1, String s2, int len1, int len2, int index) {

if(len1 == index && len2 == index) {

return 0;

}

if(len1 == index && len2 > index) {

return -1;

}

if(len1 > index && len2 == 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,len1,len2,index+1);

}

public static String findMinimum(ArrayList<String> stringArray)

{

return helper(stringArray);

}

  

//recursive helper method

private static String helper(ArrayList<String> array){

if(array.size()==1){

return array.get(0);

}

else{

int value=compareTo(array.get(0),array.get(1),array.get(0).length(),array.get(1).length(),0);

System.out.println(value);

if(value<0){

array.remove(array.get(1));

}

else if(value==1){

array.remove(0);

}

else{

array.remove(array.get(0));

}

System.out.println(array.toString());

//method's recursive call to itself

return helper(array);

}

}

}

Output with sample values:

Min string in array is bac

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote