The goal of this lab is to practice writing and working with recursive methods.
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 .
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.
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
.
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:
reversed
.
reversed
is equal to text
(be careful!).
Compile and test.
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.")
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)