SSD1的exercise5答案


好像大一时用DJ java Decompiler反编译iCarnegie给的.class文件后抄了一些代码,我现在给的这些代码好像不能通过编译,各位仔细咯
import java.util.*;
/*
 * Created on Jan 6, 2004
 *
 */

/**
 * Basic Catfish - simulates a catfish - can swim and consume
 * energy in the process.
 *
 * @author iCarnegie srt
 *
 */
public class Catfish extends LivingBeing {

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


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



    /**
     * Energy needed to swim in a block of time.
     */
    private int ENERGY_TO_SWIM = 2;
       
   
   
    /**
     * Row-wise location of the catfish
     */
    private int row;


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


    /**
     * Image of the catfish - is really a filename
     */
    private String imageFileName;

   
    /**
     * Is the catfish dead or alive?
     */
    private String deadOrAlive;
   

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


    /**
     * The simulation to which this catfish belongs.
     * This is needed so the catfish can send a message
     * to simulation.
     */
    private Simulation simulation;


    /**
     * Constructor. Initialize a 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;

            deadOrAlive = ALIVE;    

            age = 0;
            energy = 10;       
           
            imageFileName = "/Catfish-right.gif";   

            row = initialRow;
            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 filename of catfish image
     *
     * @return filename of Catfish image
     */
    public String getImage(){
        return imageFileName;
    }

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

        return age;       // Student:Please complete this method.
    }

    /**
     * Get the energy of the catfish
     *
     * @return current energy level of the catfish
     */
    public int getEnergy(){
   
    return energy;    // Student:Please complete this method.
    }
   
    /**
     * Die: The Catfish dies.
     */
    public void die(){

                             // Student:Please complete this method.   

         deadOrAlive = DEAD; // Set the deadOrAlive attribute to indicate that the catfish is dead.
    }

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

        if(deadOrAlive == DEAD)
            return true;
        else
            return false;
    }

    /**
     * Swim to a new location if possible.
     * Consumes some energy.
     */
    private void swimIfPossible() {

             // Student:Please complete this method.
      energy=energy-ENERGY_TO_SWIM;   // Consume ENERGY_TO_SWIM units of energy to swim.

      if(energy <= 0)    // Check if there is any energy left after consumption.
      {
          return;
      }else
       {               // Swim at random in one of four directions.
            row = simulation.getRand().nextInt(10);    // Assign a random row location for the Catfish as follows.
                                                       // (1) Send the "getRand" message to the "simulation" object to get a random number generator.
                                                           // The "simulation" object is initialized in the constructor above.
                                                           // (2) Send the "nextInt" message to the random number generator obtained above.
                                                         // The "nextInt" behavior returns an integer between 0(inclusive) and the integer specified as a parameter(exclusive).
                                                           // Thus, specifiying a value of 10 to the "nextInt" behavior will return an integer between 0 and 9.
            column = simulation.getRand().nextInt(10); // Similarly, assign a random column location for the catfish
            return;
       }

    }

    /**
     * Catfish lives its life. Dies if it has no energy left.
     */
    public void liveALittle() {

        // Student:Please complete this method.

        if(energy<=0)
        {
            die();
            return;
            die();       // If there is no energy left, send a "die" message to this catfish
            return;
        }else
         {
            age=age+1;           // Increment the age of the Catfish by 1
          swimIfPossible();    // Try to swim by sending a "swimIfPossible" message
          return;
         }
  }     
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值