CSCI 235 Lab 2: More Strings

Today, you’ll continue with some more word games in order to get practice working with strings.

For ready access to the hints and the documentation, you probably want to launch a browser and access this online (via the class page) when you start working on it.

Setting up

To get started, make a directory where you want to work today (that’s probably something like lab2), then change into it. You can copy a file that serves as a starting point for today’s work from the class directory:

$ cp /cslab/class/csci235/labs/lab2/Palindrome.java .

Don’t miss the dot on the end of that command.

(Even if you had a working palindrome program last week, you can probably write a better one now; so I recommend that you start from scratch. But if you really want to grab what you wrote last week, you could copy it back from your own account. See the tips on copying files if you don’t remember how. But if you do that, you’ll still need to clean it up and make sure that it meets the requirements below.)

Open Palindrome.java in Emacs. So that you don’t forget, fill in your names correctly after the @author tag (replacing the dummy names that are there). Be sure to save it.

Take a look at the three lines after the one that begins “public static void main”. These should make some sense now.

I. Basic Palindromes

A word, phrase, or sentence is a palindrome if it is the same backwards and forwards. Here are a few words that are palindromes:

Note also that the empty string (no characters at all) would also be a palindrome.

Fill in the program to test whether the input, stored in str, is a palindrome.

Print a nice message that includes the original input in quotes and says whether it is a palindrome, as in

’toot’ is a palindrome.
’tort’ is not a palindrome.

Compile and test. Try a variety of interesting cases.

II. Better Palindromes

Now we are going to make a better program. Start by copying Palindrome.java to BetterPalindrome.java. Open BetterPalindrome.java in Emacs, update the leading comment, and change the identifier in the line

public class Palindrome {

from Palindrome to BetterPalindrome. Save it, make sure it compiles (use the new filename!) and works.

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: try to find a way to do it that does not make your loop for testing more complicated. Check out this online hint if you need help. You’ll find a list of string methods in the middle of section 1.3 of the textbook, and on the handout you received in class, which is also reachable from the class page. (You could also look at the official Java documentation for class String, but that would be overwhelming.)

Now it is time to make your program ignore any characters in the input that are neither letters nor digits. Once you’ve done this, you should get something like

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

The same hint applies here.

Note: there are methods in class Character to help you classify characters. Look for methods with names that start with ‘is’ and take a parameter that is of type char.

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);

Make sure your modified program works, and enjoy a moment of celebration with your partner.

III. Pangrams

Your previous program tests whether a string is a palindrome. Now you’ll tackle another word game.

A sentence is a pangram if it contains every letter of the alphabet at least once. Examples include

Write a program which prompts the user for a string and determines whether or not that string is a pangram. Ignore spaces, punctuation, and capitalization. Your program should also include an opening documentation block (following the style we’ve seen in class) and a comment on every variable you declare.

Note: a good way to start would be to copy your palindrome program. You can do that with

$ cp BetterPalindrome.java Pangram.java

Hint: Think about how you would do this using pencil and paper. You can do this using the methods listed in the textbook, which are also in the simplified online documentation. There is an online hint, if you need one. And another.

IV. Anagrams

Two words or phrases are anagrams of each other if they contain all the same letters (with the same frequency). Examples include

Write a program Anagrams.java that prompts the user for two strings. Test whether the two strings are anagrams of each other. Ignore spaces, punctuation, and capitalization. Your program should also include an opening documentation block (following the style we’ve seen in class) and a comment on every variable you declare.

Again, you might want to copy one of your other programs to get a starting point.

Turning it in

Make a script file that shows you compiling your programs and running them with inputs that make it clear that they work correctly. (See how to script.)

Print your four .java files and the script. (See how to print.)

Use handin to hand in the files as lab2, too. (See how to do that, too.)

And don’t forget to copy the files to your own account.