Advertisement
desislava_topuzakova

05. Students

Jul 3rd, 2022
1,269
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None
  1. клас Student:
  2.  
  3. package students;
  4.  
  5. public class Student {
  6.     //полета -> характеристики (какво има студента?)
  7.     private String name;
  8.     private String lastName;
  9.     private double grade;
  10.  
  11.     //конструктор
  12.     public Student (String name, String lastName, double grade) {
  13.         //нов обект
  14.         //name = null
  15.         //lastName = null
  16.         //grade = 0.0
  17.         this.name = name;
  18.         this.lastName = lastName;
  19.         this.grade = grade;
  20.     }
  21.  
  22.     //методи -> функционалности (какво прави студента?)
  23.     public double getGrade() {
  24.         return this.grade;
  25.     }
  26.  
  27.     @Override
  28.     public String toString() {
  29.         //"{first name} {second name}: {grade}"
  30.         return String.format("%s %s: %.2f", this.name, this.lastName, this.grade);
  31.     }
  32. }
  33.  
  34.  
  35. клас Main:
  36. package students;
  37.  
  38. import java.util.ArrayList;
  39. import java.util.Comparator;
  40. import java.util.List;
  41. import java.util.Scanner;
  42.  
  43. public class Main {
  44.     public static void main(String[] args) {
  45.         Scanner scanner = new Scanner(System.in);
  46.         List<Student> studentList = new ArrayList<>();
  47.         int n = Integer.parseInt(scanner.nextLine()); //брой на студентите
  48.         for (int studentCount = 1; studentCount <= n; studentCount++) {
  49.             String personalData = scanner.nextLine();
  50.             //"Lakia Eason 3.90".split(" ") -> ["Lakia", "Eason", "3.90"]
  51.             String firstName = personalData.split(" ")[0];
  52.             String lastName = personalData.split(" ")[1];
  53.             double grade = Double.parseDouble(personalData.split(" ")[2]);
  54.  
  55.             Student student = new Student(firstName, lastName, grade);
  56.             studentList.add(student);
  57.         }
  58.  
  59.         //списък с обекти от клас Student
  60.         studentList.sort(Comparator.comparingDouble(Student :: getGrade) //ascending order по оценка
  61.                 .reversed()); //descending order по оценка
  62.  
  63.         for (Student student : studentList) {
  64.             System.out.println(student.toString());
  65.         }
  66.     }
  67. }
  68.  
Advertisement
RAW Paste Data Copied
Advertisement