Friday, October 15, 2010

Java Coding In Detail


This hour's workshop provides evidence that you cannot punish your computer in the same way that Bart Simpson is punished at the beginning of each episode of The Simpsons. Pretend you're a teacher, and the computer is the kid who contaminated your morning cup of coffee with Thorium 230. Even if you're the most strident liberal, you realize that the computer must be taught a lesson--it's not acceptable behavior to give the teacher radiation poisoning. Your computer must be punished, and the punishment is to display the same sentence over and over again.

The Repeat program will use a loop statement to handle a System.out.println() statement again and again. Once the computer has been dealt this punishment for 25,000 sentences or one minute, whichever comes first, it can stop running and think about the error of its ways.

A topic of heated debate here at concerns whether the punishment is severe enough. Thorium is a silver-white metal that has a half-life of 80,000 years. Some scientists believe that it is as toxic as plutonium, and if it finds a home in someone's liver, bone marrow, or lymphatic tissue, Thorium 230 can cause cancer, leukemia, or lung cancer. A student who irradiates a teacher probably should receive three hours of in-school detention, at least.

Use your word processor to create a new file called Enter the text of Listing 8.1 and save the file when you're done.

Listing 8.1. The full source code of


 1: import java.util.*;


 3: class Repeat {

 4:    public static void main(String arguments[]) {

 5:        String sentence = "Thorium 230 is not a toy.";

 6:        int count = 0;

 7:        GregorianCalendar start = new GregorianCalendar();

 8:        int startMinute = start.get(Calendar.MINUTE);

 9:        int startSecond = start.get(Calendar.SECOND);

10:        start.roll(Calendar.MINUTE, true);

11:        int nextMinute = start.get(Calendar.MINUTE);

12:        int nextSecond = start.get(Calendar.SECOND);

13:        while (count++ <= 25000) {

14:            System.out.println(sentence);

15:            GregorianCalendar now = new GregorianCalendar();

16:            if (now.get(Calendar.MINUTE) >= nextMinute)

17:                if (now.get(Calendar.SECOND) >= nextSecond)

18:                    break;

19:        }

20:        System.out.println("\nI wrote the sentence " + count + " times.");

21:        System.out.println("I have learned my lesson.");

22:    }

23: }

The following things are taking place in this program:

·         Line 1: The import statement makes the java.util group of classes available to this program. You're going to use two of them, Calendar and GregorianCalendar, in order to keep track of time while the program is running.

·         Lines 2 and 3: The Repeat class is declared, and the main() block of the program begins.

·         Lines 5 and 6: The sentence variable is set up with the text of the punishment sentence, and the count variable is created with a value of 0.

·         Line 7: Using the GregorianCalendar class, which is used to retrieve the time information, the start variable is created with the current time.

·         Lines 8 and 9: The get() method of the GregorianCalendar class is used to retrieve the current minute and second and store them in the variables startMinute and startSecond.

·         Line 10: The GregorianCalendar roll() method is used to roll the value of the start variable one minute forward in time.

·         Lines 11 and 12: The get() method is used again to retrieve the minute and second for start and store them in the variables nextMinute and nextSecond.

·         Line 13: The while statement begins a loop using the count variable as the counter. When count hits 25,000, the loop will end.

·         Line 14: The punishment text, stored in the string variable sentence, is displayed.

·         Line 15: Using the GregorianCalendar class, the now variable is created with the current time.

·         Lines 16-18: Using one if statement inside of another, the program tests to see whether one minute has passed by comparing the current minute and second to the values of nextMinute and nextSecond. If it has passed, break ends the while loop.

·         Line 19: The } marks the end of the while loop.

·         Lines 20 and 21: The computer displays the number of times it repeated the punishment sentence and claims to be rehabilitated.

·         Lines 22 and 23: The main() block of the program and the program are closed out with } marks.

Compile the program with the javac compiler tool and then give it a try by typing the following at the command line:

java Repeat

Run this program several times to see how many sentences are displayed in a minute's time. The Repeat program is an excellent way to see whether your computer is faster than mine. During the testing of this workshop program, Repeat usually displayed from 8,700 to 9,100 sentences in a minute's time. If your computer displays the sentence more times than mine does, don't just send me your condolences. Buy more of my books so I can upgrade.


Caution: Although most of the programs that you will write in this book will work under version 1.0.2 of the Java Developer's Kit, the Repeat program will not compile successfully unless you use version 1.1 of the Kit. This program uses the GregorianCalendar class, which, like many other new features, was introduced with 1.1 and does not exist in 1.0.2. JavaSoft encourages the use of new features like this because they make improvements on the language. For example, GregorianCalendar is part of JavaSoft's effort to enable programmers to use different calendar systems in Java programs.



The information presented in this chapter is information that you will be coming back to again and again and again when you write programs. Loops are a fundamental part of most programming languages. In several of the hours to come, you'll get a chance to manipulate graphics so that you can produce animated effects. You couldn't do this without loops.

No comments:

Post a Comment