Project 5: String buffer

1. Introduction

Remember that Strings are immutable. In this project you will make a new class that acts like a string in many ways except that you can change its contents. This project should help you understand how a class works, what goes into a class, and how you can write a class in two different ways yet have it appear to work the same way. So, you will be essentially doing this project twice---don't be suprised when, at the end of the project description, you are told to go back to the beginning and do it all again with one difference. It will be easier the second time.

A StringBuffer is an object like a string except that you can modify it, like an array. You will be writing two StringBuffer classes. So, make a new directory for this project, and in that directory make a subdirectory called version1.

cd cs235
mkdir proj5
cd proj5
mkdir version1
cd version1

Now copy the driver for your StringBuffer class.

cp /homeemp/tvandrun/pub/235/StringBufferDriver.java .

You will not make any changes to this file except to un-comment parts as you go along. Now open a file StringBuffer.java.

2. Part I: Basic class set-up

Our first version of StringBuffer will have on instance variable, a String. This string will represent the current state of the StringBuffer, and you will update this variable whenever you need to modify the StringBuffer object. Write the basic class with this instance variable (don't forget to document it), and compile it. You should make all your instance variables private and all your instance methods (and constructors) public.

Next write a constructor which will receive a String, and initialize your instance variable to that string. You also want to be able to display this StringBuffer, so write a method toString() which will return the instance variable. So far, this class is almost trivial. Notice that it is really just a container for a String.

Uncomment the "Part I" code in the driver and test it.

3. Part II: Methods that change the state of the StringBuffer

Now write two methods prepend(), which will take a string and add it to the front of this StringBuffer, and append() which will take a String and add it to the back. These methods should be void---they do not produce any value, they simply modify the object on which they are called. Uncomment the revelant section in the driver and test it.

4. Part III: Methods that report information about the StringBuffer

Now write two methods, toCharArray() and length() that work similar to those methods in the class String. (This should be really easy to write.) Then test.

5. Part IV: Methods that produce a new instance of the class

A Caesar cipher is an encryption algorithm that shifts each letter down a set number of places in the alphabet. The shift amount serves as the key to the encryption scheme. For example, if we wanted to encrypt the string "hello" with shift amount 2, the result would be "jgnnq." Similarly we would shift each letter back by 2 to decrypt the word. Letters at the end of the alphabet would wrap around to the front. So, 'y' shifted ahead 5 would be 'd'.

Recall that the computers store characters in a format that could be interpreted as integers. That is, every integer has a numeric code associated with it. Thus you can cast chars to ints and vice versa. Once you have case a letter to an integer, you can do arithmetic on it. For example, if you wanted to shift "b" ahead by 5, you could write

(char) ('b' + 5)

The result would be 'g'. Use this information to write two methods, encrypt() and decrypt(), each of which will take a numeric key (a shift amount). The first will produce a new StringBuffer equivalent to the original but with each letter shifted ahead by the given amount; the second will do the same but shifting backwards. The letter's case should be preserved (lowercase letters are encrypted to lowercase letters), and non-letter characters should not be changed. For example,

CSci 235-- Programming I: Problem-Solving

With key 6 would be

IYio 235-- Vxumxgssotm O: Vxuhrks-Yurbotm

Notice that this is mo more tricky than just adding or subtracting the key to each letter.

6: Version II

Now make a directory for version 2.

cd .. mkdir version2 cd version2 cp /homeemp/tvandrun/pub/235/StringBufferDriver.java .

And redo this project but instead of using a String for the instance variable, use a character array. All the signatures of the methods should remain the same (for example, the constructor should still receive a String), and the driver will not need any modification.

7. Turn in

Create the script file as before (cat, rm, compile, and run). Run both versions. Make sure the string you encrypt contains capital and lowercase letters and some non-letter characters.

 > a2ps -P sp (the name of the script file)

(This will print "two up", meaning two pages shown next to each other on one pice of paper. If you use a2ps on a Java file, it will format it nicely like in the handouts I've given in class. The command lpr works similarly except it does no formatting and doesn't print two up by default.)

DUE: Tuesday, Oct 31, at 5:00 PM.


Thomas VanDrunen
Last modified: Fri Feb 16 14:50:28 CST 2007