Question
Consider the following classes: public class Ch10_Student implements Comparable{ private int id; private int gpa; public Ch10_Student(int id, int gpa) { this.id = id; this.gpa
Consider the following classes:
public class Ch10_Student implements Comparable{
private int id; private int gpa;
public Ch10_Student(int id, int gpa)
{ this.id = id; this.gpa = gpa; }
public int getId(){return id;} public int getGpa(){return gpa;}
public int compareTo(Ch10_Student other){ return this.id - other.id; } }
import java.util.*;
public class Ch10_Student_Test{
public static void main(String[] args){
ArrayList students = new ArrayList();
students.add(new Ch10_Student(8, 4));
students.add(new Ch10_Student(7, 2));
students.add(new Ch10_Student(5, 3));
Collections.sort(students);
System.out.println(students.get(0).getGpa());
} }
I just have a question about this code; do I need to write a separate code for the comparable Interface or no, and if yes, what will be the code in the comparable Interface?
Thanks.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started