Home CPSC 240

Lab 12: Interfaces



To get experience implementing interfaces.


The Comparable Interface

Interfaces are used in situations where methods need to deal with objects of classes that are not necessarily known. An example of this is sorting objects of classes we create. We've seen that we can use Collections.sort to sort ArrayLists. But how can this method know how to sort Shape objects, or Item objects, or any other class we make?

For this lab, you will sort objects for the Student class we have worked on. We need some way to tell Collections.sort how to do this. Should it sort by id number, name, gpa, or something else?

The way this works is that the Student class will need to implement the interface called Comparable. Anything that implements this interface needs to provide a way to compare two objects of this type. That way, the sort method can be written so that it takes in an ArrayList of Comparable objects. Here's another case of polymorphism: anything that implements Comparable can be passed in.

Collections.sort will call upon the .compareTo() method to do the actual comparisons that are part of its sorting algorithm. This lets us specify how the objects should be sorted.

There are some classes in Java that need types filled in, like how we need to tell an ArrayList what type of data it's storing. The Comparable interface is one of these. When we implement the interface, we need to tell it what type of data we will compare Students with. In this case it's other Students, so you should implement the interface like this:

public class Student implements Comparable<Student> {

Then, you will have to implement the method compareTo which takes another Student object as its parameter. The method should return a negative number if this object should be before the other one, 0 if they are considered equal, and a positive number if this object should be after the parameter object.



You should edit Student.java so that it implements the Comparable interface and overrides the compareTo method to compare Student objects based on their names. You should look at last names first for the comparison. If the last names are the same, you should then use first names for comparison. If both first and last names are the same, you should use the student id as the comparison.

You can use Lab12.java to test your program.


Example Run

When you are done, the output of the program should look like the following:

Janiyah, Ali	1007	0.00	0
Kendra, Bowen	1003	0.00	0
Dania, Caldwell	1010	0.00	0
Milo, Dawson	1004	0.00	0
Annie, Diaz	1006	0.00	0
Ava, Doherty	1000	0.00	0
Duncan, Elliott	1018	0.00	0
Susan, Elliott	1013	0.00	0
Nate, Flores	1017	0.00	0
Kael, Ingram	1014	0.00	0
Boris, Keller	1008	0.00	0
Colin, Key	1005	0.00	0
Robert, Krause	1012	0.00	0
Liam, Landry	1019	0.00	0
Emmy, Levy	1016	0.00	0
Austin, Lindsey	1009	0.00	0
Leo, Martinez	1015	0.00	0
Madden, Oconnor	1011	0.00	0
Sal, Stevens	1002	0.00	0
Amy, Townsend	1001	0.00	0



When you are done, please submit the Java code for the Student class in Canvas.

Copyright © 2022 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.