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

📄 catfish.java

📁 ssd1 exercise5 以前做的 希望有帮助
💻 JAVA
字号:
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() {

		// Student:Please complete this method.

		return age;
	}


	/**
	 * Get the energy of the catfish
	 * 
	 * @return current energy level of the catfish
	 */
	public int getEnergy() {

		// Student:Please complete this method.

		return energy;
	}
	

	/**
	 * Die: The Catfish dies.
	 */
	public void die() {

		// Student:Please complete this method.	
	
		// Set the deadOrAlive attribute to indicate that the catfish is dead.
		deadOrAlive = 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.

		
		// 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;
		}

		// 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);


		// Similarly, assign a random column location for the catfish
		column = simulation.getRand().nextInt(10);
	}

	

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

		// Student:Please complete this method.

		// If there is no energy left, send a "die" message to this catfish
		if (energy <= 0) {
			die();
			return;
		}

		// Increment the age of the Catfish by 1
		age = age + 1;

		// Try to swim by sending a "swimIfPossible" message
		swimIfPossible();
	}

}

⌨️ 快捷键说明

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