Find Maximum Average Score of Three Subjects for a Student

1. Introduction

Calculating the average score of students across multiple subjects and identifying those with the highest averages is a common task in educational systems. This process involves aggregating scores from various subjects for each student, computing the average, and then comparing these averages to find the top performers. Such operations are crucial for academic assessments, scholarships, and recognitions. This blog post will introduce a Java program that calculates the average score of three subjects for each student and identifies the student(s) with the maximum average score.

2. Program Steps

1. Define a class Student with properties for the student's name and scores for three subjects.

2. Create a list of Student objects and populate it with sample data.

3. Calculate the average score for each student and identify the maximum average score.

4. Find and display the name(s) of the student(s) with the maximum average score.

3. Code Program

import java.util.ArrayList;
import java.util.List;

// Step 1: Defining the Student class
class Student {
    String name;
    int score1, score2, score3;

    public Student(String name, int score1, int score2, int score3) {
        this.name = name;
        this.score1 = score1;
        this.score2 = score2;
        this.score3 = score3;
    }

    public double getAverage() {
        return (score1 + score2 + score3) / 3.0;
    }
}

public class MaxAverageScore {
    public static void main(String[] args) {
        // Step 2: Creating a list of students and populating it
        List<Student> students = new ArrayList<>();
        students.add(new Student("Alice", 90, 92, 93));
        students.add(new Student("Bob", 85, 84, 88));
        students.add(new Student("Charlie", 90, 92, 93)); // Same average as Alice

        // Step 3: Calculating the maximum average score
        double maxAverage = students.stream()
            .mapToDouble(Student::getAverage)
            .max()
            .orElse(Double.NaN);

        // Step 4: Finding and displaying students with the maximum average score
        System.out.println("Students with the maximum average score of " + maxAverage + ":");
        students.stream()
            .filter(s -> s.getAverage() == maxAverage)
            .forEach(s -> System.out.println(s.name));
    }
}

Output:

Students with the maximum average score of 91.66666666666667:
Alice
Charlie

Explanation:

1. The program defines a Student class with fields for the student's name and scores in three subjects. It also includes a method getAverage() to calculate the average score across the three subjects.

2. A list of Student objects is created and populated with sample data. This list represents a simple dataset of students and their scores.

3. The program calculates the maximum average score using Java Streams. The mapToDouble(Student::getAverage) method converts the stream of Student objects into a stream of average scores, which is then processed by max() to find the highest average. If no value is present (which theoretically cannot happen in this context), orElse(Double.NaN) ensures the program handles such a case gracefully.

4. After finding the maximum average score, the program filters the list of students to find those whose average score matches the maximum. It then prints the names of these students, demonstrating how to identify and display students with the top performance based on their average scores.

5. This example showcases how to model real-world data in Java, perform computations on collections of objects, and utilize streams for data processing, providing a clear and concise solution for academic assessment scenarios.

Comments