Presentation is loading. Please wait.

Presentation is loading. Please wait.

T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 18 Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.

Similar presentations


Presentation on theme: "T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 18 Student Grades Application Introducing Two-Dimensional Arrays and RadioButton."— Presentation transcript:

1 T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 18 Student Grades Application Introducing Two-Dimensional Arrays and RadioButton s

2  2009 Pearson Education, Inc. All rights reserved. 2 Outline 18.1 Test-Driving the Student Grades Application 18.2 Two-Dimensional Rectangular Arrays 18.3 Using RadioButton s 18.4 Inserting Code into the Student Grades Application

3  2009 Pearson Education, Inc. All rights reserved. 3 In this tutorial you will learn: ■Understand the similarities and differences between one-dimensional and two-dimensional arrays. ■Declare and manipulate two-dimensional arrays. ■Understand how to use two-dimensional arrays. ■Use nested For...Next loops. ■Use RadioButton s to enable users to select exactly one option out of several. Objectives

4 Application Requirements  2009 Pearson Education, Inc. All rights reserved. 4 18.1 Test-Driving the Student Grades Application A teacher issues three tests to a class of 10 students. The grades on these tests are integers in the range from 0 to 100. The teacher has asked you to develop an application to keep track of each student’s average and the class average. The teacher has also asked that there be a choice to view the grades as either number or letter grades. The application should allow a user to input the student’s three test grades, then compute each student’s average and the class average.

5  2009 Pearson Education, Inc. All rights reserved. 5 Test-Driving the Student Grades Application ■Run the completed application (Fig. 18.1). Figure 18.1 | Running the completed Student Grades application.

6  2009 Pearson Education, Inc. All rights reserved. 6 Test-Driving the Student Grades Application (Cont.) Figure 18.2 | Inputting data to the Student Grades application. ■Enter numbers in the Test 1 :, Test 2 : and Test 3: TextBox es (Fig. 18.2).

7  2009 Pearson Education, Inc. All rights reserved. 7 Test-Driving the Student Grades Application (Cont.) ■Click the Submit Grades Button to calculate the student’s average (Fig. 18.3). Figure 18.3 | Displaying the student’s numerical grade. Numeric RadioButton selected by default

8  2009 Pearson Education, Inc. All rights reserved. 8 Test-Driving the Student Grades Application (Cont.) ■Once 10 students have been entered, all the controls in the Input Grades GroupBox are disabled (Fig. 18.4). Figure 18.4 | Input Grades GroupBox disabled after 10 students. Input Grades GroupBox disabled ListBox with numeric grades displayed Label with numeric class average displayed

9  2009 Pearson Education, Inc. All rights reserved. 9 Test-Driving the Student Grades Application (Cont.) ■Change the ListBox ’s data to letter grades (Fig. 18.5). Figure 18.5 | Displaying the students’ letter grades. Select the Letter RadioButton ListBox with letter grades displayed Label with letter class average displayed

10  2009 Pearson Education, Inc. All rights reserved. 10 ■Two-dimensional arrays require two indices to identify particular elements, in the form array(i, j). –Rectangular arrays are often used to represent tables of values arranged in rows and columns. ■Figure 18.6 illustrates a two-dimensional rectangular array. 18.2 Two-Dimensional Rectangular Arrays Figure 18.6 | Two-dimensional rectangular array with three rows and four columns.

11  2009 Pearson Education, Inc. All rights reserved. 11 ■Two-dimensional arrays are initialized much like one- dimensional arrays: Dim numbers As Integer(,) = New Integer(0 To 1, 0 To 1) {} numbers(0, 0) = 1 numbers(0, 1) = 2 numbers(1, 0) = 3 numbers(1, 1) = 4 ■Note that a comma (, ) is required inside the parentheses following the data type to indicate that the array is two-dimensional. ■You can also write the declaration as: Dim numbers(0 To 1, 0 To 1) As Integer 18.2 Two-Dimensional Rectangular Arrays (Cont.)

12  2009 Pearson Education, Inc. All rights reserved. 12 ■Two-dimensional arrays also may be initialized using an initializer list: Dim numbers As Integer(,) = New Integer(,) {{1, 2}, {3, 4}} ■The preceding declaration can also be written as Dim numbers As Integer(,) = {{1, 2}, {3, 4}} ■You can specify the lower bounds of an array implicitly: Dim numbers As Integer(,) = New Integer(1, 1) {} Dim numbers(1, 1) As Integer 18.2 Two-Dimensional Rectangular Arrays (Cont.)

13  2009 Pearson Education, Inc. All rights reserved. 13 ■A RadioButton is a small white circle that either is blank or contains a smaller dot. –When a RadioButton is selected, a dot appears in the circle. – RadioButton s represent a set of mutually exclusive options—only one can be selected at a time. –By default, all RadioButton s added directly to a Form or GroupBox become part of the same group. ■If the RadioButton is checked, the Checked property returns the Boolean value True. 18.3 Using RadioButton s

14  2009 Pearson Education, Inc. All rights reserved. 14 When the user clicks the Submit Grades Button: Retrieve the student’s grades from the TextBoxes Add the student’s test scores to the array Display the student’s test scores and average in the ListBox Display the class’s average in the Class average: Label Clear the student’s test scores from the TextBoxes If 10 students have been entered Disable the input controls When the user selects the Numeric RadioButton: Display each student’s numeric test scores and average in the ListBox Display the class’s numeric average in the Class average: Label When the user selects the Letter RadioButton: Display each student’s letter test scores and average in the ListBox Display the class’s letter average in the Class average: Label 18.3 Using RadioButton s (Cont.)

15  2009 Pearson Education, Inc. All rights reserved. 15 Figure 18.7 | ACE table for the Student Grades application. (Part 1 of 2.) ■Use an ACE table to convert the pseudocode into Visual Basic (Figure 18.7). Action/Control/Event (ACE) Table for the Student Grades Application

16  2009 Pearson Education, Inc. All rights reserved. 16 Figure 18.7 | ACE table for the Student Grades application. (Part 2 of 2.) Action/Control/Event (ACE) Table for the Student Grades Application (Cont.)

17  2009 Pearson Education, Inc. All rights reserved. 17 ■Select the View GroupBox on the template application’s Form, and add two RadioButton s. ■Name the left one numericRadioButton, and set its Text property to Numeric. Name the right one letterRadioButton, and set its Text property to Letter. ■Set the Checked property of numericRadioButton to True (Fig. 18.8). Adding RadioButton s to the View GroupBox Figure 18.8 | RadioButton s placed in the GroupBox.

18  2009 Pearson Education, Inc. All rights reserved. 18 GUI Design Tip Use RadioButton s when the user must choose only one option from a group.

19  2009 Pearson Education, Inc. All rights reserved. 19 GUI Design Tip Always place each group of RadioButton s in a separate container (such as a GroupBox ).

20  2009 Pearson Education, Inc. All rights reserved. 20 GUI Design Tip Align groups of RadioButton s either horizontally or vertically.

21  2009 Pearson Education, Inc. All rights reserved. 21 Good Programming Practice Append the RadioButton suffix to RadioButton control names.

22  2009 Pearson Education, Inc. All rights reserved. 22 Error-Prevention Tip To avoid subtle logic errors, one RadioButton in a group is often selected by default, by setting its Checked property to True. This can be done using code or by setting the value using the Properties window.

23  2009 Pearson Education, Inc. All rights reserved. 23 Declaring a Two-Dimensional Array ■Line 2 (Fig. 18.9) declares a 10-by-3 array of Integer s to contain the test scores. ■Each row in the array represents a student. Each column represents a test. Figure 18.9 | Declaring a two-dimensional array.

24  2009 Pearson Education, Inc. All rights reserved. 24 Finishing the Submit Grades Butto n’s Click Event Handler ■Double click the Submit Grades Button to display its Click event handler (Fig. 18.10). Figure 18.10 | Storing the student’s test scores. Store the student’s test scores in the array

25  2009 Pearson Education, Inc. All rights reserved. 25 ■The For...Next statement (Fig. 18.11) reviews each of the student’s test scores. ■Retrieve the highest index of the array’s second dimension, indicated by passing 1 as the argument to GetUpperBound. Finishing the Submit Grades Butto n’s Click Event Handler (Cont.)

26  2009 Pearson Education, Inc. All rights reserved. 26 Finishing the Submit Grades Butto n’s Click Event Handler (Cont.) Figure 18.11 | Displaying the output. Iterating over the student’s test scores Determining how to display the test scores Calling the LetterGrade method Displaying the student’s average Update the number of students Displaying the class average

27  2009 Pearson Education, Inc. All rights reserved. 27 ■If grades array is full, line 51 (Fig. 18.12) disables the Input Grades GroupBox. –Disabling a GroupBox disables all the controls it contains. ■The Length property of a two-dimensional array would return the total number of elements (rows times columns). Finishing the Submit Grades Butto n’s Click Event Handler (Cont.) Figure 18.12 | Application does not allow more than 10 data entries. Disable the input controls when the grades array is full

