Section 6.5. GradeBook Case Study: Select...Case Multiple-Selection Statement


6.5. GradeBook Case Study: Select...Case Multiple-Selection Statement

Chapter 5 presented the If...Then single-selection statement and the If...Then...Else double-selection statement. Occasionally, an algorithm contains a series of decisions that test a variable or expression separately for each value that the variable or expression might assume. The algorithm then takes different actions based on those values. Visual Basic provides the Select...Case multiple-selection statement to handle such decision making.

GradeBook Class with Select...Case Statement to Count A, B, C, D and F Grades.

Figure 6.9 contains an enhanced version of the GradeBook class introduced in Chapter 4 and further developed in Chapter 5. The new version not only calculates the average of a set of numeric grades entered by the user, but uses a Select...Case statement to determine whether each grade is the equivalent of an A, B, C, D or F and to increment the appropriate grade counter. The class also displays a summary of the number of students who received each grade. An extra counter is used to display the number of students who received a perfect score of 100 on the exam. Figure 6.10 shows a sample execution of the GradeBookTest module that uses an object of class GradeBook to process a set of grades.

Figure 6.9. GradeBook class uses Select...Case statement to count A, B, C, D and F grades.

  1  ' Fig. 6.9: GradeBook.vb  2  ' GradeBook class uses Select...Case statement to count letter grades.  3  Public Class GradeBook  4     Private courseNameValue As String ' name of course  5     Private total As Integer ' sum of grades                        6     Private gradeCounter As Integer ' number of grades entered      7     Private aCount As Integer ' count of A grades                   8     Private bCount As Integer ' count of B grades                   9     Private cCount As Integer ' count of C grades                  10     Private dCount As Integer ' count of D grades                  11     Private fCount As Integer ' count of F grades                  12     Private perfectScoreCount As Integer ' count of perfect scores 13 14     ' constructor initializes course name; 15     ' Integer instance variables are initialized to 0 by default 16     Public Sub New (ByVal name As String) 17        CourseName = name ' initializes CourseName 18     End Sub ' New 19 20     ' property that gets and sets the course name; the Set accessor 21     ' ensures that the course name has at most 25 characters 22     Public Property CourseName() As String 23        Get ' retrieve courseNameValue 24           Return courseNameValue 25        End Get 26 27        Set(ByVal value As String) ' set courseNameValue 28           If value.Length <= 25 Then ' if value has 25 or fewer characters 29              courseNameValue = value ' store the course name in the object 30           Else ' if name has more than 25 characters 31              ' set courseNameValue to first 25 characters of parameter name 32              ' start at 0, length of 25 33              courseNameValue = value.Substring(0, 25) 34 35              Console.WriteLine( _ 36                 "Course name (" & value & ") exceeds maximum length (25).") 37              Console.WriteLine( _ 38                 "Limiting course name to first 25 characters." & vbCrLf) 39           End If 40        End Set 41     End Property ' CourseName 42 43     ' display a welcome message to the GradeBook user 44     Public Sub DisplayMessage() 45        Console.WriteLine("Welcome to the grade book for " _ 46            & vbCrLf & CourseName & "!" & vbCrLf) 47     End Sub ' DisplayMessage 48 49     ' input arbitrary number of grades from user 50     Public Sub InputGrades() 51        Console.Write( _ 52           "Enter the grades in the range 0-100, negative value to quit: ") 53        Dim grade As Integer = Console.ReadLine() ' input grade 45 55        ' loop until user enters a sentinel value 56        While grade >= 0 57           total += grade ' add grade to total 58           gradeCounter += 1 ' increment number of grades 59 60           ' call method to increment appropriate counter 61           IncrementLetterGradeCounter(grade) 62 63           ' input next grade 64           Console.Write("Enter the grades in the range 0-100, " & _ 65              "negative value to quit: ") 66           grade = Console.ReadLine() 67        End While 68     End Sub ' InputGrades 69 70     ' add 1 to appropriate counter for specified grade 71     Private Sub IncrementLetterGradeCounter(ByVal grade As Integer) 72        Select Case grade ' determine which grade was entered      73           Case 100 ' perfect score                                74              perfectScoreCount += 1 ' increment perfectScoreCount 75              aCount += 1 ' increment aCount                       76           Case 90 To 99 ' grade was between 90 and 99             77              aCount += 1 ' increment aCount                       78           Case 80 To 89 ' grade was between 80 and 89             79              bCount += 1 ' increment bCount                       80           Case 70 To 79 ' grade was between 70 and 79             81              cCount += 1 ' increment cCount                       82           Case 60 To 69 ' grade was between 60 and 69             83              dCount += 1 ' increment dCount                       84           Case Else ' grade was less than 60                      85              fCount += 1 ' increment fCount                       86        End Select                                                 87     End Sub ' IncrementLetterGradeCounter 88 89     ' display a report based on the grades entered by user 90     Public Sub DisplayGradeReport() 91        Console.WriteLine(vbCrLf & "Grade Report:") 92 93       ' if user entered at least one grade 94       If (gradeCounter > 0) Then 95          ' calculate average of all grades entered 96          Dim average As Double = total / gradeCounter 97 98          ' output summary of results 99           Console.WriteLine("Total of the {0} grades entered is {1}", _ 100            gradeCounter, total) 101          Console.WriteLine("Class average is {0:F2}", average) 102          Console.WriteLine("Number of students who received each grade:") 103          Console.WriteLine("A: " & aCount) ' display number of A grades 104          Console.WriteLine("B: " & bCount) ' display number of B grades 105          Console.WriteLine("C: " & cCount) ' display number of C grades 106          Console.WriteLine("D: " & dCount) ' display number of D grades 107          Console.WriteLine("F: " & fCount) ' display number of F grades 108          Console.WriteLine(vbCrLf & "Number of students who received" & _ 109             "perfect scores: " & perfectScoreCount) 110       Else ' no grades were entered, so output appropriate message 111          Console.WriteLine("No grades were entered") 112      End If 113    End Sub ' DisplayGradeReport 114 End Class ' GradeBook 

