Recap - Student Records
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 35 of 110.
Challenge
EasyLet's build a student records system that brings together all the encapsulation concepts you've learned in this chapter. You'll create a Student class that properly protects sensitive academic data while providing controlled access through getters, setters, and computed properties.
You'll organize your code into two files:
student.dart: Define aStudentclass that manages academic records with proper encapsulation:- A
final String id- the student ID should never change after creation - A public
String name- names can be updated - A private
List<int> _grades- the raw grade list should be protected - A constructor that takes the id, name, and an initial list of grades
- A getter
gradeCountthat returns how many grades are recorded - A computed getter
averagethat calculates and returns the average of all grades as adouble(return0.0if no grades exist) - A computed getter
letterGradethat returns a letter based on the average:'A'for 90+,'B'for 80-89,'C'for 70-79,'D'for 60-69, and'F'for below 60 - A method
addGrade(int grade)that only accepts grades between 0 and 100 (inclusive). If valid, add the grade and printGrade [grade] added for [name]. If invalid, printInvalid grade: [grade] - A method
getHighestGrade()that returns the highest grade, or0if no grades exist - A method
displayRecord()that shows the student's academic summary
- A
main.dart: Import your student class and demonstrate the encapsulation in action:- Create a student with id
'STU001', name'Emma', and initial grades[85, 92, 78] - Call
displayRecord() - Add a grade of
95 - Attempt to add an invalid grade of
150 - Add a grade of
88 - Call
displayRecord() - Print
Highest grade: [highest]
- Create a student with id
The displayRecord() method should print in this format:
===== Student Record =====
ID: [id]
Name: [name]
Grades Recorded: [gradeCount]
Average: [average]
Letter Grade: [letterGrade]Expected output:
===== Student Record =====
ID: STU001
Name: Emma
Grades Recorded: 3
Average: 85.0
Letter Grade: B
Grade 95 added for Emma
Invalid grade: 150
Grade 88 added for Emma
===== Student Record =====
ID: STU001
Name: Emma
Grades Recorded: 5
Average: 87.6
Letter Grade: B
Highest grade: 95Try it yourself
import 'student.dart';
void main() {
// TODO: Create a student with id 'STU001', name 'Emma', and initial grades [85, 92, 78]
// TODO: Call displayRecord()
// TODO: Add a grade of 95
// TODO: Attempt to add an invalid grade of 150
// TODO: Add a grade of 88
// TODO: Call displayRecord()
// TODO: Print "Highest grade: [highest]"
}
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesLibraries & ImportsIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsInstance VariablesConstructor BasicsRecap - Simple Calculator4Null Safety
Intro to Null SafetyNullable vs Non-NullableThe ? and ! OperatorsLate Keyword & Null SafetyNull-Aware OperatorsNull Safety in ClassesRecap - User Profile System7Abstract Classes & Interfaces
Abstract ClassesAbstract MethodsInterfaces in DartImplicit InterfacesImplementing vs ExtendingMultiple InterfacesRecap - Shape Calculator10Collections & Generics
List, Set, Map OverviewType-Safe CollectionsGeneric ClassesGeneric MethodsGeneric ConstraintsIterable & IteratorRecap - Generic Storage13Advanced OOP Concepts
Composition vs InheritanceExtension MethodsCallable ClassesSealed Classes (Dart 3)Records (Dart 3)Patterns & Matching (3.0)Enums with Methods16Project: Library Management
Project OverviewBook and User Classes2Constructors in Dart
Default ConstructorNamed ConstructorsInitializer ListsConstant ConstructorsFactory ConstructorsRedirecting ConstructorsRecap - Shape Builder5Encapsulation
Public vs Private MembersThe _ Prefix ConventionLibrary-Level PrivacyGetters & Setters DepthInformation HidingRecap - Student Records8Mixins
Introduction to MixinsCreating MixinsUsing Multiple Mixinson Keyword in MixinsMixin vs InheritanceMixin vs InterfaceRecap - Animal System11Special Methods
toString() OverridehashCode & == OverrideComparable Interfacecall() MethodnoSuchMethod OverrideRecap - Custom Collection14Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern3Class Properties
Instance vs Static MembersFinal & Const FieldsLate VariablesStatic Methods & FieldsGetters and SettersRecap - Bank Account Manager6Inheritance
Basic InheritanceThe super KeywordMethod OverridingThe @override AnnotationThe final Class KeywordConstructors & InheritanceRecap - Employee Hierarchy9Polymorphism
Polymorphism BasicsPolymorphism via InterfacesRuntime Type CheckingThe is & as OperatorsCovariant KeywordRecap - Payment Processor