SSD1的exercise6答案

{声明:不得转载}
当时好像使用DJ Java Decompiler反编译iCarnegie给的.class文件,然后抄了一点代码,我给出的代码好像不能编译,各位仔细咯
Catfish.java
import java.util.Vector;
import java.util.Random;

/**
 * Catfish - simulates a catfish - can swim, eat, and consume
 * energy in the process.
 *
 *
 */
public class Catfish extends LivingBeing {

    /**
     * The catfish is born "alive".
     * Then it dies, becoming a corpse.
     */
    private static final String ALIVE = "alive";

    /**
     * The catfish is born "alive".
     * Then it dies, becoming a "dead" corpse.
     */
    private static final String DEAD = "dead";

   
    /**
     * 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;
   
   
    // Concept example: final. since it is a constant
    // Concept example: static. since only one value is needed
    //                         irrespective of number of object instances
    /**
     * String constant - used to indicate the direction catfish is facing.
     */
    private static final String RIGHT = "right";

    /**
     * String constant - used to indicate the direction catfish is facing.
     */
    private static final String LEFT = "left";

    /**
     * String constant - used to indicate the direction catfish is facing.
     */
    private static final String UP = "up";

    /**
     * String constant - used to indicate the direction catfish is facing.
     */
    private static final String DOWN = "down";

    /**
     * Name of species
     */
    private static final String SPECIES = "Catfish";

    /**
     * Row-wise location of the catfish
     */
    private int row;

    /**
     * Column-wise location of the catfish
     */
    private int column;

    /**
     * Is the catfish dead or alive?
     */
    private String deadOrAlive;
   
    /**
     * Amount of energy the catfish has.
     */
    private int energy;

    /**
     * Age expressed as blocks of time lived
     */
    private int age = 0;

    /**
     * Name of this catfish.
     */
    private final String name;

    /**
     * The simulation to which this catfish belongs.
     * This is needed so the catfish can send a message
     * to simulation and ask
     * for prey (or predator) in the neighboring locations.
     * Prey is food. Food is good!
     */
    private Simulation simulation;

    /**
     * Minimum energy level needed to survive.
     * The minimum could increase as the individual grows.
     */
    private int minEnergy;
   
    /**
     * Maximum energy level that the catfish could carry.
     * The maximum could change as the individual grows.
     */
    private int maxEnergy;
   
    /**
     * Which direction am I facing.
     */
    private String direction;

    /**
     *
     * Number of Catfish created
     */
    private static int nCatfishCreated = 0;
   
    private int energy_Gain_From_Meal;
    private static final int energy_Expend_to_Eat=2;
    private static final int energy_Expend_to_Swim=2;
  private static final int energy_Expend_to_Look_for_Food=2;
  private static final int minEnergy_Increases_In_a_Time_Block=5;
    private static final int maxEnergy_Increases_In_a_Time_Block=10;

    /**
     * Constructor. Initialize an catfish 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) {
            simulation = initialSimulation;
            nCatfishCreated=nCatfishCreated+1;
            name = SPECIES + nCatfishCreated;
            minEnergy = 10;
      maxEnergy = 100;
      energy = simulation.getRand().nextInt(maxEnergy - minEnergy) + minEnergy;
      direction=RIGHT;
      if(initialRow<=0){
          row=1;
      }else if(initialRow>10){
          row=10;
      }else{
            row=initialRow;
          }
         
          if(initialColumn<1){
          column=1;
      }else if(initialRow>10){
          column=10;
      }else{
            column=initialColumn;
          }
           
           
    }
   
    /**
     * Get the row at which the catfish is located
     *
     * @return - the row of the catfish's location.
     */       
    public int getRow() {
        return row;
    }

    /**
     * Get the column at which the catfish is located
     *
     * @return - the column of the catfish's location.
     */       
    public int getColumn() {
        return column;
    }

    /**
     * Get the catfish's age
     *
     * @return the age of the catfish expressed in blocks of time
     */
    public int getAge() {
        return age;
    }

    /**
     * Color of the catfish expressed in hex notation.
     * For example, the "green-est" color is "#00FF00",
     * "blue-est" is "#0000FF", the "red-est" is "#FF0000".
     *
     * @return the rgb color in hex notation. preceded by a pound character '#'
     */
    public String getColor() {
        return "#FFFFFF";       // default is white.
    }

    /**
     * Get the name of this catfish
     *
     * @return the name of the catfish.
     */
    public String getName() {
        return name;
    }
   
    /**
     * Get the minimum energy needed to live.
     *
     * @return the minimum energy needed for the catfish to live.
     */
    private int getMinEnergy() {
        return minEnergy;
    }
   
    /**
     * get the maximum energy that the catfish can carry.
     *
     * @return the maximum energy the catfish can carry.
     */
    private int getMaxEnergy() {
        return maxEnergy;
    }
   

    /**
     * Get the energy currently carried by the catfish.
     *
     * @return current energy level of the organism
     */
    public int getEnergy() {
        return energy;
    }

    /**
     * Sets energy level.
     * If new energy level is less than minimum energy level, the organism dies.
     * New energy level is capped at maximum energy level.
     */
    private void setEnergy(int newEnergy) {
        if (newEnergy < minEnergy)
        {
            energy=minEnergy;
            die();
        }else if(newEnergy > maxEnergy){
                energy=maxEnergy;
               }else{
                         energy=newEnergy;
                    }
                         
    }

    /**
     * Die: Change the deadOrAlive to DEAD.
     */
    public void die() {
        deadOrAlive = DEAD;
    }

    /**
     * Is the catfish dead?
     *
     * @return <code>true</code> if dead. <code>false</code>, otherwise.
     */
    public boolean isDead() {
        return (deadOrAlive == DEAD);
    }

    /**
     * Get the direction faced by the catfish.
     *
     * @return the facing direction.
     */
    private String getDirection() {
        return direction;
    }

    /**
     * Is the catfish hungry?
     *
     * @return True, if hungry. False, otherwise.
     */
    private boolean isHungry() {

        if (energy < 2*minEnergy)  // Hungry, if current energy level is less than twice the
                                    // amount needed for survival.
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Move the catfish to a new row, if new row is within lake bounds.
     *
     * @param newRow - the row to move to.
     * @return the row moved to. Lake boundary limits movement. -1, if dead.
     */
    private int moveToRow(int newRow) {
        if(deadOrAlive == ALIVE){
               if(newRow < row){
            direction = LEFT;
         }else{
              direction = RIGHT;
         }
            if (newRow < 1){
               
                newRow = 1;               
            }else if(newRow >10){
               
                newRow = 10;
            }
               
                row = newRow;
           
          }else if(deadOrAlive == DEAD){
              row=-1;
          }         
        return row;
    }

    /**
     * Move the catfish to a new column, if new column is within lake bounds.
     *
     * @param newColumn - the column to move to.
     * @return the column moved to. Lake boundary limits movement.
     */
    private int moveToColumn(int newColumn) {
        if(deadOrAlive == ALIVE){
                if(newColumn < column){
            direction = LEFT;
          }else{
              direction = RIGHT;
          }
            if (newColumn < 1){
               
                newColumn = 1;               
            }else if(newColumn >10){
               
                newColumn = 10;
            }
               
                column = newColumn;
           
          }else if(deadOrAlive == DEAD){
              column=-1;
          }         
        return column;
    }

    /**
     * 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(direction==RIGHT){
                return "/Catfish-right.gif";
            }
                else if(direction==LEFT){
                       return "/Catfish-left.gif";
                      }
                       else if(direction==UP){
                               return "/Catfish-up.gif";
                              }
                               else{
                                       return "/Catfish-down.gif";
                                      }
    }
 
    /**
     * Look for food in the neighborhood. Consume some energy in the process.
     *
     * @return a neighboring algae that is food.
     */
    private AlgaeColony lookForFoodInNeighborhood() {
        setEnergy(getEnergy() - energy_Expend_to_Look_for_Food);
        if(isDead()){
            return null;
          }
        Vector neighbors = simulation.getNeighbors(getRow(), getColumn(), 1);
        for(int 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;
        }else{           
          age++;
          swimInTheTake();
          eatAlgaeColony();
          minEnergy = minEnergy + minEnergy_Increases_In_a_Time_Block;
          maxEnergy = maxEnergy + maxEnergy_Increases_In_a_Time_Block;
          return;
        }
    }
public void swimInTheTake(){
          if(isDead()){
            return;
          }        
        if(isHungry()){
            AlgaeColony food = lookForFoodInNeighborhood();
            if(food != null)
            {
                setEnergy(getEnergy() - 2);
                if(isDead())
                    return;
                moveToRow(food.getRow());
                moveToColumn(food.getColumn());
            }
            return;
        }
        setEnergy(getEnergy() - energy_Expend_to_Swim);
        if(isDead()){
            return;
          }
        int directionswitch = simulation.getRand().nextInt(4);
        if(directionswitch == 0){
            moveToRow(getRow() - 1);
          }
        if(directionswitch == 1){
            moveToRow(getRow() + 1);
          }
        if(directionswitch == 2){
            moveToColumn(getColumn() - 1);
          }
        if(directionswitch == 3){
            moveToColumn(getColumn() + 1);
          }
  }
  public void eatAlgaeColony(){
          if(isDead()){
           return;
         }
        Vector foodMaybe = simulation.getNeighbors(getRow(), getColumn(), 0);
        for(int neighborIndex = 0; neighborIndex < foodMaybe.size(); neighborIndex++)
            if(foodMaybe.get(neighborIndex) instanceof AlgaeColony)
            {
                AlgaeColony alg = (AlgaeColony)foodMaybe.get(neighborIndex);
                energy_Gain_From_Meal = alg.giveUpEnergy(10);
                setEnergy((getEnergy() + energy_Gain_From_Meal) - energy_Expend_to_Eat);
                return;
            }

  }
                                 
}
 

Crocodile.java
import java.util.Vector;
import java.util.Random;

/**
 * Crocodile - simulates a Crocodile - can swim, eat, and consume
 * energy in the process.
 *
 *
 */
public class Crocodile extends LivingBeing {

    /**
     * The Crocodile is born "alive".
     * Then it dies, becoming a corpse.
     */
    private static final String ALIVE = "alive";

    /**
     * The Crocodile is born "alive".
     * Then it dies, becoming a "dead" corpse.
     */
    private static final String DEAD = "dead";

   
    /**
     * 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;
   
   
    // Concept example: final. since it is a constant
    // Concept example: static. since only one value is needed
    //                         irrespective of number of object instances
    /**
     * String constant - used to indicate the direction Crocodile is facing.
     */
    private static final String RIGHT = "right";

    /**
     * String constant - used to indicate the direction Crocodile is facing.
     */
    private static final String LEFT = "left";

    /**
     * String constant - used to indicate the direction Crocodile is facing.
     */
    private static final String UP = "up";

    /**
     * String constant - used to indicate the direction Crocodile is facing.
     */
    private static final String DOWN = "down";

    /**
     * Name of species
     */
    private static final String SPECIES = "Crocodile";

    /**
     * Row-wise location of the Crocodile
     */
    private int row;

    /**
     * Column-wise location of the Crocodile
     */
    private int column;

    /**
     * Is the Crocodile dead or alive?
     */
    private String deadOrAlive;
   
    /**
     * Amount of energy the Crocodile has.
     */
    private int energy;

    /**
     * Age expressed as blocks of time lived
     */
    private int age = 0;

    /**
     * Name of this Crocodile.
     */
    private final String name;

    /**
     * The simulation to which this Crocodile belongs.
     * This is needed so the Crocodile can send a message
     * to simulation and ask
     * for prey (or predator) in the neighboring locations.
     * Prey is food. Food is good!
     */
    private Simulation simulation;

    /**
     * Minimum energy level needed to survive.
     * The minimum could increase as the individual grows.
     */
    private int minEnergy;
   
    /**
     * Maximum energy level that the Crocodile could carry.
     * The maximum could change as the individual grows.
     */
    private int maxEnergy;
   
    /**
     * Which direction am I facing.
     */
    private String direction;

    /**
     *
     * Number of Crocodile created
     */
    private static int nCrocodilesCreated = 0;
   
    private  int energy_Gain_From_Meal;
    private static final int energy_Expend_to_Eat=5;
    private static final int energy_Expend_to_Swim=5;
  private static final int energy_Expend_to_Look_for_Food=5;
  private static final int minEnergy_Increases_In_a_Time_Block=5;
    private static final int maxEnergy_Increases_In_a_Time_Block=10;

    /**
     * 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) {
            simulation = initialSimulation;
            nCrocodilesCreated=nCrocodilesCreated+1;
            name = SPECIES + nCrocodilesCreated;
            minEnergy = 500;
      maxEnergy = 2000;
      energy = simulation.getRand().nextInt(maxEnergy - minEnergy) + minEnergy;
      direction=RIGHT;
      if(initialRow<=0){
          row=1;
      }else if(initialRow>10){
          row=10;
      }else{
            row=initialRow;
          }
         
          if(initialColumn<1){
          column=1;
      }else if(initialRow>10){
          column=10;
      }else{
            column=initialColumn;
          }
           
           
    }
   
    /**
     * Get the row at which the Crocodile is located
     *
     * @return - the row of the Crocodile's location.
     */       
    public int getRow() {
        return row;
    }

    /**
     * Get the column at which the Crocodile is located
     *
     * @return - the column of the Crocodile's location.
     */       
    public int getColumn() {
        return column;
    }

    /**
     * Get the Crocodile's age
     *
     * @return the age of the Crocodile expressed in blocks of time
     */
    public int getAge() {
        return age;
    }

    /**
     * Color of the Crocodile expressed in hex notation.
     * For example, the "green-est" color is "#00FF00",
     * "blue-est" is "#0000FF", the "red-est" is "#FF0000".
     *
     * @return the rgb color in hex notation. preceded by a pound character '#'
     */
    public String getColor() {
        return "#FFFFFF";       // default is white.
    }

    /**
     * Get the name of this Crocodile
     *
     * @return the name of the Crocodile.
     */
    public String getName() {
        return name;
    }
   
    /**
     * Get the minimum energy needed to live.
     *
     * @return the minimum energy needed for the Crocodile to live.
     */
    private int getMinEnergy() {
        return minEnergy;
    }
   
    /**
     * get the maximum energy that the Crocodile can carry.
     *
     * @return the maximum energy the Crocodile can carry.
     */
    private int getMaxEnergy() {
        return maxEnergy;
    }
   

    /**
     * Get the energy currently carried by the Crocodile.
     *
     * @return current energy level of the organism
     */
    public int getEnergy() {
        return energy;
    }

    /**
     * Sets energy level.
     * If new energy level is less than minimum energy level, the organism dies.
     * New energy level is capped at maximum energy level.
     */
    private void setEnergy(int newEnergy) {
        if (newEnergy < minEnergy)
        {
            energy=minEnergy;
            die();
        }else if(newEnergy > maxEnergy){
                energy=maxEnergy;
               }else{
                         energy=newEnergy;
                    }
                         
    }

    /**
     * Die: Change the deadOrAlive to DEAD.
     */
    public void die() {
        deadOrAlive = DEAD;
    }

    /**
     * Is the Crocodile dead?
     *
     * @return <code>true</code> if dead. <code>false</code>, otherwise.
     */
    public boolean isDead() {
        return (deadOrAlive == DEAD);
    }

    /**
     * Get the direction faced by the Crocodile.
     *
     * @return the facing Crocodile.
     */
    private String getDirection() {
        return direction;
    }

