dzocesrce

[NP] Lab Exercises

Apr 23rd, 2025
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5. import java.util.*;
  6. import java.util.stream.Collectors;
  7.  
  8. class LabExercises {
  9.  
  10.     List<Student> students;
  11.  
  12.     public LabExercises() {
  13.         students = new ArrayList<>();
  14.     }
  15.  
  16.     public void printByAveragePoints(boolean b, int n) {
  17.         Comparator<Student> comparator = Comparator.comparing(Student::getAveragePoints).thenComparing(Student::getIndex);
  18.         if(!b)
  19.             comparator = comparator.reversed();
  20.         students.stream().sorted(comparator)
  21.                 .limit(n)
  22.                 .forEach(i-> System.out.println(i));
  23.     }
  24.  
  25.     public List<Student> failedStudents () {
  26.         Comparator<Student> comparator = Comparator.comparing(Student::getIndex).thenComparing(Student::getAveragePoints);
  27.         return students.stream().filter(i->!i.hasSignature())
  28.                 .sorted(comparator)
  29.                 .collect(Collectors.toList());
  30.     }
  31.  
  32.     public Map<Integer,Double> getStatisticsByYear() {
  33.  
  34.         return students.stream().filter(i->i.hasSignature()).collect(Collectors.groupingBy(
  35.                 Student::getYearOfStudies,
  36.                 Collectors.averagingDouble(Student::getAveragePoints)
  37.         ));
  38.  
  39.     }
  40.  
  41.     public void addStudent(Student student) {
  42.         students.add(student);
  43.     }
  44. }
  45.  
  46.  
  47. class Student {
  48.     private String index;
  49.     private List<Integer> points;
  50.  
  51.     public Student(String index, List<Integer> points) {
  52.         this.index = index;
  53.         this.points = points;
  54.     }
  55.  
  56.     public boolean hasSignature(){
  57.         return points.size()>7;
  58.     }
  59.  
  60.     public String getIndex() {
  61.         return index;
  62.     }
  63.  
  64.     public List<Integer> getPoints() {
  65.         return points;
  66.     }
  67.  
  68.     public double getAveragePoints(){
  69.         return points.stream().mapToDouble(p -> p).sum()/10.0;
  70.     }
  71.         public int getYearOfStudies(){
  72.         return 20 - Integer.parseInt(index.substring(0,2));
  73.     }
  74.  
  75.     @Override
  76.     public String toString() {
  77.         return String.format("%s %s %.2f", index, hasSignature() ? "YES" : "NO", getAveragePoints());
  78.     }
  79. }
  80. public class LabExercisesTest {
  81.  
  82.     public static void main(String[] args) {
  83.         Scanner sc = new Scanner(System.in);
  84.         LabExercises labExercises = new LabExercises();
  85.         while (sc.hasNextLine()) {
  86.             String input = sc.nextLine();
  87.             String[] parts = input.split("\\s+");
  88.             String index = parts[0];
  89.             List<Integer> points = Arrays.stream(parts).skip(1)
  90.                     .mapToInt(Integer::parseInt)
  91.                     .boxed()
  92.                     .collect(Collectors.toList());
  93.  
  94.             labExercises.addStudent(new Student(index, points));
  95.         }
  96.  
  97.         System.out.println("===printByAveragePoints (ascending)===");
  98.         labExercises.printByAveragePoints(true, 100);
  99.         System.out.println("===printByAveragePoints (descending)===");
  100.         labExercises.printByAveragePoints(false, 100);
  101.         System.out.println("===failed students===");
  102.         labExercises.failedStudents().forEach(System.out::println);
  103.         System.out.println("===statistics by year");
  104.         labExercises.getStatisticsByYear().entrySet().stream()
  105.                 .map(entry -> String.format("%d : %.2f", entry.getKey(), entry.getValue()))
  106.                 .forEach(System.out::println);
  107.  
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment