Skip to main content

Exercise Overview

This advanced exercise demonstrates how to work with complex data structures in Dart, specifically using Map<String, List<int>> to create a comprehensive student grade management system.

What You’ll Build

A system that manages student grades by subject and provides:

Grade Tracking

Store multiple grades for each subject

Average Calculation

Calculate the average grade for each subject

Performance Analysis

Find the best and worst performing subjects

Complete Reports

Generate comprehensive grade reports

Problem Statement

Given a Map containing subjects and their corresponding grades:
Create a system that:
1

Calculate subject averages

Compute the average grade for each subject
2

Find best performance

Identify the subject with the highest average
3

Find worst performance

Identify the subject with the lowest average
4

Generate complete report

Display all subjects with their grades and averages

Solution Architecture

Data Structure

We use two Maps: one to store the raw grades (Map<String, List<int>>) and another to store the calculated averages (Map<String, double>).

Function 1: Calculate Subject Averages

The forEach method on a Map provides two parameters:
  • materia: The key (subject name)
  • calificaciones: The value (list of grades)
This allows us to iterate through each subject and its grades simultaneously.

Function 2: Find Best Performance

This function uses a simple algorithm: start with 0.0 as the best average, then update it whenever we find a higher one.

Function 3: Find Worst Performance

We initialize peorPromedio to 100.0 (the maximum possible grade) so any actual grade will be lower.

Function 4: Display Complete Report

Function 5: Display Averages

The toStringAsFixed(2) method formats the decimal number to show exactly 2 decimal places, making the output more readable.

Complete Program

Sample Output

Key Concepts

Map Data Structure

Maps store key-value pairs for efficient data lookup

Nested Collections

Map<String, List<int>> combines Maps and Lists

forEach Method

Iterate through Map entries with key-value pairs

Data Aggregation

Calculate statistics from collections of data

Advanced Concepts

We use two Maps because:
  1. calificacionesPorMateria: Stores the raw data (all individual grades)
  2. promedios: Stores the calculated averages
This separation allows us to:
  • Keep the original data intact
  • Access both raw grades and averages when needed
  • Recalculate averages if grades change
Dart provides functional programming methods that can simplify the code:
In a production system, consider:

Practice Challenges

Challenge 1: Overall Average

Add a function to calculate the overall average across all subjects.

Challenge 2: Grade Distribution

Create a function that shows how many grades fall into different ranges (90-100, 80-89, etc.).

Challenge 3: Student Class

Expand the system to manage multiple students, each with their own subject grades.

Challenge 4: Grade Input

Add functionality to let users input new subjects and grades interactively.

Enhanced Version: Multiple Students

Best Practices

Type Safety

Always specify types: Map<String, List<int>>

Immutability

Consider using final for maps that won’t be reassigned

Null Safety

Check for empty collections before operations

Clear Naming

Use descriptive names like calificacionesPorMateria