7.6. GradeBook Case Study: Declaring Methods with Multiple ParametersChapters 36 presented classes containing simple methods that had at most one parameter. Methods often need to receive more than one piece of information to perform their tasks. We now consider how to write methods with multiple parameters. Declaring Method MaximumThe application in Figs. 7.47.5 uses a user-declared method called Maximum to determine and return the largest of three Integer values entered by the user. When the application begins execution, class GradeBookTest's Main method (lines 411 of Fig. 7.5) creates one object of class GradeBook (line 6) and uses this object to call method InputGrades (line 9). This method is declared in lines 4357 of class GradeBook (Fig. 7.4). Lines 4853 prompt the user to enter three Integer values and read them from the user. Line 56 calls method Maximum (declared in lines 6076) to determine the largest of the three Integer arguments. When method Maximum returns the result to line 56, the program assigns Maximum's return value to instance variable maximumGrade. Then line 10 of Fig. 7.5 calls method DisplayGradeReport, which outputs the maximum value. Figure 7.4. User-declared method Maximum that has three Integer parameters.
Figure 7.5. Application to test class GradeBook's Maximum method.
Consider method Maximum (lines 6076). Lines 6061 indicate that the method returns an Integer value, the method's name is Maximum and the method requires three Integer parameters (x, y and z) to accomplish its task. When a method has more than one parameter, the parameters are specified as a comma-separated list. When Maximum is called in line 56, the parameter x is initialized with the value of the argument grade1, the parameter y is initialized with the value of the argument grade2 and the parameter z is initialized with the value of the argument grade3. There must be one argument in the method call for each parameter (sometimes called a formal parameter) in the method declaration. Also, the type of each argument must be consistent with the type of the corresponding parameter. To determine the maximum value, we begin with the assumption that parameter x contains the largest value, so line 63 declares local variable maximumValue and initializes it with the value of parameter x. Of course parameter y or z may contain the actual largest value, so we must compare each of these values with maximumValue. The If...Then statement in lines 6668 determines whether y is greater than maximumValue, and if so, line 67 assigns y to maximumValue. The If...Then statement in lines 7173 determines whether z is greater than maximumValue, and if so, line 72 assigns z to maximumValue. At this point the largest of the three values resides in maximumValue, so line 75 returns that value to line 56. When program control returns to the point in the program where Maximum was called, Maximum's parameters x, y and z are no longer accessible to the programwe will see why in Section 7.8. Note that methods can return at most one value, but the returned value could be a reference to an object that contains many values. Note that maximumGrade is an instance variable in class GradeBook. Variables should be declared as instance variables of a class only if they are required for use in more than one method of the class or if the program should save their values between calls to the class's methods. Implementing Method Maximum by Reusing Method Math.MaxRecall from Fig. 7.3 that class Math has a Max method that can determine the larger of two values. The entire body of our maximum method could also be implemented with two calls to Math.Max, as follows: Return Math.Max(x, Math.Max(y, z)) The outer call to Math.Max specifies arguments x and Math.Max(y, z). Before any method is called, all its arguments are evaluated to determine their values. If an argument is a method call, the method call is performed to determine its return value. So, in the preceding statement, Math.Max(y, z) is evaluated first to determine the maximum of y and z. Then the result is passed as the second argument to the other call to Math.Max, which returns the larger of its two arguments. Using Math.Max in this manner is a good example of software reusewe find the largest of three values by reusing Math.Max, which finds the largest of two values. Note how concise this code is compared to lines 6375 of Fig. 7.4. |