Figure 6.10. GradeBookTest creates a GradeBook object and invokes its methods.

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

[View full width]

Welcome to the grade book for CS101 Introduction to VB! Enter a grade in the range 0-100, negative value to quit: 99 Enter a grade in the range 0-100, negative value to quit: 92 Enter a grade in the range 0-100, negative value to quit: 45 Enter a grade in the range 0-100, negative value to quit: 57 Enter a grade in the range 0-100, negative value to quit: 63 Enter a grade in the range 0-100, negative value to quit: 71 Enter a grade in the range 0-100, negative value to quit: 76 Enter a grade in the range 0-100, negative value to quit: 85 Enter a grade in the range 0-100, negative value to quit: 90 Enter a grade in the range 0-100, negative value to quit: 100 Enter a grade in the range 0-100, negative value to quit: -1 Grade Report: Total of the 10 grades entered is 778 Class average is 77.80 Number of students who received each grade: A: 4 B: 1 C: 2 D: 1 F: 2 Number of students who received a perfect score: 1



Like earlier versions of the class, class GradeBook (Fig. 6.9) declares instance variable courseNameValue (line 4) and contains property CourseName (lines 2241) and method DisplayMessage (lines 4447), which access the course name and display a welcome message to the user, respectively. The class also contains a constructor (lines 1618) that initializes the course name.

This version of class GradeBook declares instance variables total (line 5) and gradeCounter (line 6), which keep track of the sum of the grades entered by the user and the number of grades entered, respectively. Lines 711 declare counter variables for each grade category. Line 12 declares perfectScoreCount, a counter for the number of students who received a perfect score of 100 on the exam. The class maintains total, gradeCounter and the six counters as instance variables so that they can be used or modified in any of the class's methods. Note that the class's constructor (lines 1618) sets only the course name; the remaining instance variables are Integers and are initialized to 0 by default.

This version of class GradeBook contains three additional methodsInputGrades, IncrementLetterGradeCounter and DisplayGradeReport. Method InputGrades (lines 5068) reads an arbitrary number of integer grades from the user using sentinel-controlled repetition and updates instance variables total and gradeCounter. The method calls method IncrementLetterGradeCounter (lines 7187) to update the appropriate lettergrade counter for each grade entered. Note that method IncrementLetterGradeCounter is declared Privatewe will discuss why later in the section. Method DisplayGradeReport (lines 90113) outputs a report containing the total of all the grades entered, the average of the grades, the number of students who received each letter grade and the number of students who received a perfect score.

Let's examine these methods in more detail. Lines 5153 in method InputGrades prompt the user to enter a grade, which is assigned to variable grade. In this example, we are using any negative number as a sentinel. The While statement (lines 5667) executes if there is more data to input (i.e., if the grade is not a sentinel value).

Line 57 adds grade to total. Line 58 increments gradeCounter. The DisplayGradeReport method uses these variables to compute the average. Line 61 calls method IncrementLetterGradeCounter to increment the appropriate letter-grade counter based on the numeric grade entered. Lines 6466 prompt the user and input the next grade.

Method IncrementLetterGradeCounter uses a Select...Case statement (lines 7286) to determine which counter to increment. In this example, we assume that the user enters a valid grade in the range 0100. A grade in the range 90100 represents an A, 80 89 represents a B, 7079 represents a C, 6069 represents a D and 059 represents an F.

Line 72

 Select Case grade 