    /**
     * Is the Crocodile hungry?
     *
     * @return True, if hungry. False, otherwise.
     */
    private boolean isHungry() {

        if (energy < 2*minEnergy)  // Hungry, if current energy level is less than twice the
                                    // amount needed for survival.
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Move the Crocodile to a new row, if new row is within lake bounds.
     *
     * @param newRow - the row to move to.
     * @return the row moved to. Lake boundary limits movement. -1, if dead.
     */
    private int moveToRow(int newRow) {
        if(deadOrAlive == ALIVE){
            if (newRow < 1){
                if(newRow < row){
            direction = UP;
          }else{
              direction = DOWN;
          }
                row = 1;
            }else if(newRow >= 10){
                if(newRow < row){
            direction = UP;
          }else{
              direction = DOWN;
          }
                row = 10;
            }else{
                if(newRow < row){
            direction = UP;
          }else{
              direction = DOWN;
          }
                row = newRow;
            }
         }else if(deadOrAlive == DEAD){
              row=-1;
          }         
        return row;
    }

    /**
     * Move the Crocodile to a new column, if new column is within lake bounds.
     *
     * @param newColumn - the column to move to.
     * @return the column moved to. Lake boundary limits movement.
     */
    private int moveToColumn(int newColumn) {
        if(deadOrAlive == ALIVE){
            if (newColumn < 1){
                if(newColumn < column){
            direction = LEFT;
          }else{
              direction = RIGHT;
          }
                column = 1;               
            }else if(newColumn >= 10){
                if(newColumn < column){
            direction = LEFT;
          }else{
              direction = RIGHT;
          }
                column = 10;
            }else{
                if(newColumn < column){
            direction = LEFT;
          }else{
              direction = RIGHT;
          }
                column = newColumn;
            }
          }else if(deadOrAlive == DEAD){
              column=-1;
          }         
        return column;
    }

    /**
     * This individual belongs to the Crocodile species.
     * 
     * @return The string indicating the species
     */
    public String getSpecies() {
        return SPECIES;
    }

    /**
     * Crocodile 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 Crocodile
     *
     * @return filename of Crocodile image
     */
    public String getImage() {
        if(direction==RIGHT){
                return "/Crocodile-right.gif";
            }
                else if(direction==LEFT){
                       return "/Crocodile-left.gif";
                      }
                       else if(direction==UP){
                               return "/Crocodile-up.gif";
                              }
                               else{
                                       return "/Crocodile-down.gif";
                                      }
    }
 
   
   

    /**
     * Crocodile lives its life. It may lose or gain energy.
     */
    public void liveALittle() {
         if(isDead()){
         return;
        }else{           
          age++;
          swimInTheTake();
          eatCatfish();
          minEnergy = minEnergy + minEnergy_Increases_In_a_Time_Block;
          maxEnergy = maxEnergy + maxEnergy_Increases_In_a_Time_Block;
          return;
        }
    }
public void swimInTheTake(){

        setEnergy(getEnergy() - energy_Expend_to_Swim);
        if(isDead()){
            return;
          }
        int newRow = simulation.getRand().nextInt(10)+1;
        int newColumn = simulation.getRand().nextInt(10)+1;
        moveToRow(newRow);
        moveToColumn(newColumn);
        int directionswitch = simulation.getRand().nextInt(4);
        if(directionswitch == 0){
            moveToRow(getRow() - 1);
          }
        if(directionswitch == 1){
            moveToRow(getRow() + 1);
          }
        if(directionswitch == 2){
            moveToColumn(getColumn() - 1);
          }
        if(directionswitch == 3){
            moveToColumn(getColumn() + 1);
          }
  }
  public void eatCatfish(){
          if(isDead()){
           return;
         }
        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);
                energy_Gain_From_Meal = fish.getEnergy();
                setEnergy((getEnergy() + energy_Gain_From_Meal) - energy_Expend_to_Eat);
                return;
            }

  }
                                 
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值