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

1.Write a recursive method to compare two Strings using alphabetical order as th

ID: 3887141 • Letter: 1

Question

1.Write a recursive method to compare two Strings using alphabetical order as the natural order (case insensitive).

The function should return an integer with the following convention:

s1 comes before s2 returns an integer less than 0

s1 == (or indistinguisable from) s2 returns 0

s1 comes after s2 returns an integer greater than 0

2.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.

Hint: Use the above method signature to call a private, helper method that is recursive -

you can add more parameters to this helper method!

3. Write a JUnit test class that fully tests each of these two methods.

Explanation / Answer

// Provided ths solution for 1

public class StringCompare {

static int recursiveCompare(String string1, String string2, 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(string1.charAt(index)< string2.charAt(index)){

return -1;

}

else if(string1.charAt(index)> string2.charAt(index)){

return 1;

}

return recursiveCompare(string1,string2,len1, len2,index+1);

}

public static void main(String args[]){

String str1 = "nitiomg";

String str2 = "nitiom";

int len1 = str1.length();

int len2 = str2.length();

System.out.println(recursiveCompare(str1,str2,len1, len2,0));

}

}

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