Advertisement
deiom

Untitled

Jun 24th, 2019
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 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. public class StudentGrades {
  28.  
  29. private static class Student {
  30. private String firstName;
  31. private String secondName;
  32. private double grade;
  33.  
  34. public Student(String firstName, String secondName, double grade) {
  35. this.firstName = firstName;
  36. this.secondName = secondName;
  37. this.grade = grade;
  38. }
  39. public double getGrade(){
  40. return this.grade;
  41. }
  42. }
  43.  
  44. public static void main(String[] args) {
  45. Scanner scanner = new Scanner(System.in);
  46. int n = Integer.parseInt(scanner.nextLine());
  47. List<Student> students = new ArrayList<>();
  48.  
  49. while (n-- > 0) {
  50. {
  51. String[] input = scanner.nextLine().split(" +");
  52. String firstName = input[0];
  53. String secondName = input[1];
  54. double grade = Double.parseDouble(input[2]);
  55. Student student = new Student(firstName, secondName, grade);
  56. students.add(student);
  57. }
  58. students.sort(Comparator.comparingDouble(Student::getGrade));
  59. for (Student student : students) {
  60. System.out.println(student.toString());
  61. }
  62.  
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement