Lab 6: Recursive methods

The goal of this lab is to practice writing and working with recursive methods.

1. Setup

As in previous assignments, move into your cs235 directory. Make a directory for this assignment, and change into it.

cd cs235
mkdir lab6
cd lab6

Copy the following file from the public directory for this class:

cp /homeemp/tvandrun/pub/235/RecursiveString.java .

This file contains the stubs for eight methods for you to write, as well as a simple main method to test these methods out using information from the args array. In each section below, you will be instructed to fill-in the body for one of these methods; you also should uncomment the code in the main method which tests it.

2. Computing palindromes recursively

You've already programmed at least one algorithm for determining whether or not a String is a palindrome. Now you need to figure out a recursive method to do it. Figure the algorithm on your own, but here's a recursive definition of a palindrome to get you started:

A palindrome is

(Remember, "prepend" means "concatenate to the front" and "append" means "concatenate to the back.")

Write the isPalindrome() method and test.

3. Reversing strings

Write a method which will reverse a String. First write out a recursive definition of what the reverse of String is; then transform that definition into a recursive algorithm for computing the reverse of a String.

4. Computing prefixes

Write a recursive version of the startsWith() method you wrote for the quiz.

5. Computing containment

Write a recursive method which will determine if one String is a substring of another.

6. Computing semordnilap

Write a recursive method that will determine if two Strings are semordnilap. You may not use your reverse() method.

7. Computing a String's length

Write a recursive method that will compute a String's length. You may use only substring() and equals().

8. Computing if two Strings are equal

Write a recursive method that will compute whether or not two Strings are equal to eachother in their contents. You may use only length(), charAt(), and substring().

9. Computing a substring

Write a recursive method that will compute a substring of another String, using only charAt() and concatenation. You may assume that the indices are valid.

10. Turn in

Create the script file as before (cat, rm, compile, and run). Run the program using a variety of words. Print it out and hand in a hard copy.

a2ps -P sp (the name of the script file)

Thomas VanDrunen
Last modified: Tue Feb 13 12:12:10 CST 2007