Advertisement
mark79

Students

Jun 24th, 2019
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Students {
  4.  
  5.     private static class Student {
  6.         private String firstName;
  7.         private String secondName;
  8.         private double grade;
  9.  
  10.         public Student(String firstName, String secondName, double grade) {
  11.             this.firstName = firstName;
  12.             this.secondName = secondName;
  13.             this.grade = grade;
  14.         }
  15.  
  16.         public double getGrade() {
  17.             return this.grade;
  18.         }
  19.  
  20.         @Override
  21.         public String toString() {
  22.             return String.format("%s %s: %.2f", firstName, secondName, grade);
  23.         }
  24.     }
  25.  
  26.     public static void main(String[] args) {
  27.         Scanner scanner = new Scanner(System.in);
  28.         int n = Integer.parseInt(scanner.nextLine());
  29.         List<Student> students = new ArrayList<>();
  30.  
  31.         while (n-- > 0) {
  32.  
  33.             String[] input = scanner.nextLine().split("\\s+");
  34.             String firstName = input[0];
  35.             String secondName = input[1];
  36.             double grade = Double.parseDouble(input[2]);
  37.             Student student = new Student(firstName, secondName, grade);
  38.             students.add(student);
  39.         }
  40.         students.sort(Comparator.comparingDouble(Student::getGrade));
  41.         Collections.reverse(students);
  42.  
  43.         for (Student student : students) {
  44.             System.out.println(student.toString());
  45.         }
  46.  
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement