Menu
Coddy logo textTech

Recap - Student Records

Part of the Object Oriented Programming section of Coddy's C# journey — lesson 35 of 70.

challenge icon

Challenge

Easy

Let's build a student records system that brings together all the encapsulation techniques you've learned in this chapter. You'll create a secure system where student data is properly protected, grades are validated, and certain information remains immutable after creation.

You'll organize your code across two files:

  • Student.cs: Create a Student class in the Records namespace that demonstrates proper encapsulation. Your student should have:
    • A readonly field Name (string) that's publicly accessible but can only be set during construction
    • A private readonly field for the student ID (string) - this should never be directly accessible from outside
    • A private list to store grades (integers)
    • A public read-only property Average that calculates and returns the average of all grades, or 0 if no grades exist
    • A public method AddGrade(int grade) that only accepts grades between 0 and 100 (inclusive) - invalid grades should be silently ignored
    • A public method GetGradeCount() that returns how many valid grades have been recorded
    • A public method GetStudentInfo() that returns a string in the format "{Name} (ID: {last 4 characters of student ID})" - notice how you're exposing only a masked version of the ID!
    The constructor should accept the name and student ID in that order.
  • Program.cs: In your main file, create a student and add several grades to demonstrate how the encapsulation works. You'll show that valid grades are accepted, invalid grades are rejected, and the average is calculated correctly.

You will receive five inputs:

  • Student name
  • Student ID (at least 4 characters)
  • First grade to add (integer)
  • Second grade to add (integer, might be invalid like -5 or 150)
  • Third grade to add (integer)

Print the output in this format:

{GetStudentInfo() result}
Grades recorded: {GetGradeCount()}
Average: {Average}

For example, if the inputs are Emma Wilson, STU12345, 85, 150, and 90, the output should be:

Emma Wilson (ID: 2345)
Grades recorded: 2
Average: 87.5

Notice how the invalid grade of 150 was silently rejected, so only two grades were recorded. The student ID is protected - external code can never see the full ID, only the masked version through GetStudentInfo(). The name is readable but immutable. This is encapsulation working together to create a robust, well-protected class!

Try it yourself

using System;
using Records;

class Program
{
    public static void Main(string[] args)
    {
        // Read inputs
        string name = Console.ReadLine();
        string studentId = Console.ReadLine();
        int grade1 = Convert.ToInt32(Console.ReadLine());
        int grade2 = Convert.ToInt32(Console.ReadLine());
        int grade3 = Convert.ToInt32(Console.ReadLine());

        // TODO: Create a Student object with the name and studentId

        // TODO: Add all three grades using AddGrade method

        // TODO: Print the output in the required format:
        // {GetStudentInfo() result}
        // Grades recorded: {GetGradeCount()}
        // Average: {Average}
    }
}

All lessons in Object Oriented Programming