第二次实验Java:Task2

EXERCISE5要求

  1. 解压ex5(解压后放到tomcat文件夹下webapps).zip到webapps文件夹下,改名为ex5;
  2. work目录下有一个Catfish.java文件,这个是我们要完成的文件;
  3. 启动tomcat后,打开content的initialWorldFish.html,观察具体功能,如图:

点击之后如下界面,此时鱼儿是静止不动的:  

目前鱼儿是不动的,我们需要通过完成Catfish.java的内容,编译生成新的Catfish.class.然后达到鱼儿随机运动的目标,如下图:

 

4. work文件夹下有一个Catfish.java文件,按照Catfish.java文件中的提示,完成相应的内容,编译后覆盖掉老的Catfish.class文件,达到鱼儿随机移动的目标即为完成实验。

5. 代码完成涉及到对Simulation对象的方法的调用和涉及到Random的使用,在doc目录下有文档Simulation.html和文档Random.html,请大家参考。

最终提交的文件是Catfish.java

//导包,导入随机数Random的包
import java.util.Random;

public class Catfish extends LivingBeing {
    //The catfish is born "alive". Then it dies, becoming a "dead" corpse. 
	private String ALIVE = "alive";
    private String DEAD = "dead";
    private int ENERGY_TO_SWIM = 2;//Energy needed to swim in a block of time.
    private int row;//Row-wise location of the catfish
    private int column;//Column-wise location of the catfish
    private String imageFileName;//Image of the catfish - is really a filename
    private String deadOrAlive;//Is the catfish dead or alive?
    //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;
    
    

    public Catfish(int initialRow, int initialColumn, Simulation initialSimulation)
    {
        simulation = initialSimulation;
        deadOrAlive = ALIVE;//一开始活着
        age = 0;//需要补在下面的变量
        energy = 100;//需要补在下面的变量
        imageFileName = "Catfish-right.gif";
        row = initialRow;
        column = initialColumn;
    }

    //Get the row at which the catfish is located 
	public int getRow(){
        return row;//the row of the catfish's location.
	}

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

    //get filename of catfish image
	public String getImage(){
		return imageFileName;//filename of Catfish image
	}

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

    //Get the energy of the catfish
	public int getEnergy(){
        return energy;//current energy level of the catfish
	}

    //Set the deadOrAlive attribute to indicate that the catfish is dead.
	public void die(){
		deadOrAlive = DEAD;//现在死了

	}

    //Is the catfish dead?
	public boolean isDead() {
		if(deadOrAlive == DEAD)
			return true;
		else
			return false;
	}

    //Swim to a new location if possible.
	private void swimIfPossible(){
        //Consume ENERGY_TO_SWIM units of energy to swim. 
		energy = energy - ENERGY_TO_SWIM;
        //Check if there is any energy left after consumption.
        if(energy <= 0){
            return;
        }else{
            // Swim at random in one of four directions.
		    // 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.
            row = simulation.getRand().nextInt(10);
            column = simulation.getRand().nextInt(10);
            return;
        }
	}
	
    //Catfish lives its life. Dies if it has no energy left.
	public void liveALittle() {
        //If there is no energy left, send a "die" message to this catfish
		if(energy <= 0){
            die();
            return;
        }else{
            //Increment the age of the Catfish by 1
            age = age + 1;
            //Try to swim by sending a "swimIfPossible" message
            swimIfPossible();
            return;
        }
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DoorBreaker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值