Menu
Coddy logo textTech

Recap - Student Records

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 35 of 110.

challenge icon

Challenge

Easy

Let'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 a Student class 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 gradeCount that returns how many grades are recorded
    • A computed getter average that calculates and returns the average of all grades as a double (return 0.0 if no grades exist)
    • A computed getter letterGrade that 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 print Grade [grade] added for [name]. If invalid, print Invalid grade: [grade]
    • A method getHighestGrade() that returns the highest grade, or 0 if no grades exist
    • A method displayRecord() that shows the student's academic summary
  • 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]

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: 95

Try 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