Source of Neighbor.java


  1: 
  2: public class Neighbor implements Cloneable
  3: {
  4:     private String name;
  5:     private int    numberOfChildren;
  6:     private Pet    familyPet;
  7:         
  8:     public Object clone( )
  9:     {
 10:         try
 11:         {
 12:             Neighbor copy = (Neighbor)super.clone( );
 13:             copy.familyPet = (Pet)familyPet.clone( );
 14:             return copy;
 15:         }
 16:         catch(CloneNotSupportedException e)
 17:         {//This should not happen.
 18:             return null; //To keep the compiler happy.
 19:         }
 20:     }
 21:         
 22:     public Pet getPet( )
 23:     {
 24:         return (Pet)familyPet.clone( );
 25:     }
 26:         
 27:         //Other methods
 28:         public void setName(String newName)
 29:         {
 30:                 name = newName;
 31:         }
 32:         
 33:         public void setChildren(int howMany)
 34:         {
 35:                 numberOfChildren = howMany;
 36:         }
 37:         
 38:         public void setPet(Pet thePet)
 39:         {
 40:                 familyPet = thePet;
 41:         }
 42:         
 43:         public void setNeighbor(String newName, int children, Pet thePet)
 44:         {
 45:                 setName(newName);
 46:                 setChildren(children);
 47:                 setPet(thePet);
 48:         }
 49:         
 50:         public int getNumberOfChildren()
 51:         {
 52:                 return numberOfChildren;
 53:         }
 54:         
 55:         public String getName()
 56:         {
 57:                 return name;
 58:         }
 59:         
 60:         public String getPetName()
 61:         {
 62:                 return familyPet.getName();
 63:         }
 64: 
 65:         public int getPetAge()
 66:         {
 67:                 return familyPet.getAge();
 68:         }
 69: 
 70:         public double getPetWeight()
 71:         {
 72:                 return familyPet.getWeight();
 73:         }
 74: 
 75:     public void writeOutput( )
 76:     {
 77:         System.out.println("Neighbor " + name + " has " +
 78:                                         numberOfChildren + 
 79:                                                         " children and this pet:");
 80:                 familyPet.writeOutput();
 81:         }
 82: }