Workshop: Teaching Your Computer a Lesson

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 a 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 1,000,000 sentences or one minute, whichever comes first, it can stop running and think about the error of its ways.

By the way

A topic of heated debate here at Sams 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 serious problems. 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 Repeat.java. Enter the text of Listing 8.2 and save the file when you're done.

Listing 8.2. The Full Source Code of Repeat.java
 1: import java.util.*;
 2: 3: class Repeat {
 4: public static void main(String[] arguments) {
 5: String sentence = "Thorium 230 is not a toy.";
 6: int count = 10;
 7: Calendar start = Calendar.getInstance();
 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 < 1000000) {
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: }
21: count++;
22: }
23: System.out.println("\nI wrote the sentence " + count
24: + " times.");
25: System.out.println("I have learned my lesson.");
26: }
27: }


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 one of these classes, Calendar, in order to keep track of time while the program is running.
  • Lines 3 and 4: 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 Calendar class, which keeps track of time-related information, the start variable is created with the current time as its value.
  • Lines 8 and 9: The get() method of the Calendar class is used to retrieve the current minute and second and store them in the variables startMinute and startSecond.
  • Line 10: The Calendar 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 1,000,000, the loop will end.
  • Line 14: The punishment text, stored in the string variable sentence, is displayed.
  • Line 15: Using the Calendar class, the now variable is created with the current time.
  • Lines 16–20: Using one if statement inside of another, the program tests to see if 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 22: The } marks the end of the while loop.
  • Lines 23–25: The computer displays the number of times it repeated the punishment sentence and claims to be rehabilitated.
  • Lines 26 and 27: The main() block of the program and the program are closed out with } marks.

Compile the program with the javac compiler tool or another compiler and then give it a try. Using the JDK, you can run it 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 around 530,000 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 tutorials so I can upgrade.

      
Comments