Recap - Student Records
Part of the Object Oriented Programming section of Coddy's C# journey — lesson 35 of 70.
Challenge
EasyLet'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 aStudentclass in theRecordsnamespace that demonstrates proper encapsulation. Your student should have:- A
readonlyfieldName(string) that's publicly accessible but can only be set during construction - A private
readonlyfield 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
Averagethat calculates and returns the average of all grades, or0if 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!
- A
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.5Notice 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
1Fundamentals of OOP
External FilesNamespaces & DirectivesIntro to Classes & ObjectsThe 'this' KeywordMethods and ParametersFields vs PropertiesConstructorsObject InitializersRecap - Simple Calculator4Inheritance
Basic Inheritance (:) SyntaxThe 'base' KeywordVirtual & Override KeywordsSealed ClassesThe 'object' Base ClassRecap - Employee Hierarchy7Advanced Features
Operator OverloadingIndexers (this[])ToString() OverrideExtension MethodsRecap - Custom List2Properties & Static Members
Auto-Implemented PropertiesRead/Write-Only PropertiesStatic Fields & MethodsStatic ClassesExpression-Bodied Members5Polymorphism & Interfaces
Compile vs Runtime PolyInterface vs Abstract ClassMultiple InterfacesExplicit InterfacesUpcasting & DowncastingRecap - Shape Calculator3Class Architecture
Instance vs Static Data'readonly' & 'const' KeywordsBacking FieldsRecap - Bank Account Manager6Encapsulation
Access ModifiersProperties for EncapsulationData Hiding ImplementationImmutability PatternsRecap - Student Records