CS 231 Intro to Programming, Spring 2003
Lab 7: Working with Strings and Files
In this lab you will gain experience working with files and strings. A String is a data structure containing a list of characters. Individual characters can be manipulated in much the same way as arrays. The String class also contains a number of useful functions (see the the java api online help for the String class.)
Level 1 - Reading and Writing to Files (Best Grade = C)
In Java, we can easily read ASCII text to and from files. In this part of the lab
you will set up your program to read text from a file and to display the text in a TextArea.
You will also be able to save the text in the TextArea to a text file. We provide you with two classes for doing this: MyWriter.java and MyReader.java. These files may also be found in the folder
L:\classes\cs231\fileIO
To read a file and store its contents into a String:
MyReader myReader = new MyReader();
String contents= "";
while (myReader.hasMoreData())
contents += myReader.giveMeTheNextLine() + "\n";
To write the String "contents" to a file
MyWriter myWriter = new MyWriter();
myWriter.print(contents);
myWriter.close();
Create an Applet and add a TextArea to display the contents of the file. Add a "load"
button to read the contents from a file. Add a "save" button to save the text in the
TextArea to a file. Test your code to make sure it works. Note that you can modify
the contents of the TextArea (or just type in stuff from scratch) and save the results
to a file.
Level 2 - Find/Replace (Best Grade = B)
A common feature of word processing systems is the find and replace command. Implement
a simple find and replace, where the user enters a single character c into
a textfield and another character d into a different textfield.
The program then replaces every occurrance of the character c with the character d.
You will need two TextFields (for the find and replace characters) and a Button that when pressed, does the find and replace.
Level 3 - Encryption (Best Grade = A)
A very simple way of encrypting text is to replace each character with the character
appears next in the alphabet. Thus an 'a' would be replaced with a 'b', a 'b' with
a 'c' and so on. Add an encrypt button that, when pressed, will use this encyption
method to encrypt your file. Also add a decrypt button that takes the decrypted text
and returns it to the original form. Thus, if you press the encrypt button followed
by the decrypt button, you will end up with the original text.
Level 4 - Encryption Variation (Extra credit)
Instead of replacing a letter with the next letter in the alphabet, one can create
a key which is some permutation of the alphabet. For example, consider the first 4 letters of the alphabet (abcd). The key could be the permutation cadb, which would mean that
a is replaced with c
b is replaced with a
c is replaced with d
d is replaced with b
Modify your program so that you can encrypt using a key. The key can be stored in an
array much like the deck of cards was stored in an array. A permutation can be
generated randomly in much the same way that you created a deck and shuffled it.
Level 4 - Find/Replace (Extra credit)
Instead of replacing a single character with another character, we can
replace an arbitrary word with another arbitrary word. That is,
the user enters two words s and t. Note that s and t may not be the same length.
The program looks through the text for every occurrance of s and replaces it with t.
Due Date
Submit your code in March 19. Demonstrate your code no later than your scheduled lab time during the week of March 31 (the week after break).
[top]
[CS 231 Home Page]