We now declare class GradeBook
(Fig. B.3) with a displayMessage
method that displays the course name as part of the welcome message. (See the sample execution in Fig.B.4.) The new method requires a parameter that represents the course name to output.
1 // Fig. B.3: GradeBook.java
2 // Class declaration with one method that has a parameter.
3
4 public class GradeBook
5 {
6 // display a welcome message to the GradeBook user
7 public void displayMessage( String courseName )
8 {
9 System.out.printf( "Welcome to the grade book for
%s!
",
10 courseName );
11 } // end method displayMessage
12 } // end class GradeBook
Before discussing the new features of class GradeBook
, let’s see how the new class is used from the main
method of class GradeBookTest
(Fig. B.4). Line 12 creates a Scanner
named input
for reading the course name from the user. Line 15 creates the GradeBook
object myGradeBook
. Line 18 prompts the user to enter a course name. Line 19 reads the name from the user and assigns it to the nameOfCourse
variable, using Scanner
method nextLine to perform the input. The user types the course name and presses Enter to submit the course name to the program. Pressing Enter inserts a newline character at the end of the characters typed by the user. Method nextLine
reads characters typed by the user until it encounters the newline character, then returns a String
containing the characters up to, but not including, the newline. The newline character is discarded.
1 // Fig. B.4: GradeBookTest.java
2 // Create a GradeBook object and pass a String to
3 // its displayMessage method.
4 import java.util.Scanner; // program uses Scanner
5
6 public class GradeBookTest
7 {
8 // main method begins program execution
9 public static void main( String[] args )
10 {
11 // create Scanner to obtain input from command window
12 Scanner input = new Scanner( System.in );
13
14 // create a GradeBook object and assign it to myGradeBook
15 GradeBook myGradeBook = new GradeBook();
16
17 // prompt for and input course name
18 System.out.println( "Please enter the course name:" );
19 String nameOfCourse = input.nextLine(); // read a line of text
20 System.out.println(); // outputs a blank line
21
22 // call myGradeBook's displayMessage method
23 // and pass nameOfCourse as an argument
24 myGradeBook.displayMessage( nameOfCourse );
25 } // end main
26 } // end class GradeBookTest
Please enter the course name:
CS101 Introduction to Java Programming
Welcome to the grade book for
CS101 Introduction to Java Programming!
Class Scanner
also provides method next that reads individual words. When the user presses Enter after typing input, method next
reads characters until it encounters a whitespace character (such as a space, tab or newline), then returns a String
containing the characters up to, but not including, the white-space character (which is discarded). All information after the first white-space character is not lost—it can be read by other statements that call the Scanner
’s methods later in the program. Line 20 outputs a blank line.
Line 24 calls myGradeBooks
’s displayMessage
method. The variable nameOfCourse
in parentheses is the argument that’s passed to method displayMessage
so that the method can perform its task. The value of variable nameOfCourse
in main
becomes the value of method displayMessage
’s parameter courseName
in line 7 of Fig. B.3. When you execute this application, notice that method displayMessage
outputs the name you type as part of the welcome message (Fig. B.4).