Source of Fruit.java


  1: //Fruit.java
  2: 
  3: public class Fruit implements Comparable
  4: {
  5:     private String fruitName;
  6: 
  7:     public Fruit()
  8:     {
  9:         fruitName = "";
 10:     }
 11: 
 12:     public Fruit(String name)
 13:     {
 14:         fruitName = name;
 15:     }
 16: 
 17:     public void setName(String name)
 18:     {
 19:         fruitName = name;
 20:     }
 21: 
 22:     public String getName()
 23:     {
 24:         return fruitName;
 25:     }
 26:     public int compareTo(Object o)
 27:     {
 28:         if ((o != null) && (o instanceof Fruit))
 29:         {
 30:             Fruit otherFruit = (Fruit) o;
 31:             if (fruitName.length() > otherFruit.fruitName.length())
 32:                 return 1;
 33:             else if (fruitName.length() < otherFruit.fruitName.length())
 34:                 return -1;
 35:             else
 36:                 return 0;
 37:         }
 38:         return -1;  // Default if other object is not a Fruit
 39:     }
 40: 
 41:     /*
 42:     public int compareTo(Object o)
 43:     {
 44:         if ((o != null) && (o instanceof Fruit))
 45:         {
 46:             Fruit otherFruit = (Fruit) o;
 47:             if (fruitName.length() > otherFruit.fruitName.length())
 48:                 return 1;
 49:             else if (fruitName.length() < otherFruit.fruitName.length())
 50:                 return -1;
 51:             else
 52:                 return 0;
 53:         }
 54:         return -1;  // Default if other object is not a Fruit
 55:     }
 56:     */
 57: 
 58:     /*
 59:     public int compareTo(Object o)
 60:     {
 61:         if ((o != null) && (o instanceof Fruit))
 62:         {
 63:             Fruit otherFruit = (Fruit)o;
 64:             return this.fruitName.compareTo(otherFruit.fruitName);
 65:         }
 66:         return -1;  // Default if other object is not a Fruit
 67:     }
 68:     */
 69: }