Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Animal implements Comparable {
- //public class Animal {
- private int size; // the size of the animal
- private int legLength;
- public double weight;
- private String name;
- /** Creates an animal of size 0.
- */
- public Animal ()
- {
- //System.out.println ("In AMINALS constructor");
- this (0, "no name");
- }
- /** Creates an animal of the given size.
- *@param newSize The size of the animal created.
- */
- public Animal (int newSize)
- {
- //System.out.println ("In AMINALS constructor");
- this(newSize, "no name");
- }
- /** Creates an animal of the given size.
- *@param name The name of the animal created.
- */
- public Animal (String name)
- {
- //System.out.println ("In AMINALS constructor");
- this(0, name);
- }
- /** Creates an animal of the given size.
- *@param newSize The size of the animal created.
- *@param name The name of the animal created.
- */
- public Animal (int newSize, String name)
- {
- //System.out.println ("In AMINALS constructor");
- size = 0;
- legLength = 10;
- weight = 100;
- this.name = name;
- }
- /** Creates a copy of an animal by copying the size.
- *@param other The animal from which to create a copy.
- */
- public Animal (Animal other)
- {
- //System.out.println ("In AMINALS constructor");
- size = other.getSize();
- legLength = 10;
- weight = 100;
- name = other.getName();
- }
- /** Get the name of the animal.
- */
- public String getName ()
- {
- return name;
- }
- /** Get the size of the animal.
- */
- public int getSize ()
- {
- return size;
- }
- /** Set the size of the animal.
- *@param newSize The new size of the animal.
- */
- public void setSize (int newSize)
- {
- size = newSize;
- }
- /** A method which simulates the animal moving (galloping, jumping), by printing
- * a message to the screen.
- */
- public void move ()
- {
- System.out.println ("The ANIMAL " + getName() + " moves .... ");
- }
- /** A method simulates the animal moving around, by printing
- * a message to the screen.
- */
- public void moveAround ()
- {
- System.out.println ("The ANIMAL " + getName() + " moves around");
- move ();
- move ();
- move ();
- }
- /** Overrides the toString method of the Object class
- * returns a string representing the important information in the Animal object
- */
- public String toString ()
- {
- String animalStr = "";
- animalStr = animalStr + " ANIMAL of size " + getSize();
- return animalStr;
- }
- /** Implementation of the method required in the Comparable interface.
- * Needed to use the built-in sort method for arrays.
- * @param other The object to which the object calling this method is
- * compared to.
- * @return Returns 0 if both objects are considered to have the same content.
- * Otherwise returns -1 if other is larger than this;
- * and returns 1 if other is smaller than this.
- **/
- public int compareTo (Object other)
- {
- int otherSize = ((Animal) other).getSize();
- if ( other instanceof Frog && /*Animal is not Frog*/ )
- {
- return 1;
- }
- else
- {
- if ( /* Animal is Frog*/ && !(other instanceof Frog) )
- {
- return - 1;
- }
- else
- {
- if (size == otherSize)
- {
- return 0;
- }
- if (size < otherSize)
- {
- return -1;
- }
- else
- {
- return 1;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment