animal.java

来自「Java source code for the Ant Colony Opti」· Java 代码 · 共 93 行

JAVA
93
字号
package jwo.jpss.ants;      // Part of the ant simulation package.
import jwo.jpss.spatial.*;  // For spatial classes.

//  *******************************************************
/** Class for defining an animal. All animals are spatial 
  * objects and can live, die and feed in some way.
  * @author Jo Wood
  * @version 1.3, 18th September, 2001.
  */
//  *******************************************************

public class Animal extends SpatialObject implements Feeds 
{
    // ------------------- Object variables -------------------
    
    private int foodLevel;	// Food level of animal.
    private boolean alive;	// Is animal dead or alive?
    
    // --------------------- Constructor ---------------------
            
    /** Creates an animal with a given initial food level and footprint.
      * @param foodLevel Initial food level of animal.
      * @param footprint Animal's initial footprint.
      */
    public Animal(int foodLevel, Footprint footprint)
    {
    	super(footprint);
    	this.foodLevel = foodLevel;
    	alive = true;
    }
    
    // ----------------------- Methods -----------------------
    
    /** Let the animal eat some food and thus increase its food
      * level by the given value.
      * @param foodUnits Number of food units to eat.
      */
    public void eat(int foodUnits)
    {
    	foodLevel += foodUnits;
    }
    
    /** Let the animal metabolise some food and thus decrease its
      * food level by the given value.
      * @param foodUnits Number of food units to metabolise.
      */
    public void metabolise(int foodUnits)
    {
    	foodLevel -= foodUnits;

        // Check whether animal has enough food to survive.
    	if (foodLevel <0)
    	{
    	    alive = false;
    	    foodLevel = 0;
    	}
    }
    
    // ---------------- Accessor/mutator methods -------------
    
    /** Report the current food level of this animal.
      * @param return Current food level.
      */
    public int getFoodLevel()
    {
    	return foodLevel;
    }
    
    /** Set the current food level.
      * @param int foodLevel New food level for object.
      */
    public void setFoodLevel(int foodLevel)
    {
    	this.foodLevel = foodLevel;
        metabolise(0);
    }
    
    /** Determines if animal is alive or not.
      * @return True if animal is alive.
      */
    public boolean isAlive()
    {
    	return alive;
    }
    
    /** Kills the animal (or raises from the dead).
      * @param alive Animal is dead if false, alive if true.
      */
    public void setAlive(boolean alive)
    {
    	this.alive = alive;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?