Lab 4: Recursion
Objectives. The objectives of this lab are to give you practice writing and debugging
recursive methods.
Part I: The StringProcessor Class
Design, implement, and test a class named StringProcessor, which meets the
following criteria:
- The StringProcessor class contains several static utility
methods, all of which are implemented recursively.
- The reverse(String str):String method reverses the string str
and returns the result. So StringProcessor.reverse("java") returns "avaj".
- The isPalindrome(String str):boolean method returns true if its
parameter is a palindrome---i.e., a string that reads the same way backwards
and forwards. So StringProcessor.isPalindrome("madamimadam") returns true.
NOTE: Don't use the reverse() method for this problem.
- The countVowels(String str): int method counts the number of
vowels in its parameter. So StringProcessor.countVowels("madamimadam") returns 5.
This method should work on both upper and lower case letters.
- Challenge: The binarytoInt(String str): int converts its string parameter,
which represents a binary integer such as "10011" into its decimal value (19). So
StringProcessor.binaryToInt("110011") returns 51.
- Challenge: The toInt(String str): int converts its string parameter to an
integer. So StringProcessor.toInt("13094") returns 13094.
Suggestions.
- In designing your algorithms, try to visualize the "head/tail" pattern that we talked about in class.
- In designing your algorithms, try to view the problem as similar to previous examples we
have looked at.
- An important part of designing a recursive algorithm, is designing how parameters will be
used. At least one (and possibly more than one) parameter must serve as the "recusion" parameter,
meaning it (they) will change in the recursive calls to keep the process progressing toward
the base case.
- Design your algorithms in pseudocode before you start
writing code. Identify the base and recursive cases. Show your
algorithm for each method to the instructor.
- Use stepwise refinement as you develop and test your
algorithm.
- Note that in some cases the static public method may itself
not be recursive, but may rather call a recursive static private method
that solves the problem recursively.
- Make sure you test your methods well---that is, test a wide range of arguments to the methods, not just the ones used in the examples.
You're done. Great work!