📄 catfish.java
字号:
import java.util.Vector;
/*
* Created on Jul 5, 2003
*
*/
/**
* Catfish - simulates a catfish - can swim, eat, and consume
* energy in the process.
*
* @author iCarnegie av
*
*/
public class Catfish extends Animal {
/**
* Energy needed to swim in a block of time.
*/
private static final int ENERGY_TO_SWIM = 2;
/**
* Energy needed to look for food once.
*/
private static final int ENERGY_TO_LOOK_FOR_FOOD = 1;
/**
* Lowest possible energy needed for a baby to survive.
*/
private static final int BABY_MIN_ENERGY = 15;
/**
* Maximum energy that a baby can store.
*/
private static final int BABY_MAX_ENERGY = 100;
/**
* Name of species
*/
private static final String SPECIES = "Catfish";
/**
*
* Number of Catfish created
*/
private static int nCatfishCreated = 0;
/**
* Constructor. Initialize an algae to start life at a specified
* location with a specified energy. If location is out of bounds,
* locate the catfish at the nearest edge.
*
* @param initialRow - the row at which the catfish is located
* @param initialColumn - the column at which the catfish is located
* @param initialSimulation - the simulation that the catfish belongs to
*/
public Catfish(
int initialRow,
int initialColumn,
Simulation initialSimulation) {
super(
initialRow,
initialColumn,
initialSimulation,
SPECIES + nCatfishCreated,
BABY_MIN_ENERGY,
BABY_MAX_ENERGY);
++nCatfishCreated;
}
/**
* This individual belongs to the Catfish species.
*
* @return The string indicating the species
*/
public String getSpecies() {
return SPECIES;
}
/**
* Catfish should be displayed as an image.
*
* @return a constant defined in {@link Simulation#IMAGE Simulation} class
*/
public String getDisplayMechanism() {
return Simulation.IMAGE;
}
/**
* Get the image of the catfish
*
* @return filename of Catfish image
*/
public String getImage() {
if (getDirection() == RIGHT) {
return "/Catfish-right.gif";
}
if (getDirection() == LEFT) {
return "/Catfish-left.gif";
}
if (getDirection() == UP) {
return "/Catfish-up.gif";
}
if (getDirection() == DOWN) {
return "/Catfish-down.gif";
}
return "Catfish-right.gif";
}
/**
* Look for food in the neighborhood. Consume some energy in the process.
*
* @return a neighboring living being that is food.
*/
private LivingBeing lookForFoodInNeighborhood() {
int neighborIndex;
// Looking for food consumes energy.
setEnergy(getEnergy() - ENERGY_TO_LOOK_FOR_FOOD);
if (isDead()) {
return null;
}
Vector neighbors =
simulation.getNeighbors(getRow(), getColumn(), 1);
for (neighborIndex = 0;
neighborIndex < neighbors.size();
++neighborIndex) {
if (neighbors.get(neighborIndex) instanceof AlgaeColony) {
return (AlgaeColony) neighbors.get(neighborIndex);
}
}
return null;
}
/**
* Catfish lives its life. It may lose or gain energy.
*/
public void liveALittle() {
if (isDead()) {
return;
}
super.liveALittle();
// As I am growing bigger, I need to increase my minEnergy
// and maxEnergy.
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -