Lab 3: Java Strings

The goal of this lab is to exercise your understanding of Java Strings (and a few other types) and practice the recent Java constructs you have seen.

Today, you'll be making a couple of additions to a palindrome program. You'll start by reading a version of that program of a program that I am providing.

I. Reading a program

Open a terminal window. Then make a directory for this lab, change into it, and copy my version of the program:

$ mkdir lab3
$ cd lab3
$ cp /cslab/class/csci235/lab3/Palindrome.java .

You can look at that in XEmacs; a printed copy is also being handed out.

There are a few things you should notice in this program.

  1. Notice how every variable declared has a comment describing it; this is one of the requirements in the coding guidelines for your programs in this course.
  2. Lines 23-25 get input from the keyboard. We'll talk about the Scanner class later. Right now, pay attention to line 24, which prints instructions for the user; we call this a prompt. Note that it uses print() rather than println() and that there is a space on the end of the prompt string.
  3. This may be the first time you have seen a boolean variable: isAPalindrome is declared on line 32. Note how it is assigned and tested.
  4. There is a break statement on line 40. Executing a break is a way to leave the containing loop (while) immediately.

Feel free to run the program to notice how it works.

II. Better output

Right now, the program simply prints out "yes" or "no". Modify it so that it prints out a nice sentence saying what is or is not a palindrome. Make the output something like

'oops' is not a palindrome.
or
'pop' is a palindrome.
Note that the tested string is printed out in quotation marks.

When you have this working, stash a copy of it by copying it:

$ cp Palindrome.java Palindrome2.java
(This will give you something to back up to if you make a mess later on.)

III. Be more accepting

Right now, the program distinguishes based on case, so that, for example, "Pop" is not correctly recognized as a palindrome. Fix it so that it ignores case. Test it; make sure that it prints the input string as the user typed it.

This should be very simple. Pages 38-41 of the textbook list some of the methods available for strings. (You could look at the online documentation for class String, but that is a lot longer and contains a lot of clutter that we don't yet understand.)

Again, once you have it working, you probably want to stash a copy as Palindrome3.java.

IV. Palindromes, even better

Now it is time to make your program ignore any spaces and punctuation in the input. Once you've done this, you should get something like

'A man, a plan, a canal: Panama!' is a palindrome.

Note: there are methods in class Character to help you classify characters. Look in the documentation to find them. Look for methods with names that start with 'is' that take a parameter that is of type char. Ignore the parts about "Fields" and anything that refers to a "code point".

You'll notice that these methods have the word static in front of them, as does the digit() method that you used in the last lab. When a method is not static, you name it relative to an object, as when substring() is called in the second line of

String s = "1234";
String t = s.substring(2,3);
When you call a static method, though, you name it relative to the class, as in the use of digit() here:
int i = Character.digit('3', 10);

Turning it in

Make a script file that shows you compiling the final version of your program and running it with inputs that make it clear that it works correctly. (See how to script,)

Print your final .java file and your script. (See how to print,)

Use handin to hand in all of the files (three versions of the palindrom source and your script) as lab3, too. (See how to do that, too.) You probably want to copy your files to your own account before you leave.


Updated by Cary Gray,
based on work by Thomas VanDrunen
Last modified: Fri Jan 21 09:09:01 CST 2011