Advertisement
Aldin_SXR

Student.java

Mar 11th, 2024
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. public class Student implements Comparable<Student> {
  2.     String fullName;
  3.     String department;
  4.     double gpa;
  5.  
  6.     /* Constructor: set initial values of the Student object */
  7.     public Student(String fullName, String department, double gpa) {
  8.         this.fullName = fullName;
  9.         this.department = department;
  10.         this.gpa = gpa;
  11.     }
  12.  
  13.     /* Compare "this" student to "that" student (the other object) */
  14.     public int compareTo(Student that) {                                // 1
  15.         if (this.gpa > that.gpa) {                                      // 2
  16.             return 1;                                                   // 2
  17.         } else if (this.gpa < that.gpa) {                               // 3
  18.             return -1;                                                  // 3
  19.         } else {                                                        // 4
  20.             return 0;                                                   // 4
  21.         }
  22.     }
  23. }
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement