⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crocodile.java

📁 《湖泊生态的简单模拟》(Simulation Of Living Beings In A Lake )。这是我修读美国卡耐基梅隆大学Carnegie Mellon University(CMU)课程s
💻 JAVA
字号:
import java.util.Vector;

/**
 * Crocodile - simulates a crocodile - can wade, eat, and consume 
 * energy in the process.
 * 
 * @author 张维
 * @version 1.0.0
 */
public class Crocodile extends Animal {
	  
	  /**
	   * Energy needed to wade in a block of time.
	   */
    private static final int ENERGY_TO_WADE = 10;
    
    /**
	   * Energy expended to eat once.
	   */
    private static final int ENERGY_TO_EAT = 10;
    
    /**
	   * Lowest possible energy needed for a baby to survive. 
	   */
    private static final int BABY_MIN_ENERGY = 1000;
    
    /**
	   * Maximum energy that a baby can store. 
	   */
    private static final int BABY_MAX_ENERGY = 2000;
    
    /**
	   * For each block of time, the min energy grows by a certain amount
	   */
    private static final int MIN_ENERGY_GROWTH_INCREMENT = 5;
    
    /**
	   * For each block of time, the max energy grows by a certain amount
	   */
    private static final int MAX_ENERGY_GROWTH_INCREMENT = 10;
    
    /**
	   * Name of species
	   */
    private static final String SPECIES = "Crocodile";
    
    /**
	   * 
	   * Number of Crocodile created
	   */
    private static int nCrocodilesCreated = 0;
    
    /**
	   * Constructor. Initialize an crocodile to start life at a specified 
	   * location with a specified energy. If location is out of bounds,
	   * locate the crocodile at the nearest edge.
	   * 
	   * @param initialRow - the row at which the crocodile is located
	   * @param initialColumn - the column at which the crocodile is located
	   * @param initialSimulation - the simulation that the crocodile belongs to
	   */
    public Crocodile(int initialRow, int initialColumn, Simulation initialSimulation) {
    	
        super(initialRow,
              initialColumn,
              initialSimulation,
              SPECIES + nCrocodilesCreated,
              BABY_MIN_ENERGY, BABY_MAX_ENERGY);
              
        nCrocodilesCreated++;
    }
    
    /**
	   * This individual belongs to the Crocodile species.
	   *  
	   * @return The string indicating the species
	   */
    public String getSpecies() {
    	
        return "Crocodile";
    }
    
   /**
	  * Crocodile should be displayed as an image.
	  * 
	  * @return a constant defined in {@link Simulation#IMAGE Simulation} class
    */
    public String getDisplayMechanism(){
    	
        return "image";
    }
    
    /**
	   * Get the image of the crocodile
	   * 
	   * @return filename of Crocodile image
	   */
    public String getImage() {
    	
        if(getDirection() == "right")
            return "/Crocodile-right.gif";
        if(getDirection() == "left")
            return "/Crocodile-left.gif";
        if(getDirection() == "up")
            return "/Crocodile-up.gif";
        if(getDirection() == "down")
            return "/Crocodile-down.gif";
        else
            return "Crocodile-right.gif";
    }
    
    /**
     * The crocodile wades to a new location in the lake if possible.  
     */
    private void wadeIfPossible() {
    	
        setEnergy(getEnergy() - ENERGY_TO_WADE);
        
        if(isDead()){
            return;
        } else {
               //Get the value of the boundary of the lake.
               int firstRow = simulation.getFirstRow();
               int firstColumn = simulation.getFirstColumn();
               int lastRow = simulation.getLastRow();
               int lastColumn = simulation.getLastColumn();
               
               //The crocodile wades randomly.
               int newRow = simulation.getRand().nextInt((lastRow - firstRow) + 1);
               int newColumn = simulation.getRand().nextInt((lastColumn - firstColumn) + 1);
                
               //Insure the new value is within lake boundary             
               newRow += firstRow;
               newColumn += firstColumn;
               
               moveToRow(newRow);
               moveToColumn(newColumn);
        }
    }
    
    /**
     * The Crocodile eats Catfish and gains  energy  from a meal
     * (i.e.the energy it stores is increased).
     */
    private void eatIfPossible() {
    	
        if(isDead())
            return;
            
        // Let us assume that the distant between the Catfish and the possible food(AlgaeColon) is 0,and
        // there may be other livebeings such as catfish ,algaecolon,crocodile.   
        Vector foodMaybe = simulation.getNeighbors(getRow(), getColumn(), 0);
        
        for(int neighborIndex = 0; neighborIndex < foodMaybe.size(); neighborIndex++)
            if(foodMaybe.get(neighborIndex) instanceof Catfish)
            {
                Catfish fish = (Catfish)foodMaybe.get(neighborIndex);
                
                int energyGained = fish.getEnergy();
                fish.die();
                setEnergy((getEnergy() + energyGained) - ENERGY_TO_EAT);
            }
    }
    
    /**
	   * Crocodile lives its life. It may lose or gain energy.
	   */
    public void liveALittle() {
    	
        if(isDead())
        {
            return;
        } 
        else {
            super.liveALittle();
            
            wadeIfPossible();
            eatIfPossible();
            
            // As I am growing bigger, I need to increase my minEnergy and maxEnergy.	       
            minEnergy = getMinEnergy() + MIN_ENERGY_GROWTH_INCREMENT	;
            maxEnergy = getMaxEnergy()+ MAX_ENERGY_GROWTH_INCREMENT ;
        }
    }    
}

⌨️ 快捷键说明

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