Advertisement
Ligh7_of_H3av3n

08. Academy Graduation

May 20th, 2024
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. package Lekciq;
  2.  
  3.  
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6. import java.util.stream.IntStream;
  7.  
  8. public class AcademyGraduation {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.  
  13.         Scanner scan = new Scanner(System.in); // Create a Scanner object for input
  14.         int numberOfStudents = Integer.parseInt(scan.nextLine()); // Read the number of students
  15.  
  16.         // Create a TreeMap to store student names and their grades
  17.         Map<String, List<Double>> graduationList = new TreeMap<>();
  18.  
  19.         // Iterate over each student
  20.         IntStream.range(0, numberOfStudents)
  21.                 .mapToObj(i -> scan.nextLine())
  22.                 .forEach(name -> {
  23.                     // Read the name of the student
  24.                     String studentName = name;
  25.  
  26.                     // Read the grades for the student and convert them to doubles
  27.                     List<Double> grades = Arrays.stream(scan.nextLine().split("\\s+"))
  28.                             .map(Double::parseDouble)
  29.                             .collect(Collectors.toList());
  30.  
  31.                     // Store the student name and their grades in the graduationList
  32.                     graduationList.put(studentName, grades);
  33.                 });
  34.  
  35.         // Iterate over the graduationList and print each student's name and their average grade
  36.         graduationList.forEach((name, grades) -> {
  37.             System.out.printf("%s is graduated with %s%n", name, getAverage(grades));
  38.         });
  39.     }
  40.  
  41.     // Method to calculate the average grade for a list of grades
  42.     private static Double getAverage(List<Double> grades) {
  43.         double sum = 0.0;
  44.         // Calculate the sum of grades
  45.         for (Double grade : grades) {
  46.             sum += grade;
  47.         }
  48.         // Calculate and return the average grade
  49.         return sum / grades.size();
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement