📄 algaecolony.java
字号:
/*!Begin Snippet:AlgaeColony*/
/*
* Created on Jul 4, 2003
*
*/
/**
* AlgaeColony does not move. If there is sunlight, a portion of the solar
* energy will be converted into life-energy.
*
* @author iCarnegie av
*/
public class AlgaeColony extends LivingBeing {
/**
* Lowest possible energy needed for a baby to survive.
*/
private static final int BABY_MIN_ENERGY = 5;
/**
* Maximum energy that a baby can store.
*/
private static final int BABY_MAX_ENERGY = 255;
/**
* Amount of energy needed to live for a block of time.
*/
private static final int ENERGY_TO_LIVE = 1;
/**
* Name of species
*/
private static final String SPECIES = "Algae";
/**
* Number of Algae objects created so far.
*/
private static int nAlgaeCreated = 0;
/**
* Constructor. Initialize an algae to start life at a specified
* location with a specified energy. If location is out of bounds,
* locate the algae at the nearest edge.
*
* @param initialRow - the row at which the algae is located
* @param initialColumn - the column at which the algae is located
* @param initialSimulation - the simulation that the algae belongs to
*/
public AlgaeColony(
int initialRow,
int initialColumn,
Simulation initialSimulation) {
super(
initialRow,
initialColumn,
initialSimulation,
SPECIES + nAlgaeCreated,
BABY_MIN_ENERGY,
BABY_MAX_ENERGY);
++nAlgaeCreated;
}
/**
* The mechanism to display Algae is to use its color.
*
* @return a constant defined in {@link Simulation#COLOR Simulation} class
*/
public String getDisplayMechanism() {
return Simulation.COLOR;
}
/**
* Get the species that the algae belongs to
*
* @return a string indicating the species.
*/
public String getSpecies() {
return SPECIES;
}
/**
* Get the color of Algae
*
* @return - the color as a string in hexademinal notation
*/
public String getColor() {
// Concept example: Distinction between local variable
// and instance variable.
// Note: Since energyLevel is declared as a *private* instance
// in LivingBeing it is not visible here.
// We create a local variable
// whose name happens to be the same as the instance variable.
// The local variable is assigned a value that is the
// instance variable's value.
int energy = getEnergy();
// Let us limit the energy to the color range of 0 to 255.
if (energy < 0) {
energy = 0;
}
if (energy > 255) {
energy = 255;
}
// Convert energy scale into green scale expressed in hex form.
String greenLevel = Integer.toHexString(energy);
// If energy is a value less than 16,
// there will be only one character.
// Since we need 2 characters for green, we pad left with a "0".
if (energy < 16) {
greenLevel = "0" + greenLevel;
}
// Now prepend with "00" to indicate there is no red
// and append with "00" to indicate there is no blue.
return "#00" + greenLevel + "00";
}
/**
* Algae is being eaten up.
* So, relinquish energy up to the amount requested.
* If no energy remains, die.
*
* @param energyWanted - amount of energy requested - expressed as int.
* @return - the amount of energy that algae can give up.
* If the requested energy is greater than the available energy,
* only the available energy will be given up.
*/
public int giveUpEnergy(int energyWanted) {
int energy = getEnergy();
// We do not know what it means to want negative energy!!!
if (energyWanted < 0) {
return 0;
}
if (energyWanted < energy) {
setEnergy(energy - energyWanted);
} else // caller is asking for more than energy than available.
// Give only what I have.
// I will die since my energy level falls below minimum.
{
energyWanted = energy;
setEnergy(getMinEnergy() - 1);
}
return energyWanted;
}
/**
* Algae lives its life. May gain or lose energy.
*/
public void liveALittle() {
if (isDead()) {
return;
}
super.liveALittle();
int row = getRow();
int column = getColumn();
int sun = simulation.getSunlight(row, column);
// Let us assume that algae can convert 50% of
// solar energy into life-energy.
// Algae has to give up some energy to live.
setEnergy((int) (sun * 0.5) + getEnergy() - ENERGY_TO_LIVE);
}
}
/*!End Snippet:AlgaeColony*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -