< Day Day Up > |
Workshop: Presenting CreditsIn 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 Program1: 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:
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 ProgramThe Piano (1993) A Jane Campion film. Ada Holly Hunter Baines Harvey Keitel Stewart Sam Neill Flora Anna Paquin |
< Day Day Up > |