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 music. You might have felt lost yourself with some of the topics introduced during this hour.
Next, to reinforce the string-handling features that have been covered, you write a Java program to display credits for a feature film. You can probably guess the movie.
Return to the Java24
project in NetBeans and create a new Java class called Credits
. Enter the text of Listing 6.1 into the source editor and save the file when you’re done.
1: class Credits {
2: public static void main(String[] args) {
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 + ")
" +
17: "A " + director + " film.
" +
18: role1 + " " + actor1 + "
" +
19: role2 + " " + actor2 + "
" +
20: role3 + " " + actor3 + "
" +
21: role4 + " " + actor4);
22: }
23: }
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.
• Lines 4–14 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.
• Lines 16–21 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 character (
) causes the text after it to be displayed at the beginning of a new line. The tab character (
) 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 do encounter error messages, correct any typos you find in your version of the Credits
program and save it again. NetBeans compiles the program automatically. When you run the program, you see an output window like the output pane in Figure 6.1.
If this hour’s trivia related to The Piano and the films of director Jane Campion has sparked your curiosity or if you just dig quiet women in braids, visit Magnus Hjelstuen’s unofficial The Piano website at www.cadenhead.org/piano
.
When your version of Credits
looks like Figure 6.1, give yourself some credit. Six hours into this book, you’re writing longer Java programs and dealing with more sophisticated issues. Strings are something you use every time you sit down to write a program.
At the beginning of The Piano, Holly Hunter’s character Ada lost her piano when her new husband refused to make his Maori laborers carry it home. Strings cannot be taken away from you. You’ll be using strings in many ways to communicate with users.
Q. How can I set the value of a string variable to be blank?
A. Use an empty string, a pair of double quotation marks without any text between them. The following code creates a new string variable called adaSays
and sets it to nothing:
String adaSays = "";
Q. I can’t seem to get the toUpperCase()
method to change a string so that it’s all capital letters. What am I doing wrong?
A. When you call a String object’s toUpperCase() method, it doesn’t actually change the String object it is called on. Instead, it creates a new string that is set in all uppercase letters. Consider the following statements:
String firstName = "Nessie";
String changeName = firstName.toUpperCase();
System.out.println("First Name: " + firstName);
These statements display the text First Name: Nessie because firstName contains the original string. If you switched the last statement to display the changeName variable instead, it would output First Name: NESSIE.
Strings do not change in value in Java after they are created.
Q. Do all methods in Java display true
or false
in the same way that the equals()
method does in relation to strings?
A. Methods have different ways of producing a response after they are used. When a method sends back a value, as the equals()
method does, it’s called returning a value. The equals()
method is set to return a Boolean value. Other methods might return a string, an integer, another type of variable, or nothing at all—which is represented by void
.
Q. Why do schools assign grades the letters A, B, C, D and F but not E?
A. The letter grade E already was being used in an alternative grading system. Until the mid-20th century, in the United States the most popular grading system was to assign E for excellent, S for satisfactory, N for needs improvement or U for the dreaded unsatisfactory. So when the ABCD system came along, giving a failing student an E was considered a not-so-excellent idea.
ESNU grading remains in wide use in elementary schools.
The following questions test your knowledge of the care and feeding of a string.
1. My friend concatenates. Should I report him to the authorities?
A. No. It’s only illegal during the winter months.
B. Yes, but not until I sell the story to TMZ.com first.
C. No. All he’s doing is pasting two strings together in a program.
2. Why is the word String
capitalized, whereas int
and others are not?
A. String
is a full word, but int
ain’t.
B. Like all objects that are a standard part of Java, String
has a capitalized name.
C. Poor quality control at Oracle.
3. Which of the following characters puts a single quote in a string?
A. <quote>
B. ’
C. ‘
1. C. Concatenation is just another word for pasting, joining, melding, or otherwise connecting two strings together. It uses the +
and +=
operators.
2. B. The types of objects available in Java are all capitalized, which is the main reason variable names have a lowercase first letter. It makes it harder to mistake them for objects.
3. B. The single backslash is what begins one of the special characters that can be inserted into strings.
You can review the topics of this hour more fully with the following activities:
• Write a short Java program called Favorite
that puts the code from this hour’s “Comparing Two Strings” section into the main()
block statement. Test it out to make sure it works as described and says that Ada’s favorite instrument is not the ukulele. When you’re done, change the initial value of the guess
variable from ukulele
to piano
. See what happens.
• Modify the Credits
program so the names of the director and all performers are displayed entirely in uppercase letters.
To see Java programs that implement these activities, visit the book’s website at www.java24hours.com.
3.22.249.48