Assignment 6: Recursive methods

The goal of this assignment is to practice writing and working with recursive methods. There are two parts. The first part should take you about the length of the class period. The second part may require coming back later to finish. In total there will be two files (or programs) that you will work on.

1. Setup

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

> cd cs241
> mkdir assign6
> cd assign6

Like last time, I am giving you partially-written files to start with. Copy them from the public directory for this class:

> cp /homeemp/tvandrun/pub/241/PalindromeTest.java .
> cp /homeemp/tvandrun/pub/241/MergeSort.java .

PART I: Testing for Palindromes

2. Introduction

A palindrome is a word, number, phrase, or sentence that is the same backwards and forwards. If we ignore spaces, punctuation, and capitalization, some interesting palindromes are

The first part of this assignment is to write a program that tests if a string is a palindrome. We'll confine ourselves to single words (like "level", "radar", and "racecar"). Open the file PalindromeTest.java in Xemacs. Notice that the main method is already written for you. Read it and understand how it works. Notice that there are two methods, each of which should test a string and return "true" or "false". The difference is, one will be iterative and the other recursive.

3. Computing palindromes iteratively

Fill-in (and document) the body of palindromeIterative. There are several algorithms you could use. For example:

or

Compile and test.

4. 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.")

PART II: Merge sort revisited

5. Finishing merge sort.

Remember that on Friday we looked at a recursive algorithm for sorting the characters in a string in lexicographical order. Our algorithm relied on another algorithm called "merge", which we did not have time to work out in class. Open the file MergeSort.java in Xemacs. The file contains a driver and a method for the merge sort algorithm we saw in class. However, the method merge is left blank (except for a dummy line so that the program will compile).

Figure out what needs to be done for the merge operation to make merge sort work right. Remember, think of it as if you had two sorted piles of index cards in front of you on your desk. You need to merge them into a single sorted pile. How would you do it? Implement the same algorithm.

6. Hints

Remember the following string methods:

7. Extra credit

I am not asking for a recursive algorithm for merge. However, it is possible to do the merge recursively. You will get bonus points if your merge algorithm is recursive. (I still recommend you get the iterative version working first, and then think about how to do it recursively.)

8. Turn in

Create the script file as before (cat, rm, compile, and run). Run the palindrome test on two palindromes and two non-palindromes for each method. Run mergesort on six strings, a mixture of short ones and long ones, with an odd number of characters and and even number of characters, including one with only one character. Then print it.

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

(This will print "two up", meaning two pages shown next to each other on one pice of paper. If you use a2ps on a Java file, it will format it nicely like in the handouts I've given in class. The command lpr works similarly except it does no formatting and doesn't print two up by default.)

Then turn in the hard copy in class on Friday or put it in my box by 5:00 pm.

DUE: Friday, Feb 17, at 5:00 PM.


Thomas VanDrunen
Last modified: Mon Feb 14 14:28:09 CST 2005