KaiquanMah commited on
Commit
79054f0
·
verified ·
1 Parent(s): fe7addf

int/double, str (alphabetical)

Browse files
Week 6: Methods of OO Programming/07A. Interface 'Comparable' [compareTo method]+++ ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Interface Comparable
2
+ When an object implements the INTERFACE 'Comparable', it can be compared to OTHER OBJECTS (USU instances of the SAME CLASS).
3
+ The METHOD defined in the interface is called 'compareTo', and is utilized for example by the sorting methods in Java.
4
+
5
+ This means that if your class implements the interface,
6
+ => LATER WHEN U CREATE a 'LIST containing OBJECTS created FROM THAT CLASS'
7
+ => THE LIST OF OBJECTS can be ordered by using the 'sort' method from class 'Collections'.
8
+
9
+
10
+
11
+ Method 'compareTo' RETURNS an INTEGER by the following rules:
12
+ - the method returns a 0, if the values are equal
13
+ - the method returns a NEGATIVE value if the current (this) object is "SMALLER" than the other object
14
+ - the method returns a POSITIVE value if the current (this) object is "LARGER" than the other object
15
+
16
+
17
+
18
+
19
+
20
+ ======================================================
21
+
22
+
23
+ Note that the programmer must decide on how the actual comparison is made.
24
+ For example, the order of two Student objects could be determined based on the
25
+ - student id,
26
+ - number of credits, or for example
27
+ - name (in alphabetical order).
28
+ Hence, it is important to think what is the correct method in the current situation.
29
+
30
+ The interface uses GENERICS.
31
+ This is discussed in detail in the last week of the course.
32
+ Here, it means that the class defines the types of objects used in the comparison.
33
+ 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.
34
+
35
+ Let's consider a class 'Student' which implements the 'Comparable' interface.
36
+ The property used for comparison is the 'number of credits'.
37
+
38
+ // Generic type definition for implementing the interface
39
+ class Student implements Comparable<Student> {
40
+ private String studentId;
41
+ private String name;
42
+ private int credits;
43
+
44
+ // CONSTRUCTOR
45
+ public Student(String studentId, String name, int credits) {
46
+ this.studentId = studentId;
47
+ this.name = name;
48
+ this.credits = credits;
49
+ }
50
+
51
+ @Override
52
+ public String toString() {
53
+ return name + " (" + studentId + "), " + credits + " op.";
54
+ }
55
+
56
+ @Override
57
+ public int compareTo(Student o) {
58
+ if (this.credits == o.credits) {
59
+ return 0;
60
+ }
61
+ else if (this.credits < o.credits) {
62
+ return -1;
63
+ }
64
+ else {
65
+ return 1;
66
+ }
67
+ }
68
+ }
69
+
70
+
71
+
72
+
73
+ For clarity, above, there was an entire conditional statement written in the method 'compareTo',
74
+ but naturally, we could get the same result with a single expression:
75
+
76
+ return this.credits - o.credits
77
+
78
+
79
+
80
+
81
+
82
+ ======================================================
83
+
84
+
85
+
86
+
87
+ Now, we can SORT a list of student objects into natural order based on their credits:
88
+
89
+ import java.util.ArrayList;
90
+ import java.util.Collections;
91
+
92
+ public class SortTest {
93
+ public static void main(String[] args) {
94
+ // CREATE OBJECTS
95
+ Student o1 = new Student("1234", "Oscar", 15);
96
+ Student o2 = new Student("4321", "Olive", 25);
97
+ Student o3 = new Student("3333", "Paul", 7);
98
+ Student o4 = new Student("9999", "Pauline", 19);
99
+
100
+ // CREATE A LIST OF OBJECTS
101
+ ArrayList<Student> students = new ArrayList<>();
102
+ students.add(o1);
103
+ students.add(o2);
104
+ students.add(o3);
105
+ students.add(o4);
106
+
107
+ // SORT HERE
108
+ Collections.sort(students);
109
+
110
+ for (Student o : students) {
111
+ System.out.println(o);
112
+ }
113
+ }
114
+ }
115
+
116
+ Program output:
117
+ Paul (3333), 7 op.
118
+ Oscar (1234), 15 op.
119
+ Pauline (9999), 19 op.
120
+ Olive (4321), 25 op.
121
+
122
+
123
+
124
+ ====================================================================
125
+
126
+
127
+
128
+
129
+
130
+ As another example, let's consider the class Car.
131
+
132
+ In the example, the cars are sorted based on their 'make'.
133
+ Note that we can "use the String class implementation of the Comparable class's " compareTo in our own class.
134
+
135
+ class Car implements Comparable<Car> {
136
+ private String make;
137
+ private String model;
138
+
139
+ public Car(String make, String model) {
140
+ this.make = make;
141
+ this.model = model;
142
+ }
143
+
144
+ @Override
145
+ public String toString() {
146
+ return make + " " + model;
147
+ }
148
+
149
+ // DEFAULT 'compareTo' COMPARES STRINGS IN ALPHABETICAL ORDER
150
+ @Override
151
+ public int compareTo(Car o) {
152
+ return this.make.compareTo(o.make);
153
+ }
154
+ }
155
+
156
+
157
+
158
+
159
+
160
+
161
+
162
+ Example of sorting an array of car objects:
163
+
164
+ import java.util.Arrays;
165
+
166
+ public class Example {
167
+ public static void main(String[] args) {
168
+ // CREATE OBJECTS
169
+ Car a1 = new Car("Volvo", "440");
170
+ Car a2 = new Car("Porsche", "911");
171
+ Car a3 = new Car("Datsun", "100a");
172
+ Car a4 = new Car("Citroen", "C5");
173
+
174
+ // CREATE ARRAY OF OBJECTS
175
+ Car[] cars = {a1, a2, a3, a4};
176
+ // SORT ARRAY
177
+ Arrays.sort(cars);
178
+
179
+ for (Car car : cars) {
180
+ System.out.println(car);
181
+ }
182
+ }
183
+ }
184
+
185
+ Program output:
186
+ Citroen C5
187
+ Datsun 100a
188
+ Porsche 911
189
+ Volvo 440
190
+
191
+
192
+