28  2009 Pearson Education, Inc. All rights reserved. 28 ■The For...Next statement (Fig. 18.13) sums the grades contained in the specified student row of the grades array. ■The format control string "{0:F}" formats the test average with exactly two digits following the decimal point. Coding Methods to Average Test Grades

29  2009 Pearson Education, Inc. All rights reserved. 29 Figure 18.13 | Calculating a student’s test average. Calculate the student’s test average Coding Methods to Average Test Grades (Cont.) Sum the student’s test grades

30  2009 Pearson Education, Inc. All rights reserved. 30 ■The CalculateStudentAverage method (Fig. 18.14) uses nested For...Next statements to iterate over the columns in each row. Figure 18.14 | Calculating the class’s test average. Coding Methods to Average Test Grades (Cont.) Outer For...Next statement iterates over each row Inner For...Next statement iterates over each column Calculate the class’s test average

31  2009 Pearson Education, Inc. All rights reserved. 31 ■Double click the Numeric RadioButton to generate its CheckedChanged event handler (Fig. 18.15). Figure 18.15 | Method numericRadioButton_CheckedChanged. Coding Event Handlers for the RadioButton s Displaying the class’s grades

32  2009 Pearson Education, Inc. All rights reserved. 32 ■Double click the Letter RadioButton to generate its event handler (Fig. 18.16). Figure 18.16 | Method letterRadioButton_CheckedChanged. Coding Event Handlers for the RadioButton s (Cont.) Displaying the class’s grades

33  2009 Pearson Education, Inc. All rights reserved. 33 ■Method DisplayClassGrades (Fig. 18.17) displays each student’s grades and test average, as well as the class average, in the format selected by the user. Coding Event Handlers for the RadioButton s (Cont.)

34  2009 Pearson Education, Inc. All rights reserved. 34 Figure 18.17 | Method DisplayClassGrades. Coding Event Handlers for the RadioButton s (Cont.) Displaying the class’s average Inner For...Next statement adds the grades in each column of the current row to the output String Outer For...Next statement iterates over each row of the array

35  2009 Pearson Education, Inc. All rights reserved. 35 ■Figure 18.18 presents the source code for the Student Grades application. Outline (1 of 11 ) Creating a two- dimensional array Assigning values to elements of a two- dimensional array

36  2009 Pearson Education, Inc. All rights reserved. 36 Outline (2 of 11 ) Using the Checked property of a RadioButton control Calling the LetterGrade method with an element of a two-dimensional array Accessing an element of a two-dimensional array Iterating over the columns in a row of a two-dimensional array

37  2009 Pearson Education, Inc. All rights reserved. 37 Outline (3 of 11 ) Determining the number of rows in a two-dimensional array Disabling all the controls in a GroupBox

38  2009 Pearson Education, Inc. All rights reserved. 38 Outline (4 of 11 ) Iterating over the columns in a row of a two- dimensional array Using the Checked property of a RadioButton control Calculating the student’s average and formatting it as a number with two digits after the decimal point

39  2009 Pearson Education, Inc. All rights reserved. 39 Outline (5 of 11 ) Using nested For... Next statements to iterate over the columns in each row of a two-dimensional array

40  2009 Pearson Education, Inc. All rights reserved. 40 Outline (6 of 11 ) Using the Checked property of a RadioButton control Calculating the class’s average and formatting it as a number with two digits after the decimal point

41  2009 Pearson Education, Inc. All rights reserved. 41 Outline (7 of 11 )

42  2009 Pearson Education, Inc. All rights reserved. 42 Outline (8 of 11 ) Handling the CheckedChanged event of a RadioButton control

43  2009 Pearson Education, Inc. All rights reserved. 43 Outline (9 of 11 )

44  2009 Pearson Education, Inc. All rights reserved. 44 Outline (10 of 11 ) Outer For...Next statement iterates over the rows of a two- dimensional array Inner For...Next statement iterates over the columns of a row in a two-dimensional array Using the control variable’s name to improve readability of nested For...Next statements

45  2009 Pearson Education, Inc. All rights reserved. 45 Outline (11 of 11 ) Using the control variable’s name to improve readability of nested For...Next statements


Download ppt "T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 18 Student Grades Application Introducing Two-Dimensional Arrays and RadioButton."

Similar presentations


Ads by Google