EXERCISE5要求
- 解压ex5(解压后放到tomcat文件夹下webapps).zip到webapps文件夹下,改名为ex5;
- work目录下有一个Catfish.java文件,这个是我们要完成的文件;
- 启动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;
}
}
}