Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Student implements Comparable<Student> {
  4.     private int id;
  5.     private String fname;
  6.     private double cgpa;
  7.     public Student(int id, String fname, double cgpa) {
  8.         super();
  9.         this.id = id;
  10.         this.fname = fname;
  11.         this.cgpa = cgpa;
  12.     }
  13.     public int getId() {
  14.         return id;
  15.     }
  16.     public String getFname() {
  17.         return fname;
  18.     }
  19.     public double getCgpa() {
  20.         return cgpa;
  21.     }
  22.     public int compareTo(Student to) {
  23.         return (new Double(to.getCgpa())).compareTo(this.getCgpa());
  24.     }
  25. }
  26.  
  27. //Complete the code
  28. public class Solution
  29. {
  30.     public static void main(String[] args){
  31.         Scanner in = new Scanner(System.in);
  32.         int testCases = Integer.parseInt(in.nextLine());
  33.        
  34.         List<Student> studentList = new ArrayList<Student>();
  35.         while(testCases>0){
  36.             int id = in.nextInt();
  37.             String fname = in.next();
  38.             double cgpa = in.nextDouble();
  39.            
  40.             Student st = new Student(id, fname, cgpa);
  41.             studentList.add(st);
  42.            
  43.             testCases--;
  44.         }
  45.        
  46.         Collections.sort(studentList);
  47.         for(Student st: studentList){
  48.             System.out.println(st.getFname());
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement