Small Java Programs

This chapter discusses naming and coding conventions as well as reserved words in Java. When you go through this chapter, you'll get some hands-on experience with writing in Java.

14. Finished Program


The completed program is given below.

Did you remember to put in the quote marks?

Finished Program

Here is the completed program. Be sure that you put the quote marks where they belong. If you forget just one of them, the compiler will become confused and will not translate your program!

public class Haiku
  public static void main ( String[] args )
    System.out.println("On a withered branch"); System.out.println("
        A crow has just alighted:"); System.out.println("
        Nightfall in autumn."); } }         

If you have been creating the example programs and compiling and running them, you may have noticed that spaces and new lines in the program are not critical. (However, you can't put spaces in the middle of a word, and spaces inside the quote marks do matter.) For example, the following version of the program will compile correctly and will do exactly the same thing as the original version when it is run:

public                    class     
  public   static void main(String[] args )
  System.out.   println(          "On a withered branch");
    System  .  out.println("A crow has just alighted:"         );
System.out.println("Nightfall in autumn.");

The compiler does not "see" the two dimensional layout of the program. It regards the program as a stream of characters, one following the other.

However, humans are sensitive to the layout of text, and it is important to be neat and consistent when you create a source file. Although the second version of the program runs correctly, it is much harder for a person to understand.

Question 14:

If there were a slight mistake in the poorly laid-out program, would it be easy to find?