📄 animal.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -