Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Learn Java from Ben Franklin

Write better Java code using Franklin's learning techniques

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
In his autobiography (see Resources for a link), Ben Franklin describes how he worked to improve his writing. Franklin found prose he judged to be excellent, wrote a short encapsulation of each sentence's main idea (he called them hints; here I will use the term summaries), put them aside for several days, and then tried to recreate the original using only his hints. He then compared his work with the original, making corrections where he fell short, and sometimes finding that he seemed to have improved on the original. People learning Java can do the same: find a model Java program, briefly summarize each line or section, put the notes away for a while, and then attempt to recreate the original from the notes. Once the program is finished, compare it to the original and make corrections where necessary; perhaps you'll discover you have improved the original program. In this article, I'll provide several exercises for applying Franklin's learning techniques to Java.

Exercise 1: Using summaries

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.

Exercise 2: Randomly ordering summaries

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:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • Also by Robert Nielsen: