Advertisement
deiom

Untitled

Jun 24th, 2019
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. //Students
  2. //Write a program that receives n count of students and orders them by grade (in descending). Each student should
  3. //have first name (string), last name (string) and grade (floating-point number).
  4. //Input
  5. // First line will be a number n
  6. // Next n lines you will get a student info in the format "{first name} {second name} {grade}"
  7. //Output
  8. // Print each student in the following format "{first name} {second name}: {grade}"
  9. //Example
  10. //Input
  11. //4
  12. //Lakia Eason 3.90
  13. //Prince Messing 5.49
  14. //Akiko Segers 4.85
  15. //Rocco Erben 6.00
  16. //Output:
  17. //Rocco Erben: 6.00
  18. //Prince Messing: 5.49
  19. //Akiko Segers: 4.85
  20. //Lakia Eason: 3.90
  21.  
  22. import java.util.ArrayList;
  23. import java.util.Comparator;
  24. import java.util.List;
  25. import java.util.Scanner;
  26.  
  27.  
  28. public class StudentGrades {
  29.  
  30. private static class Student {
  31. private String firstName;
  32. private String secondName;
  33. private double grade;
  34.  
  35. public Student(String firstName, String secondName, double grade) {
  36. this.firstName = firstName;
  37. this.secondName = secondName;
  38. this.grade = grade;
  39. }
  40.  
  41. public double getGrade() {
  42. return this.grade;
  43. }
  44.  
  45. @Override
  46. public String toString() {
  47. return String.format("%s %s: %.2f", firstName, secondName, grade);
  48. }
  49. }
  50.  
  51. public static void main(String[] args) {
  52. Scanner scanner = new Scanner(System.in);
  53. int n = Integer.parseInt(scanner.nextLine());
  54. List<Student> students = new ArrayList<>();
  55.  
  56. while (n-- > 0) {
  57. String[] input = scanner.nextLine().split(" ");
  58. String firstName = input[0];
  59. String secondName = input[1];
  60. double grade = Double.parseDouble(input[2]);
  61. Student student = new Student(firstName, secondName, grade);
  62. students.add(student);
  63. }
  64. students.sort(Comparator.comparingDouble(Student::getGrade).reversed());
  65. for (Student student : students) {
  66. System.out.println(student.toString());
  67. }
  68.  
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement