Guest User

Untitled

a guest
Apr 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. public class Animal implements Comparable  {
  2. //public class Animal  {
  3.    
  4.     private int size;       // the size of the animal
  5.     private int legLength;
  6.     public double weight;
  7.     private String name;
  8.    
  9.     /** Creates an animal of size 0.
  10.      */
  11.     public Animal ()
  12.     {
  13.         //System.out.println ("In AMINALS constructor");
  14.         this (0, "no name");
  15.     }
  16.    
  17.     /** Creates an animal of the given size.
  18.      *@param newSize The size of the animal created.
  19.      */
  20.     public Animal (int newSize)
  21.     {
  22.         //System.out.println ("In AMINALS constructor");
  23.         this(newSize, "no name");
  24.     }
  25.    
  26.     /** Creates an animal of the given size.
  27.      *@param name The name of the animal created.
  28.      */
  29.     public Animal (String name)
  30.     {
  31.         //System.out.println ("In AMINALS constructor");
  32.         this(0, name);
  33.     }
  34.    
  35.     /** Creates an animal of the given size.
  36.      *@param newSize The size of the animal created.
  37.      *@param name The name of the animal created.
  38.      */
  39.     public Animal (int newSize, String name)
  40.     {
  41.         //System.out.println ("In AMINALS constructor");
  42.         size = 0;
  43.         legLength = 10;
  44.         weight = 100;
  45.         this.name = name;
  46.     }
  47.     /** Creates a copy of an animal by copying the size.
  48.      *@param other The animal from which to create a copy.
  49.      */
  50.     public Animal (Animal other)
  51.     {
  52.         //System.out.println ("In AMINALS constructor");
  53.         size = other.getSize();
  54.         legLength = 10;
  55.         weight = 100;
  56.         name = other.getName();
  57.     }
  58.    
  59.    
  60.     /** Get the name of the animal.
  61.    */
  62.     public String getName ()
  63.     {
  64.         return name;
  65.     }
  66.  
  67.    
  68.     /** Get the size of the animal.
  69.      */
  70.     public int getSize ()
  71.     {
  72.         return size;
  73.     }
  74.    
  75.  
  76.     /** Set the size of the animal.
  77.      *@param newSize The new size of the animal.
  78.      */
  79.     public void setSize (int newSize)
  80.     {
  81.         size = newSize;
  82.     }
  83.    
  84.     /** A method which simulates the animal moving (galloping, jumping), by printing
  85.      * a message to the screen.
  86.      */
  87.     public void move ()
  88.     {
  89.         System.out.println ("The ANIMAL " + getName() + " moves .... ");
  90.  
  91.     }
  92.    
  93.    
  94.     /** A method simulates the animal moving around, by printing
  95.      * a message to the screen.
  96.      */
  97.     public void moveAround ()
  98.     {
  99.         System.out.println ("The ANIMAL " + getName() + " moves  around");
  100.         move ();
  101.         move ();
  102.         move ();
  103.        
  104.     }
  105.    
  106.     /** Overrides the toString method of the Object class
  107.      *  returns a string representing the important information in the Animal object
  108.      */
  109.      public String toString ()
  110.      {
  111.         String animalStr = "";
  112.         animalStr = animalStr + " ANIMAL  of size " + getSize();
  113.         return animalStr;
  114.     }
  115.  
  116.  
  117.    
  118.     /** Implementation of the method required in the Comparable interface.
  119.      *  Needed to use the built-in sort method for arrays.
  120.      * @param other  The object to which the object calling this method is
  121.      *   compared to.
  122.      * @return  Returns 0 if both objects are considered to have the same content.
  123.      *          Otherwise returns -1 if other is larger than this;
  124.      *                 and returns 1 if other is smaller than this.
  125.      **/
  126.    
  127.     public int compareTo (Object other)
  128.     {
  129.         int otherSize =  ((Animal) other).getSize();
  130.            
  131.         if ( other instanceof Frog && /*Animal is not Frog*/ )
  132.         {
  133.             return 1;
  134.         }
  135.         else
  136.         {
  137.             if ( /* Animal is Frog*/ && !(other instanceof Frog) )
  138.             {
  139.                 return - 1;
  140.             }
  141.             else
  142.             {
  143.                 if (size == otherSize)
  144.                 {
  145.                     return 0;
  146.                 }
  147.                 if (size < otherSize)
  148.                 {
  149.                     return -1;
  150.                 }
  151.                 else
  152.                 {
  153.                     return 1;
  154.                 }
  155.             }
  156.         }
  157.        
  158.     }
  159.    
  160. }
Add Comment
Please, Sign In to add comment