Spaces:
Running
Running
TurkuBasicOOPinJava
/
Week 6: Methods of OO Programming
/07A. Interface 'Comparable' [compareTo method]+++
| Interface Comparable | |
| When an object implements the INTERFACE 'Comparable', it can be compared to OTHER OBJECTS (USU instances of the SAME CLASS). | |
| The METHOD defined in the interface is called 'compareTo', and is utilized for example by the sorting methods in Java. | |
| This means that if your class implements the interface, | |
| => LATER WHEN U CREATE a 'LIST containing OBJECTS created FROM THAT CLASS' | |
| => THE LIST OF OBJECTS can be ordered by using the 'sort' method from class 'Collections'. | |
| Method 'compareTo' RETURNS an INTEGER by the following rules: | |
| - the method returns a 0, if the values are equal | |
| - the method returns a NEGATIVE value if the current (this) object is "SMALLER" than the other object | |
| - the method returns a POSITIVE value if the current (this) object is "LARGER" than the other object | |
| ====================================================== | |
| Note that the programmer must decide on how the actual comparison is made. | |
| For example, the order of two Student objects could be determined based on the | |
| - student id, | |
| - number of credits, or for example | |
| - name (in alphabetical order). | |
| Hence, it is important to think what is the correct method in the current situation. | |
| The interface uses GENERICS. | |
| This is discussed in detail in the last week of the course. | |
| Here, it means that the class defines the types of objects used in the comparison. | |
| In practice, it is almost always the same as the class itself: students are compared to other students, cars to other cars, and files to other files. | |
| Let's consider a class 'Student' which implements the 'Comparable' interface. | |
| The property used for comparison is the 'number of credits'. | |
| // Generic type definition for implementing the interface | |
| class Student implements Comparable<Student> { | |
| private String studentId; | |
| private String name; | |
| private int credits; | |
| // CONSTRUCTOR | |
| public Student(String studentId, String name, int credits) { | |
| this.studentId = studentId; | |
| this.name = name; | |
| this.credits = credits; | |
| } | |
| @Override | |
| public String toString() { | |
| return name + " (" + studentId + "), " + credits + " op."; | |
| } | |
| @Override | |
| public int compareTo(Student o) { | |
| if (this.credits == o.credits) { | |
| return 0; | |
| } | |
| else if (this.credits < o.credits) { | |
| return -1; | |
| } | |
| else { | |
| return 1; | |
| } | |
| } | |
| } | |
| For clarity, above, there was an entire conditional statement written in the method 'compareTo', | |
| but naturally, we could get the same result with a single expression: | |
| return this.credits - o.credits | |
| ====================================================== | |
| Now, we can SORT a list of student objects into natural order based on their credits: | |
| import java.util.ArrayList; | |
| import java.util.Collections; | |
| public class SortTest { | |
| public static void main(String[] args) { | |
| // CREATE OBJECTS | |
| Student o1 = new Student("1234", "Oscar", 15); | |
| Student o2 = new Student("4321", "Olive", 25); | |
| Student o3 = new Student("3333", "Paul", 7); | |
| Student o4 = new Student("9999", "Pauline", 19); | |
| // CREATE A LIST OF OBJECTS | |
| ArrayList<Student> students = new ArrayList<>(); | |
| students.add(o1); | |
| students.add(o2); | |
| students.add(o3); | |
| students.add(o4); | |
| // SORT HERE | |
| Collections.sort(students); | |
| for (Student o : students) { | |
| System.out.println(o); | |
| } | |
| } | |
| } | |
| Program output: | |
| Paul (3333), 7 op. | |
| Oscar (1234), 15 op. | |
| Pauline (9999), 19 op. | |
| Olive (4321), 25 op. | |
| ==================================================================== | |
| As another example, let's consider the class Car. | |
| In the example, the cars are sorted based on their 'make'. | |
| Note that we can "use the String class implementation of the Comparable class's " compareTo in our own class. | |
| class Car implements Comparable<Car> { | |
| private String make; | |
| private String model; | |
| public Car(String make, String model) { | |
| this.make = make; | |
| this.model = model; | |
| } | |
| @Override | |
| public String toString() { | |
| return make + " " + model; | |
| } | |
| // DEFAULT 'compareTo' COMPARES STRINGS IN ALPHABETICAL ORDER | |
| @Override | |
| public int compareTo(Car o) { | |
| return this.make.compareTo(o.make); | |
| } | |
| } | |
| Example of sorting an array of car objects: | |
| import java.util.Arrays; | |
| public class Example { | |
| public static void main(String[] args) { | |
| // CREATE OBJECTS | |
| Car a1 = new Car("Volvo", "440"); | |
| Car a2 = new Car("Porsche", "911"); | |
| Car a3 = new Car("Datsun", "100a"); | |
| Car a4 = new Car("Citroen", "C5"); | |
| // CREATE ARRAY OF OBJECTS | |
| Car[] cars = {a1, a2, a3, a4}; | |
| // SORT ARRAY | |
| Arrays.sort(cars); | |
| for (Car car : cars) { | |
| System.out.println(car); | |
| } | |
| } | |
| } | |
| Program output: | |
| Citroen C5 | |
| Datsun 100a | |
| Porsche 911 | |
| Volvo 440 | |