-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuck.java
More file actions
29 lines (23 loc) · 726 Bytes
/
Duck.java
File metadata and controls
29 lines (23 loc) · 726 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package TemplatePattern.JavaSortExample;
public class Duck implements Comparable<Duck> {
private String name;
private int weight;
public Duck(String name, int weight) {
this.name = name;
this.weight = weight;
}
public String toString() {
return name + " weighs " + weight;
}
// In Java we override the compareTo method from the Comparable interface to define the sorting order
@Override
public int compareTo(Duck otherDuck) {
if(this.weight < otherDuck.weight) {
return -1;
} else if(this.weight == otherDuck.weight) {
return 0;
} else { // this.weight > otherDuck.weight
return 1;
}
}
}