next up previous
Next: Names Up: Assignment 7: Strings and Previous: Documentation

Palindromes

1.
Create a new project named Palindrome.prj based on cs151HelloStationery.

2.
Type in the method named first from last week, the one that takes a String and returns the first letter.

3.
Write a method named last that takes a String and returns the last letter. Test this method.

4.
Write a method named middle that takes a String and returns a substring that contains everything except the first and last characters.

Hint: use the substring method. Here is the documentation:

public String substring (int beginIndex, int endIndex)

Returns a new string that is a substring of this string. The substring
begins at the specified beginIndex and extends to the character at
index endIndex - 1.

Parameters: 
beginIndex - the beginning index, inclusive. 
endIndex - the ending index, exclusive. 

Returns: the specified substring.

Try to figure out what they mean by ``inclusive'' and ``exclusive.'' Run a couple of experiments to see if you are right.

5.
A palindrome is a word that reads the same both forward and backward, like ``otto'' and ``palindromeemordnilap.'' An alternative, recursive definition of a palindrome is ``either an empty string or a word whose first letter is the same as the last, and whose middle is a palindrome.''

Write a recursive method named isPalindrome that takes a String and that returns a boolean indicating whether the word is a palindrome or not.

6.
On a piece of paper, figure out a strategy for checking palindromes iteratively. Think about what variables to use and what role they will play. Show me your plan before you proceed.

7.
Once I have ok'ed your plan, write it up in Java.


next up previous
Next: Names Up: Assignment 7: Strings and Previous: Documentation
Allen B. Downey
1999-10-19