Project Bonus Task & Solution: Determine the Best Performing Section in Final Exams
☰Fullscreen
Table of Content:
Bonus Task (Optional):
-
Display the subject names when inputting marks (Mathematics, Physics, English, Computer Application, and Biology) instead of just showing "Subject 1", "Subject 2", etc.
For example, while inputting marks for a student, the prompt should be:
Enter marks for student 1: Mathematics: Physics: English: Computer Application: Biology:
This Bonus Task makes the project more realistic by asking students to interact with subject-specific data, reinforcing the connection between real-world academic performance and programming.
Solution
Here’s the Java code:
import java.util.Scanner; public class BestPerformingSection { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Number of sections and subjects int sections = 3; // A, B, C int subjects = 5; // 5 subjects: Mathematics, Physics, English, Computer Application, Biology // Array to hold subject names String[] subjectNames = {"Mathematics", "Physics", "English", "Computer Application", "Biology"}; // Variables to store total marks for each section double[] sectionTotals = new double[sections]; double[] sectionAverages = new double[sections]; int[] studentsPerSection = new int[sections]; // Loop to input the number of students in each section for (int i = 0; i < sections; i++) { String sectionName = (i == 0) ? "A" : (i == 1) ? "B" : "C"; System.out.print("Enter the number of students in Section " + sectionName + ": "); studentsPerSection[i] = scanner.nextInt(); } // Loop through each section to input marks for (int i = 0; i < sections; i++) { String sectionName = (i == 0) ? "A" : (i == 1) ? "B" : "C"; System.out.println("\nEntering marks for Section " + sectionName + ":"); double sectionTotal = 0; // Loop through each student in the section for (int student = 0; student < studentsPerSection[i]; student++) { System.out.println("Enter marks for student " + (student + 1) + ":"); // Loop through each subject for the student for (int subject = 0; subject < subjects; subject++) { System.out.print(" " + subjectNames[subject] + ": "); // Display subject name double marks = scanner.nextDouble(); sectionTotal += marks; // Add marks to section total } } // Store total marks for the section sectionTotals[i] = sectionTotal; } // Calculate the average score for each section for (int i = 0; i < sections; i++) { sectionAverages[i] = sectionTotals[i] / (studentsPerSection[i] * subjects); } // Compare the averages to find the best performing section String bestSection = "A"; double highestAverage = sectionAverages[0]; if (sectionAverages[1] > highestAverage) { bestSection = "B"; highestAverage = sectionAverages[1]; } if (sectionAverages[2] > highestAverage) { bestSection = "C"; highestAverage = sectionAverages[2]; } // Output the result System.out.println("\n--- Results ---"); System.out.printf("Section A average score: %.2f\n", sectionAverages[0]); System.out.printf("Section B average score: %.2f\n", sectionAverages[1]); System.out.printf("Section C average score: %.2f\n", sectionAverages[2]); System.out.println("The best performing section is: " + bestSection + " with an average score of: " + highestAverage); // Close the scanner scanner.close(); } }
Explanation of Updates:
-
Subject Names Array:
- I added an array called
subjectNames
to hold the names of the five subjects: Mathematics, Physics, English, Computer Application, and Biology.
- I added an array called
-
Displaying Subject Names:
- When entering marks for each student, the program now prompts the user to enter marks for each subject by name instead of simply displaying "Subject 1", "Subject 2", etc.
-
Input and Processing Logic:
- The logic for processing the input and calculating the total and average marks for each section remains the same, but the subject names are now displayed as part of the input process.
Enter the number of students in Section A: 2 Enter the number of students in Section B: 2 Enter the number of students in Section C: 2 Entering marks for Section A: Enter marks for student 1: Mathematics: 80 Physics: 85 English: 78 Computer Application: 90 Biology: 82 Enter marks for student 2: Mathematics: 75 Physics: 80 English: 77 Computer Application: 88 Biology: 79 Entering marks for Section B: ... --- Results --- Section A average score: 81.25 Section B average score: 80.00 Section C average score: 83.50 The best performing section is: C with an average score of: 83.50