-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.java
More file actions
53 lines (42 loc) · 1.29 KB
/
Copy pathStudent.java
File metadata and controls
53 lines (42 loc) · 1.29 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
import java.util.Comparator;
public class Student implements Comparable<Student> {
private String regNo;
private String name;
private String email;
// Default constructor
public Student() {
}
// Overloaded constructor
public Student(String regNo, String name, String email) {
this.regNo = regNo;
this.name = name;
this.email = email;
}
// Getters and setters (encapsulation)
public String getRegNo() {
return regNo;
}
public void setRegNo(String regNo) {
this.regNo = regNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
// Implement compareTo for Comparable interface (natural ordering by regNo)
@Override
public int compareTo(Student other) {
return this.regNo.compareTo(other.regNo);
}
// Brief explanation: The Comparable interface allows objects to be sorted in Java Collections
// (e.g., ArrayList.sort() or Collections.sort()) by defining a natural order via compareTo().
// Here, students are ordered alphabetically by regNo using String comparison.
}