Previous Section  < Day Day Up >  Next Section

Workshop: Presenting Credits

In The Piano, Ada McGrath Stewart was thrown into unfamiliar territory when she moved from Scotland to New Zealand to marry a stranger who didn't appreciate her ivory tickling. You might have felt similarly lost with some of the topics introduced during this hour.

As a workshop to reinforce the string-handling features that have been covered, you will write a Java program to display credits for a feature film. You have three guesses as to the movie chosen, and if you need a hint, it starts with a The and ends with a musical instrument that can be used to express the repressed passion of attractive mutes.

Load the word processor you're using to write Java programs and create a new file called Credits.java. Enter the text of Listing 6.1 into the word processor and save the file when you're done.

Listing 6.1. The Credits Program

 1: class Credits {

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

 3:         // set up film information

 4:         String title = "The Piano";

 5:         int year = 1993;

 6:         String director = "Jane Campion";

 7:         String role1 = "Ada";

 8:         String actor1 = "Holly Hunter";

 9:         String role2 = "Baines";

10:         String actor2 = "Harvey Keitel";

11:         String role3 = "Stewart";

12:         String actor3 = "Sam Neill";

13:         String role4 = "Flora";

14:         String actor4 = "Anna Paquin";

15:         // display information

16:         System.out.println(title + " (" + year + ")\n" +

17:             "A " + director + " film.\n\n" +

18:             role1 + "\t" + actor1 + "\n" +

19:             role2 + "\t" + actor2 + "\n" +

20:             role3 + "\t" + actor3 + "\n" +

21:             role4 + "\t" + actor4);

22:     }

23: }


Before you attempt to compile the program, look over the program and see whether you can figure out what it's doing at each stage. Here's a breakdown of what's taking place:

  • Line 1 gives the Java program the name Credits.

  • Line 2 begins the main() block statement in which all of the program's work gets done.

  • Line 3 is a comment statement explaining that you're going to set up the film's information in subsequent lines.

  • Lines 414 set up variables to hold information about the film, its director, and its stars. One of the variables, year, is an integer. The rest are string variables.

  • Line 15 is another comment line for the benefit of humans like us who are examining the program.

  • Lines 1621 are one long System.out.println() statement. Everything between the first parenthesis on Line 16 and the last parenthesis on Line 21 is displayed onscreen. The newline text (\n) causes the text after it to be displayed at the beginning of a new line. The tab text (\t) inserts tab spacing in the output. The rest are either text or string variables that should be shown.

  • Line 22 ends the main() block statement.

  • Line 23 ends the program.

If you're using the JDK, you can attempt to compile the program by going to the folder that contains Credits.java and typing this command:


javac Credits.java


If you do not see any error messages, the program has compiled successfully, and you can run it with the following command:


java Credits


If you do encounter error messages, correct any typos you find in your version of the Credits program and try again to compile it.

Listing 6.2 shows the output of the Credits application: a rundown of the film, year of release, director, and the four lead performers from The Piano. Be glad that you didn't have to present the credits for an ensemble film. A program detailing Robert Altman's Short Cuts, the 1993 film with more than 25 lead characters, could hog an hour of typing alone.

Listing 6.2. The Output of the Credits Program

The Piano (1993)

A Jane Campion film.



Ada     Holly Hunter

Baines  Harvey Keitel

Stewart Sam Neill

Flora   Anna Paquin


    Previous Section  < Day Day Up >  Next Section