CMPU 102 - Lab 4

In this lab you will implement a program to determine when a phrase is a palindrome.

A phrase is a palindrome when the sequence of the letters in the phrase is the same both forwards an backwards, ignoring space and punctuation.  Some examples:

Rise to vote, sir
Able was I ere I saw Elba
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal - Panama!

Your task is to write a program to determine whether or not a phrase is a palindrome.  A starting point is provided in the CheckPalindrome class, whose main method reads lines of input from  You should modify the loop so that after each line of input is read, it checks the line to see if it is a palindrome, and then prints either "Palindrome" or "Not a palindrome" to System.out.

A stack data structure is useful for balancing earlier events with later events.  So, you should be able to use a stack to determine if a phrase is a palindrome.  You may use an instance of the java.util.Stack class in your program.  Since you will be balancing earlier characters with later characters, a stack of characters is appropriate:

Stack<Character> stack = new Stack<Character>();

Boxing and Unboxing

Note that because char is a primitive type, we cannot directly store char values in a collection (like a Stack).  Instead, we must store references to Character objects, where each object stores a single char value.

Fortunately, Java will automatically convert a char value to a Character object when you push a value on the stack:

char c = ...

Java will also convert a Character reference back to a char value when you pop a value off of the stack:

char poppedValue = stack.pop();

These automatic conversions are known as boxing and unboxing.  You can think of the Character class as being a "box" that holds a single primitive char value.  In most situations, Java will automatically handle boxing and unboxing for you, so that you can usually provide a primitive value (e.g., char) in a situation where a boxed value (e.g., Character) is required, and vice versa.  However, you should be aware of what is happening behind the scenes.

Requirements and hints

You will need to treat a String object as a sequence of characters.  You can use the length and charAt methods to get the length of a string and the value of a single character of the string, respectively.  For example, here is how to iterate over all of the characters in a String:

String line = ...
for (int i = 0; i < line.length(); i++) {
    char c = line.charAt(i);

    // Do something with c

Your program should ignore all characters other than letters.  You can find out whether or not a character is a letter using the Character.isLetter method:

char c = ...

if (Character.isLetter(c)) {
    // c is a letter

You should also ignore case.  One way to do this is by converting all letters to lower-case:

c = Character.toLowerCase(c);

Getting started

Start a web browser using the small Firefox button on the task bar at the bottom of the screen, or by typing

firefox &
in a terminal window and hitting return.  Go to the class web page, scroll down to the "Labs" section, and download the file into your home directory.

Start Eclipse by typing

eclipse &
in a terminal window and hitting return.  Once it starts up, choose "File->Import->Existing Projects into Workspace" from the menu, click the "Next" button, click "Select archive file", select the file "" in your home directory, and click the "Finish" button.  You will see a new project called "lab4" in the Package Explorer.

Running the program

First, go to the menu and choose "Window->Show View...->Console".

Next, in the Package Explorer, right click on "", and choose "Run As...->Java Application".  You may type phrases in the Console window.  Each time you hit return, the program will read a line and execute your palindrome-detection algorithm.

Submitting your work

Before you submit, show an instructor or lab coach that your program can distinguish palindromes from non-palindromes.

Submit your completed project by entering the following commands in a terminal window (pressing return after each command):

cd eclipse-workspace
submit102 lab4