Grade Point Average Example

Grade Point Average Example

Key Points:
  – Using a structure for data handling
  – Lists (lists of structures, listbox)
  – User input validation and numeric formatting  

The GPA example calculates the student’s Grade Point Average using data input by the user.  The data has two points per item – a type that is either T or Q indicating that the score comes from a test or from a quiz, and a score.  The score from a test will be a mark out of 100, and the score from a quiz will be a mark out of 20.  Therefore, the test scores will be weighted by a factor of 1 and the quiz scores weighted by a factor of 0.2.  Any number of scores can be entered.  The GPA is calculated as the sum of the scores divided by the sum of the weights.

Start a new Windows Forms project and create the following GUI elements on Form1:

GPA Screen

Control Name Type Text Non-default Properties
btnClear Button Clear  
txtType TextBox N/a CharacterCasing = Upper
txtScore TextBox N/a  
btnAdd Button Add  
btnAbout Button About  
lstEntries ListBox N/a  
btnRemove Button Remove  
btnCalculate Button Calculate  
lblGPA Label N/a  

Structure.  The example uses a simple structure called GPA which consists of a string named Type which must be either “T” or “Q”, and a double name Score.

Lists. The example iuses a list of GPA structures for holding the type and score for each quiz or test entered by the user.  A list, in the form of a listbox, is also used for displaying the scores as they are entered.

User validation.  A single line of code in the Add button click event confirms that the user has entered either T or Q for the type and a valid number for the score.  A message is displayed if either one is incorrect.  Note that there s no need to check for “t” or “q” as the character casing for the text box has been set to “Upper” so all input will be converted to upper case. Additional validation could be added – that the score for type T was 100 or less, and the score for type Q was 20 or less, and neither score was less than zero.

Display. The list box is updated after each item is added by clearing it and then listing each item in the GPA list.

Calculation.  The list of GPA structures is scanned to get the total weighting and the total score, so that the GPA can be calculated.

Formatting.  The result is calculated to 2 decimal places and displayed in the GPA label as a percentage.

About. In the Project menu option, select Add Windows Form, select the About Box, and click OK.  The form AboutBox1 will be created.  Adjust control properties in this form (eg, the image and the text in the text box). The About button displays this form.

Public Class Form1

Structure GPA
  Dim Type As String
  Dim Score As Double
  Public Sub New(ByVal TestType As String, ByVal TestScore As Integer)
    Type = TestType
    Score = TestScore
  End Sub
End Structure

Dim GPAList As List(Of GPA) = New List(Of GPA)

Private Sub btnClear_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnClear.Click
  GPAList.Clear()
  lstEntries.Items.Clear()
  lblGPA.Text = ""
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnAdd.Click
  Dim Marks As Double
  If (txtType.Text = "T" Or txtType.Text = "Q") _
    And Double.TryParse(txtScore.Text, Marks) Then
    Dim myGPA As GPA = New GPA
    myGPA.Type = txtType.Text
    myGPA.Score = Marks
    GPAList.Add(myGPA)
    listupdate()
  Else
    MsgBox("Invalid data. Pease Re-enter")
  End If
End Sub

Private Sub listupdate()
  lstEntries.Items.Clear()
  For Each myGPA As GPA In GPAList
    lstEntries.Items.Add(myGPA.Type & " " & myGPA.Score.ToString)
  Next
End Sub

Private Sub btnCalculate_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnCalculate.Click
  Dim TotalScore As Double = 0
  Dim TotalWeight As Double = 0
  For Each myGPA As GPA In GPAList
    If myGPA.Type = "T" Then
      TotalScore += myGPA.Score
      TotalWeight += 1
    End If
    If myGPA.Type = "Q" Then
      TotalScore += myGPA.Score
      TotalWeight += 0.2
    End If
  Next
  If TotalWeight <> 0 Then
    lblGPA.Text = Math.Round((TotalScore / TotalWeight), 2).ToString & "%"
  End If
End Sub

Private Sub btnRemove_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnRemove.Click
  If lstEntries.SelectedItems.Count = 1 Then
    GPAList.RemoveAt(lstEntries.SelectedIndex)
    lstEntries.Items.RemoveAt(lstEntries.SelectedIndex)
  Else
    MsgBox("Please select one item in the list to remove.")
  End If
End Sub
Private Sub btnAbout_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnAbout.Click
    AboutBox1.ShowDialog()
End Sub

End Class
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s