public class Fruit implements Comparable
1: public class Fruit implements Comparable
2: {
3: private String fruitName;
4:
5: public Fruit()
6: {
7: fruitName = "";
8: }
9: public Fruit(String name)
10: {
11: fruitName = name;
12: }
13: public void setName(String name)
14: {
15: fruitName = name;
16: }
17: public String getName()
18: {
19: return fruitName;
20: }
21: public int compareTo(Object o)
22: {
23: if ((o != null) &&
24: (o instanceof Fruit))
25: {
26: Fruit otherFruit = (Fruit) o;
27: return (fruitName.compareTo(otherFruit.fruitName));
28: /*** Alternate definition of comparison using fruit length ***/
29: /*
30: if (fruitName.length() > otherFruit.fruitName.length())
31: return 1;
32: else if (fruitName.length() < otherFruit.fruitName.length())
33: return -1;
34: else
35: return 0;
36: */
37: }
38: return -1; // Default if other object is not a Fruit
39: }
40: }