Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Experiment –3.

Student Name: Deepti Rathore UID: 21BCS11732


Branch: CSE Section/Group: 809-A
Semester: 3rd Date of Performance: 05/11/2022
Subject Name: Object Oriented Subject Code: 21CSH-218
Programming Using Java

1. Aim/Overview of the practical:


You are given a list of student information: ID, FirstName, and CGPA. Your task
is to rearrange them according to their CGPA in decreasing order. If two student
have the same CGPA, then arrange them according to their first name in
alphabetical order. If those two students also have the same first name, then order
them according to their ID. No two students have the same ID.

2. Objective:
To study the execution of Sorting in Java.

3. Source Code:

1. import java.util.*;
2.
3. class 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. }
23.
24. //Complete the code
25. public class Solution
26. {
27. public static void main(String[] args){
28. Scanner in = new Scanner(System.in);
29. int testCases = Integer.parseInt(in.nextLine());
30.
31. List<Student> studentList = new ArrayList<Student>();
32. while(testCases>0){
33. int id = in.nextInt();
34. String fname = in.next();
35. double cgpa = in.nextDouble();
36.
37. Student st = new Student(id, fname, cgpa);
38. studentList.add(st);
39.
40. testCases--;
41. }
42. Collections.sort(studentList, new Comparator<Student>() {
43. @Override
44. public int compare(Student s1, Student s2) {
45. if(s2.getCgpa()>s1.getCgpa()){
46. return 1;
47. }else if(s2.getCgpa()<s1.getCgpa()){
48. return -1;
49. }
50. return s1.getFname().compareTo(s2.getFname());
51. }
52. });
53. for(Student st: studentList){
54. System.out.println(st.getFname());
55. }
56. }
57. }
4. Result/Output:

You might also like