Project 2: Testing for palindromes

The goal of this project is to practice iterating over data using loops. After a few weeks you already have the pieces for making very sophisticated programs. Here we will apply them to a non-trivial problem.

1. 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

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

2. Setup

Move into your cs235 directory, make a directory for this project, and change into it.

cd cs235
mkdir proj2
cd proj2

Then start a new file (something like Palindrome.java). Don't forget the opening documentation and comments for every variable declaration.

3. Version 1: interactive

Write a program that

4. Version 2: using command line arguments

Write a second version of the program (call it something like Palindrome2.java). This time, instead of prompting the user for palindromes, we'll use the strings on the command line and test them. So, you will need a loop that iterates through the elements of args. For each one, print it and display whether or not it is a palindrome. A sample run might look like

> java Palindrome2 wheaton level radar computer
wheaton false
level true
radar true
computer false

You can copy and paste certain chunks of code from your first version. Remember, a loop may contain any statement---including another loop.

5. Turn in

When you are all finished and are confident that your program fulfills all the instructions here and is working right, start the script program, giving it a file name like "proj2script". Display, compile, and run both versions of your program. Run them with both palindromes and non-palindromes to demonstrate that it both detects and rejects correctly. Then print the script.

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

Then turn in the hard copy in class or put it in my box.

DUE: Wed, Sept 20, at 5:00 PM.


Thomas VanDrunen
Last modified: Tue Sep 19 08:49:59 CDT 2006