chapter 18 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 18 PowerPoint Presentation
Download Presentation
Chapter 18

Loading in 2 Seconds...

play fullscreen
1 / 16

Chapter 18 - PowerPoint PPT Presentation


  • 138 Views
  • Uploaded on

Chapter 18. Introducing 2 dimensional arrays Using radio buttons Revisit student grades application Problem definition.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 18' - lola


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 18
Chapter 18
  • Introducing 2 dimensional arrays
  • Using radio buttons
  • Revisit student grades application
  • Problem definition
slide2
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 average of the class as a whole. The teacher has also asked that there be a choice to view the grades as either numbers or letters. Letter grades should be calculated according to the grading system:

90–100 A 80–89 B 70–79 C 60–69 D Below 60 F

The application should allow a user to input the student’s name and three test grades, then compute each student’s average and the class average. The application should display number grades by default.

one dimensional array
One dimensional Array

SEVEN ELEMENTS

Name of Array

Position numbers

First Element Seventh Element

Position 0 Position6 (n-1)

2d arrays
2D Arrays
  • Two dimensional arrays are initialized just 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

  • (,) indicates 2D array
  • Alternate forms to declare
    • Dim numbers As Integer ( , ) = New Integer ( , ) {{1,2}, {3,4}}
    • Dim numbers As Integer ( , ) = New Integer (1,1) { }
radio buttons
Radio Buttons

Come on --- you didn’t think there would be a slide on this.

You all know what a radio button is?

developing application
Developing Application
  • Add the 2 radio buttons
    • Rename 1st as numericRadioButton and set its text property to Numeric
    • Rename 2nd as letterRadioButton and set its text property as letter
  • Create a 10 X 2 array under the public class. Remember 10 X 2 means (0 To 9, and 0 to 1)
logic
Logic
  • Declare array to store student name and average
  • With each click of submit button add student name to first column and average to second column of the array. To do this you will have to calculate the average of the three scores entered.
  • Calculate the running total of all the scores in the class for the class average
    • Running total = running total + elements of second column
  • Now based on which radio button is selected, display the results in the list box
  • Now create an event for radio button changed so that the values displayed can be changed as per the users requirements
developing application1
Declaring and Initializing the Array

Dim grades As String(,) = New String(0 To 9, 0 To 1) {}

Dim studentCount As Integer = 0

Write a function to calculate Grade

Function LetterGrade(ByVal grade As Double) As String

Select Case (grade)

Case Is >= 90

Return "A"

Case Is >= 80

Return "B"

Case Is >= 70

Return "C"

Case Is >= 60

Return "D"

Case Else

Return "F"

End Select

End Function

Developing Application
developing application2
Create a function testAverage(A,B,C)

Function TestAverage (ByVal One As String, ByVal Two As String, ByVal Three As String)

Return (Val(One)+ Val(Two) + Val (Three))/3

End Function

Developing Application
adding event handlers
Adding event handlers
  • Create button click event handler

Dim counter As Integer = 0

Dim gradeTotal As Double = 0

‘ add student name and test average

adding event handlers1
Adding event handlers

grades(studentCount, 0) = nameTextBox.Text

grades(studentCount, 1) = String.Format("{0:F}", TestAverage(test1TextBox.Text, test2TextBox.Text, test3TextBox.Text))

studentCount += 1

Do While counter < studentCount

gradeTotal += Convert.ToDouble(grades(counter, 1))

counter += 1

Loop

If letterRadioButton.Checked = True Then

gradesListBox.Items.Add(grades(studentCount - 1, 0) & ControlChars.Tab & ControlChars.Tab & LetterGrade(Convert.ToDouble(grades(studentCount - 1, 1))))

averageLabel.Text = LetterGrade(gradeTotal / studentCount)

Else

gradesListBox.Items.Add(grades(studentCount - 1, 0) & ControlChars.Tab & ControlChars.Tab & grades(studentCount - 1, 1))

averageLabel.Text = String.Format("{0:f}", gradeTotal / studentCount)

End If

nameTextBox.Clear()

test1TextBox.Clear()

test2TextBox.Clear()

test3TextBox.Clear()

If studentCount = 10 Then

submitButton.Enabled = False

End If

adding event handlers checked changed radio button
Adding event handlers – checked changed radio button
  • You have all the information that you need already stored in the arrays. You need to reformat it and display it.
  • On numeric radio button selected
    • Clear the list box
    • Total the average and display in the class average text box
    • Display the name and the score in the listbox
adding event handlers checked changed radio button1
Adding event handlers – checked changed radio button

Dim counter As Integer = 0

Dim gradeTotal As Double = 0

gradesListBox.Items.Clear()

Do While counter < studentCount

gradesListBox.Items.Add(grades(counter, 0) & grades(counter, 1))

counter += 1

Loop

If studentCount <> 0 Then

averageLabel.Text = String.Format("{0:f}", gradeTotal / studentCount)

End If

repeat for the letter grade
Repeat for the letter grade

Dim counter As Integer = 0

Dim gradeTotal As Double = 0

gradesListBox.Items.Clear()

Do While counter < studentCount

gradesListBox.Items.Add(grades(counter, 0) & LetterGrade(Convert.ToDouble(grades(counter, 1))))

counter += 1

Loop

If studentCount <> 0 Then

averageLabel.Text = LetterGrade(Val(averageLabel.Text))

End If