Lab 2: Manipulating strings

The goal of this lab is to practice the Java features for input and output, for arithmetic and type casting, and String manipulation.

I. Converting strings to numbers and back again

1. Set up

First open a terminal window.

I have prepared a stub of the program for you. Make a new directory for this lab, cd into it, and copy the file from the course directory.

mkdir lab2
cd lab2
cp /homes/tvandrun/Public/cs235/lab2/Convert.java .

2. Reading in and printing out a String

The program you are writing will eventually do the following:

This lab will walk you through the steps.

A. First, input a String from the user, using keyboard.nextLine() and display it back to the screen. Compile and run the program.

B. Next, suppose that the first character of the String is a digit, and you want to pick off the character and treat it as a int. The type of expression that results from charAt(0) is a char. What happens if you explicitly cast it to an int (or, store it in an int variable, forcing an automatic cast)?

C. What you should notice is that it will turn the character into an int, but not the one you want (for example, the digit 1 will turn into the int 49). This is because when Java converts from a char to an int it simply looks at the bit sequence and reinterprets them. "49" happens to be an integer interpretation of the bit sequence standing for the character "1".

Figure out how to convert a digit character to an appropriate int. There is more than one way to do this. Compile and test your program before you move on. Your program should input the String, generate the int, and print it to the screen.

3. Converting a mult-digit number

Now you want to convert more than just the first character. Assume that all the characters in the String are digits (and, when you test your program, input only digits). Modify your program so that instead of merely processing the first character, it loops through the String to produce the entire int. Store this result in an int variable and print the result on the screen. Compile and test.

4. Converting a real number

Now add a second part to your program (that is, keep the previous part intact, and add something coming after it). As before, you should input a String. This time assume that the String contains a representation of a real number---including a whole and decimal part, formatted something like xxxx.yyy, so you can assume that the String has only digits except for one . character. Write an algorithm for converting this String to an appropriate double. Hint: you will probably need two loops, one for handling the characters before the decimal point, the other for after. Store the result in a double variable and print it to the screen.

5. Printing currency representation

Finally, add a final part to your program. Multiply the int from part 4 and the double from part 5 together. The result will be a double, which you will display to the screen. However, you do not want simply to use System.out.println; you want to interpret this as an amount of money, so you should round this to the nearest hundredth and display exactly two decimal places. Write an algorithm for doing this.

II. Testing if a string is a palindrome

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

Your task is to write a program that lets the user input a string and tests if the string is a palindrome. As in Part I, I am providing a stub program.

cp /homes/tvandrun/Public/cs235/lab2/Palindrome.java .

2. Code it up

There are two parts to this. First, you will want to pre-process the string---since we want to ignore punctuation, spaces, and the case of letters, you want to create a string like the string the user gave except with the non-letter characters stripped out and the letters all of one case. Second, you want to do the actually testing of the new string to see if it is a palindrome.

Your program should print a message like "This string is a palindrome" or "This string is not a palindrome".

III. To turn in

Sometimes you will be asked to turn in an electronic version of your program by copying the .java files that you have written or modified to a grading directory. Today you will turn in a hard copy. Here are the procedures for generating something to turn in which demonstrates you have complete the lab.

  1. Start a script file. (Remember, script somefilename will start recording everything you do in the command shell to a a file called somefilename; just script will record it to a file called typescript.)
  2. Display the source file; in this case, type cat Convert.java.
  3. Compile (javac Convert.java)
  4. Run the program (java Convert)
  5. Repeat the last three steps for Palindrome.java
  6. Exit the script (exit)
  7. Print out the script file. Use the command a2ps instead of lpr-- this make the file print in "two-up" form, that is, two pages per page. (a2ps -P sp typescript-- or whatever the name of your script file is)

Make sure you have exited the script file before you print, or else it will print forever!

Then turn in the hard copy to me.


Thomas VanDrunen
Last modified: Mon Sep 8 16:15:30 CDT 2008