Lab 7: 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 lab7
cd lab7

Copy the file PalindromeTest.java from the public directory for this class:

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

2. Reversing a string iteratively

Reversing a string is a simple matter of looping through it and using charAt and concatenation to construct a new string backwards. Fill-in (and document) the body of reverseIterative using this algorithm. Then test it using the main method.

3. Reversing a string recursively

This can also be done recursively. Think of a way to formulate the problem (that is, define what the reverse of a string is) recursively. Then implement it in a method, reverseRecursive.

2. Computing palindromes iteratively

A couple assignments ago you each wrote a method to determine whether or not a String is a palindrome. We will be doing that again, but with two different algorithms. Fill-in the body of palindromeIterative, using this algorithm:

Compile and test.

3. Computing palindromes recursively

Now do the same for palindromeRecursive, except using a recursive algorithm. Your method palindromeRecursive must call itself. 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.")

4. Turn in

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

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

Thomas VanDrunen
Last modified: Mon Oct 9 16:28:08 CDT 2006