Section 7.6. GradeBook Case Study: Declaring Methods with Multiple Parameters


7.6. GradeBook Case Study: Declaring Methods with Multiple Parameters

Chapters 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 Maximum

The 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.

  1  ' Fig. 7.4: GradeBook.vb  2  ' Definition of class GradeBook that finds the maximum of three grades.  3  Public Class GradeBook  4     Private courseNameValue As String ' name of course  5     Private maximumGrade As Integer ' maximum of three  grades  6  7     ' constructor initializes course name  8     Public Sub New(ByVal name As String)  9        CourseName = name ' initializes CourseName 10        maximumGrade = 0 ' this value will be replaced by maximum grade 11     End Sub ' New 12 13     ' property that gets and sets the course name; the Set accessor 14     ' ensures that the course name has at most 25 characters 15     Public Property CourseName() As String 16        Get ' retrieve courseNameValue 17           Return courseNameValue 18        End Get 19 20        Set(ByVal value As String) ' set courseNameValue 21           If value.Length <= 25 Then ' if value has 25 or fewer characters 22              courseNameValue = value ' store the course  name in the object 23           Else ' if name has more than 25 characters 24              ' set courseNameValue to first 25 characters of parameter name 25              ' start at 0, length of 25 26              courseNameValue = value.Substring(0, 25) 27 28              Console.WriteLine( _ 29                 "Course name (" & value & ") exceeds maximum length (25).") 30              Console.WriteLine( _ 31                 "Limiting course name to first 25 characters." & vbCrLf) 32           End If 33        End Set 34     End Property ' CourseName 35 36     ' display a welcome message to the GradeBook user 37     Public Sub DisplayMessage() 38       Console.WriteLine("Welcome to the grade book for " _ 39           & vbCrLf & CourseName & "!" & vbCrLf) 40     End Sub ' DisplayMessage 41 42     ' input three grades from user 43     Public Sub InputGrades() 44        Dim grade1 As Integer ' first grade entered by user 45        Dim grade2 As Integer ' second grade entered by user 46        Dim grade3 As Integer ' third grade entered by user 47 48        Console.Write("Enter the first grade:" ) 49        grade1 = Console.ReadLine() 50        Console.Write("Enter the second grade: ") 51        grade2 = Console.ReadLine() 52        Console.Write("Enter the third grade: ") 53        grade3 = Console.ReadLine() 54 55        ' store the maximum in maximumGrade 56        maximumGrade = Maximum(grade1, grade2, grade3) 57     End Sub ' InputGrades 58 59     ' returns the maximum of its three integer parameters 60     Function Maximum(ByVal x As Integer, ByVal y As Integer, _ 61        ByVal z As Integer) As Integer 62 63        Dim maximumValue As Integer = x ' assume x is the largest to start 64 65        ' determine whether y is greater than maximumValue 66        If (y > maximumValue) Then 67           maximumValue = y ' make y the new maximumValue 68        End If 69 70        ' determine whether z is greater than maximumValue 71        If (z > maximumValue) Then 72           maximumValue = z ' make z the new maximumValue 73        End If 74 75        Return maximumValue 76     End Function ' Maximum 77 78     ' display a report based on the grades entered by user 79     Public Sub DisplayGradeReport() 80        ' output maximum of grades entered 81        Console.WriteLine("Maximum of grades entered: " & maximumGrade) 82     End Sub ' DisplayGradeReport 83  End Class ' GradeBook 

Figure 7.5. Application to test class GradeBook's Maximum method.

  1  ' Fig. 7.5: GradeBookTest.vb  2  ' Create a GradeBook object, input grades and display grade report.  3  Module GradeBookTest  4     Sub Main()  5        ' create GradeBook object  6        Dim gradeBook1 As New GradeBook("CS101 Introduction to VB")  7  8        gradeBook1.DisplayMessage() ' display welcome message  9        gradeBook1.InputGrades() ' read grades from user 10        gradeBook1.DisplayGradeReport() ' display report based on grades 11     End Sub ' Main 12  End Module ' GradeBookTest 

Welcome to the grade book  for CS101 Introduction to VB! Enter the first grade: 65 Enter the second grade: 87 Enter the third  grade: 45 Maximum of  grades entered: 87 



Welcome to the grade book  for CS101 Introduction to VB! Enter the first grade: 45 Enter the second grade: 65 Enter the third  grade: 87 Maximum of grades entered: 87 



Welcome to the grade book  for CS101 Introduction to VB! Enter the first grade: 87 Enter the second grade: 45 Enter the third  grade: 65 Maximum of grades entered: 87 



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.Max

Recall 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.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net