Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 36 ยท Projects and Practice

Project: Grade Book

Build a console grade book in VB.NET - classes, interfaces, dictionaries, LINQ statistics, letter grades, menus and input validation, with the VB program read-only and a runnable C# stand-in.

A second project, this time a console application: a teacher's grade book. It exercises classes and interfaces, Dictionary, LINQ statistics, a menu loop, and defensive input handling. Console programs are the ideal way to practice because there is no UI to distract from the logic.

The VB.NET compiler is not available here, so the VB program is read-only. The runnable C# stand-in runs the same design with scripted input in place of the keyboard, and uses only .NET classes that VB uses as well.

Requirements

  1. Add students and record scores (0-100) for named assessments.
  2. Show each student's average and letter grade.
  3. Show class statistics: mean, median, highest, lowest, standard deviation.
  4. Rank students; list those below a pass mark.
  5. Reject invalid input without crashing.

Design

classDiagram
    class Student {
      +String Name
      +Dictionary Scores
      +Average() Double
      +Letter() String
    }
    class GradeBook {
      -List~Student~ students
      +AddStudent(name)
      +Record(name, test, score)
      +Ranking() IEnumerable
      +ClassStats() Stats
    }
    GradeBook "1" o-- "*" Student

A grade book owns students; each student owns its scores by assessment name.

The VB program (read-only)

VB
Imports System.Linq

Public Class Student
    Public ReadOnly Property Name As String
    Public ReadOnly Property Scores As New Dictionary(Of String, Double)

    Public Sub New(name As String)
        Me.Name = name
    End Sub

    Public ReadOnly Property Average As Double
        Get
            Return If(Scores.Count = 0, 0.0, Scores.Values.Average())
        End Get
    End Property

    Public ReadOnly Property Letter As String
        Get
            Select Case Average
                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 Get
    End Property
End Class

Module Program
    Sub Main()
        Dim book As New GradeBook()
        Do
            Console.WriteLine("1) Add student  2) Record score  3) Report  0) Quit")
            Select Case Console.ReadLine()
                Case "1" : book.AddStudent(Prompt("Name: "))
                Case "2" : RecordScore(book)
                Case "3" : book.PrintReport()
                Case "0" : Exit Do
                Case Else : Console.WriteLine("Unknown choice")
            End Select
        Loop
    End Sub

    Private Function ReadScore(text As String) As Double?
        Dim value As Double
        If Double.TryParse(Prompt(text), value) AndAlso value >= 0 AndAlso value <= 100 Then
            Return value
        End If
        Return Nothing
    End Function
End Module

Notice Select Case Average with Case Is >= 90: VB allows range and comparison cases, which reads much better than a chain of Ifs. Parsing uses TryParse so bad input is a normal result, not an exception.

Runnable core with scripted input

C# Grade book core and report (C# stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The standard deviation here is the population version (divide by n); a sample version divides by n minus 1. Choosing between them is a statistics decision, and the code should say which one it uses.

Testing the logic

Because GradeBook knows nothing about the console, you can verify it in a unit test project (MSTest, xUnit or NUnit) with no input at all:

VB
<TestMethod>
Public Sub Letter_Is_B_For_85()
    Dim s As New Student("T")
    s.Scores("a") = 85
    Assert.AreEqual("B", s.Letter)
End Sub
C# Edge cases for letter grades

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Extend it

  • Weighted assessments (exam 60 percent, quizzes 40 percent).
  • Save and load with JSON.
  • A histogram of grades printed with String(c, n) bars.
  • Replace the console with a Windows Forms front end reusing the same GradeBook class.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why use Double.TryParse instead of Double.Parse for keyboard input?

    TryParse returns a Boolean and an out value, so users cannot crash the program with typos.
  2. 2. What does Select Case Average with Case Is >= 90 demonstrate?

    Case Is lets Select Case test with relational operators.
  3. 3. Which value is most important to test for a letter-grade function?

    Bugs concentrate at boundaries.
  4. 4. Why does GradeBook avoid Console calls in its methods?

    Separating logic from I/O makes it reusable and testable.

Summary

  • Model students and scores with classes and a dictionary; compute averages with LINQ.
  • Select Case with Case Is expresses grade bands clearly.
  • Validate with TryParse; test boundary values.
  • Keep logic free of console and UI code so it is reusable and testable.

Next: Practice Exercises and Solutions