Consider the following demonstration program, which creates a file and writes three lines of text to that file:
import java.io.*;
public class WriteTest2 {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("test.txt");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
pw.println("This is a test");
pw.println("This is only a test");
pw.println("In the event of a real emergency it would be too late");
pw.close();
bw.close();
fw.close();
}
catch (Exception e) {
System.out.println(e.toString());
}
}
}
One way to summarize the text is:
main()
try
FileWriter object
BufferedWriter object
PrintWriter object
Write one lame joke to file
close FileWriter object
close BufferedWriter object
close PrintWriter object
catch
Now you put the notes and original program away for a few days, after which you try to write a program based on the list of summaries. Then you compare your version of the program to the original version. If you see a deficiency in your version, make a note. You may even have the satisfaction of discovering that your version is superior.
While the above code, as well as other example code in this article, is relatively simple, you can use the exercises presented here with much more difficult programs. I chose relatively easy-to-understand programs so that audiences of mixed Java skills could easily grasp them. Rest assured, however, that those with more advanced skills can do these same exercises using more complex Java programs.
Franklin also created an even more challenging exercise: instead of writing the summaries in order, he put them in random order. As with the previous exercise, he would put the notes away for a while, then try to recreate the original from the unordered summaries. Similarly, you can randomize the summary order of Java programs that you will later recreate. For example, you can try to recreate the above program based on the following random list of summaries: