• Tidak ada hasil yang ditemukan

Declaring a Method with a Parameter

Summary

3.3 Declaring a Method with a Parameter

class’soperations, which correspond to methods in Java. The UML models operations by listing the operation name preceded by an access modifier (in this case+) and followed by a set of parentheses. ClassGradeBookhas one method,displayMessage, so the bottom compartment of Fig. 3.3 lists one operation with this name. Method displayMessage

doesnotrequire additional information to perform its tasks, so the parentheses following the method name in the class diagram areempty, just as they were in the method’s decla- ration in line 7 of Fig. 3.1. The plus sign (+) in front of the operation name indicates that

displayMessageis a public operation in the UML (i.e., apublicmethod in Java). We’ll often use UML class diagrams to summarize a class’s attributes and operations.

3.3 Declaring a Method with a Parameter 77

Class Scanner also provides a similar method—next—that reads individual words.

When the user pressesEnterafter typing input, methodnextreads characters until it encoun- ters awhite-space character(such as a space, tab or newline), then returns aStringcontaining 1 // Fig. 3.4: 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( )

8 {

9 10

11 } // end method displayMessage 12 } // end class GradeBook

Fig. 3.4 | Class declaration with one method that has a parameter.

1 // Fig. 3.5: GradeBookTest.java

2 // Create 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

20 System.out.println(); // outputs a blank line 21

22 // call myGradeBook's displayMessage method 23 // and pass nameOfCourse as an argument 24

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!

Fig. 3.5 | Create aGradeBookobject and pass aStringto itsdisplayMessagemethod.

String courseName

System.out.printf( "Welcome to the grade book for\n%s!\n", courseName );

String nameOfCourse = input.nextLine(); // read a line of text

myGradeBook.displayMessage( nameOfCourse );

the characters up to, butnotincluding, the white-space character (which is discarded). All information after the first white-space character is not lost—it can be read by other state- ments that call theScanner’s methods later in the program. Line 20 outputs a blank line.

Line 24 callsmyGradeBooks’sdisplayMessagemethod. The variablenameOfCourse

in parentheses is theargumentthat’s passed to methoddisplayMessageso that the method can perform its task. The value of variablenameOfCourseinmainbecomes the value of methoddisplayMessage’sparametercourseNamein line 7 of Fig. 3.4. When you execute this application, notice that methoddisplayMessageoutputs the name you type as part of the welcome message (Fig. 3.5).

More on Arguments and Parameters

In Fig. 3.4,displayMessage’s parameter list (line 7) declares one parameter indicating that the method requires aStringto perform its task. When the method is called, the argument value in the call is assigned to the corresponding parameter (courseName) in the method header. Then, the method body uses the value of thecourseNameparameter. Lines 9–10 of Fig. 3.4 display parametercourseName’s value, using the%sformat specifier inprintf’s for- mat string. The parameter variable’s name (courseNamein Fig. 3.4, line 7) can be thesame or differentfrom the argument variable’s name (nameOfCoursein Fig. 3.5, line 24).

The number of arguments in a method callmustmatch the number of parameters in the parameter list of the method’s declaration. Also, the argument types in the method call must be “consistent with” the types of the corresponding parameters in the method’s dec- laration. (As you’ll learn in Chapter 6, an argument’s type and its corresponding param- eter’s type are not always required to beidentical.) In our example, the method call passes one argument of typeString(nameOfCourseis declared as aStringin line 19 of Fig. 3.5) and the method declaration specifies one parameter of type String (courseName is declared as aStringin line 7 of Fig. 3.4). So in this example the type of the argument in the method call exactly matches the type of the parameter in the method header.

Updated UML Class Diagram for ClassGradeBook

The UML class diagram of Fig. 3.6 models classGradeBookof Fig. 3.4. Like Fig. 3.1, this

GradeBookclass containspublicoperationdisplayMessage. However, this version ofdis-

playMessagehas a parameter. The UML models a parameter a bit differently from Java by listing the parameter name, followed by a colon and the parameter type in the parentheses following the operation name. The UML has its own data types similar to those of Java (but, as you’ll see, not all the UML data types have the same names as the corresponding Java types). The UML typeStringdoes correspond to the Java typeString.GradeBook methoddisplayMessage(Fig. 3.4) has aStringparameter namedcourseName, so Fig. 3.6 listscourseName : Stringbetween the parentheses followingdisplayMessage.

Fig. 3.6 | UML class diagram indicating that classGradeBookhas adisplayMessage

operation with acourseNameparameter of UML typeString. GradeBook

+ displayMessage( courseName : String )