YCP Logo Lab 15: Palindromes

Due: Wednesday, Nov 5th by 11:59 PM

Getting started

Import CS201_Lab15.zip (File->Import...->General->Existing Projects into Workspace->Archive File). You should see a project called CS201_Lab15 in the Package Explorer.

Your Task

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!
A Danish custard? Drat such sin, Ada!

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 System.in. 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.

Here is an example run (user input in bold):

A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal - Panama!
Palindrome
Rise to vote, sir
Palindrome
Boy howdy!
Not a palindrome

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 should 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 = ...
stack.push(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);

Running the program

Run the CheckPalindrome class as a Java Application (right-click, Run As->Java Application).

Type in some phrases and make sure that the program can successfully distinguish palindromes from non-palindromes.

Submit

Make sure that you have included your Excel file in the project!

When you are done, save the project (CS201_Lab15) to a zip file by right-clicking it and choosing

Export...->Archive File

Upload the saved zip file to the Marmoset server as lab15. The server URL is

https://camel.ycp.edu:8443/