-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain4.java
More file actions
76 lines (62 loc) · 2.23 KB
/
Copy pathMain4.java
File metadata and controls
76 lines (62 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.*;
class Student2 {
private int id;
private String firstName;
private double cgpa;
public Student2(int id, String firstName, double cgpa) {
this.id = id;
this.firstName = firstName;
this.cgpa = cgpa;
}
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public double getCgpa() {
return cgpa;
}
}
class StudentC implements Comparator<Student2> {
@Override
public int compare(Student2 s1, Student2 s2) {
if (s1.getCgpa() != s2.getCgpa()) {
// Sort by cgpa in descending order
return Double.compare(s2.getCgpa(), s1.getCgpa());
} else if (!s1.getFirstName().equals(s2.getFirstName())) {
// If cgpa is equal, sort by firstName in alphabetical order
return s1.getFirstName().compareTo(s2.getFirstName());
} else {
// If cgpa and firstName are equal, sort by id in ascending order
return Integer.compare(s1.getId(), s2.getId());
}
}
}
public class Main4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Student2> students = new ArrayList<>();
// Input number of students
//System.out.print("Enter the number of students: ");
int n = scanner.nextInt();
scanner.nextLine(); // Consume newline
// Input student information
//System.out.println("Enter student details for each student in the format: ID FirstName CGPA");
for (int i = 0; i < n; i++) {
//System.out.print("Enter details for student " + (i + 1) + ": ");
int id = scanner.nextInt();
String firstName = scanner.next();
double cgpa = scanner.nextDouble();
students.add(new Student2(id, firstName, cgpa));
}
// Sort students
Collections.sort(students, new StudentC());
// Print the first names of students after sorting
//System.out.println("Students sorted by CGPA, firstName, and ID:");
for (Student2 student : students) {
System.out.println(student.getFirstName());
}
scanner.close();
}
}