begins the Select...Case statement. The expression following the keywords Select Case (in this case, grade) is called the controlling expression. The controlling expression is compared sequentially with each Case. If a matching Case is found, the code in the Case executes, then program control proceeds to the first statement after the Select...Case statement (line 87).

Common Programming Error 6.3

Duplicate Case statements are logic errors. At run time, the first matching Case is executed.


The first Case statement (line 73) determines whether the value of grade is equal to 100. If this is true, the statements in lines 7475 execute, incrementing both aCount (because a grade of 100 is an A) and perfectScoreCount. Note that a Case statement can specify multiple actionsin this case, incrementing both aCount and perfectScoreCount. The next Case statement (line 76) determines whether grade is between 90 and 99 inclusive. In this case, only aCount is incremented (line 77). Keyword To specifies the range; lines 7883 use this keyword to present a series of similar Cases. Each case increments the appropriate counter.

Common Programming Error 6.4

If the value on the left side of the To keyword in a Case statement is larger than the value on the right side, the Case is ignored during program execution; this is probably a logic error.


If no match occurs between the controlling expression's value and a Case label, the optional Case Else (lines 8485) executes. We use the Case Else in this example to process all controlling-expression values that are less than 60, that is, all failing grades. If no match occurs and the Select...Case does not contain a Case Else, program control simply continues with the first statement after the Select...Case. Case Else commonly is used to deal with invalid input. When employed, the Case Else must be the last Case.

The required End Select keywords terminate the Select...Case statement. Note that the body parts of the Select...Case statement are indented to emphasize structure and improve program readability.

Error-Prevention Tip 6.4

Provide a Case Else in Select...Case statements. Cases not handled in a Select...Case statement are ignored unless a Case Else is provided. The inclusion of a Case Else statement can facilitate the processing of exceptional conditions.


Types of Case Statements

Case statements also can use relational operators to determine whether the controlling expression satisfies a condition. For example

 Case Is < 0 


uses keyword Is along with the relational operator, <, to test for values less than 0.

Multiple values can be tested in a Case statement where the values are separated by commas, as in

 Case 0, 5 To 9 


which tests for the value 0 or values in the range 59.

GradeBookTest Module That Demonstrates Class GradeBook

Module GradeBookTest (Fig. 6.10) creates a GradeBook object (line 7). Line 9 invokes the object's DisplayMessage method to output a welcome message to the user. Line 10 invokes the object's InputGrades method to read grades from the user and keep track of the sum of all the grades entered and the number of grades. Recall that method InputGrades also calls method IncrementLetterGradeCounter to keep track of the number of students who received each letter grade. Line 11 invokes method DisplayGradeReport of class GradeBook, which outputs a report based on the grades entered (as in the sample execution in Fig. 6.10). Line 94 of class GradeBook (Fig. 6.9) determines whether the user entered at least one gradethis helps us avoid dividing by zero. If so, line 96 calculates the average of the grades. Lines 99109 then output the totals of all the grades, the class average, the number of students who received each letter grade and the number of students who received a perfect score. If no grades were entered, line 111 outputs an appropriate message.

Note that module GradeBookTest (Fig. 6.10) does not directly call GradeBook method IncrementLetterGradeCounter (lines 7187 of Fig. 6.9). This method is used exclusively by method InputGrades of class GradeBook to update the appropriate lettergrade counter as each new grade is entered by the user. Method IncrementLetterGradeCounter exists solely to support the operation of class GradeBook's other methods and thus is declared Private. Methods declared Private can be called only by other members of the class in which the Private methods are declared. Such Private methods are commonly referred to as utility methods or helper methods.

Using the With Statement

The With statement allows you to make multiple references to the same object in a concise manner. For example, we can replace lines 911 of Fig. 6.10 (which all reference the same object, gradeBook1) with

 With gradeBook1    .DisplayMessage() ' display welcome message    .InputGrades() ' read grades from user    .DisplayGradeReport() ' display report based on grades End With 


These lines of code are collectively known as a With statement block. At the beginning of the block, we specify the object (gradeBook1) that we will be using in the block. The With statement allows you to access an object's members in the block without having to specify the name of the object before the dot separator, e.g., .DisplayMessage() instead of gradeBook1.DisplayMessage().

Select...Case Statement UML Activity Diagram

Figure 6.11 shows the UML activity diagram for the general Select...Case statement. Again, note that (besides an initial state, transition arrows, merges, a final state and several notes) the diagram contains only action states and decisions.

Figure 6.11. Select...Case multiple-selection statement UML activity diagram.


In Chapter 11, Object-Oriented Programming: Polymorphism, we present a more elegant method of implementing multiple-selection logic. We use a technique called polymorphism to create programs that are often clearer, more manageable, and easier to extend than programs that use Select...Case logic.



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