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 /homes/tvandrun/Public/cs235/lab6/RecursiveString.java .

This file contains the stubs for six 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 will do the following:

In these problems, you will never need to declare a local variable. You will also never write a loop. Some problems have specific restrictions; pay careful attention to those.

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. In your method, you may use charAt(), length(), and substring()

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 a String's length

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

7. Computing if two Strings are equal

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

8. Turn in

Create the script file as before. Run the program using a variety of words. Print it out and hand in a hard copy.

a2ps (the name of the script file)

9. Extra credit

One advantage to your being able to write methods is that now you can write pieces of code that can be plugged into larger programs. In this exercise you will write a method that defines some behavior within a program.

Copy the following files.

cp /homes/tvandrun/Public/cs235/lab6_ec/* .

You'll notice that this includes many class files, things that are already compiled for you. If you run the program KnightGame, it will open a window containing a chess-like board of a given size. For example,

java KnightGame 8 &

Will produce a window containing a normal 8x8 chessboard.

The problem given to you is, given a position on a chessboard and a number of moves, what positions can a knight reach from the given position within the given number of moves.

Open the file KnightSearch.java. It contains the stub of method performing this search, which you must complete. The program is set up in such a way that when the user clicks the "go" button, your method will be invoked, and the results will appear on the method.

Your method will be given a value of type ChessBoard. This type has the following operations (assume a variable of this type called board, just like the parameter to search():

The obvious strategy for solving this is

The details are a little difficult. See what you can do.


Thomas VanDrunen
Last modified: Thu Oct 1 15:02:34 CDT 2009