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: 
 27:     public int compareTo(Object o)
 28:     {
 29:         if ((o != null) && (o instanceof Fruit))
 30:         {
 31:             Fruit otherFruit = (Fruit)o;
 32:             return this.fruitName.compareTo(otherFruit.fruitName);
 33:         }
 34:         return -1;  // Default if other object is not a Fruit
 35:     }
 36:     /*
 37:     public int compareTo(Object o)
 38:     {
 39:         if ((o != null) && (o instanceof Fruit))
 40:         {
 41:             Fruit otherFruit = (Fruit) o;
 42:             if (fruitName.length() > otherFruit.fruitName.length())
 43:                 return 1;
 44:             else if (fruitName.length() < otherFruit.fruitName.length())
 45:                 return -1;
 46:             else
 47:                 return 0;
 48:         }
 49:         return -1;  // Default if other object is not a Fruit
 50:     }
 51:     */
 52: 
 53:     /*
 54:     */
 55: }