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

📄 animal.java

📁 卡耐基梅隆大学 SSD课程体系 ssd1 exesice8 答案
💻 JAVA
字号:
/*!Begin Snippet:Animal*/
/*
 * Created on Jul 6, 2003
 *
 */

// Concept example: extends. 
// 		Each Animal is a LivingBeing. 
// 		Animal should inherit all attributes and behavior of LivingBeing.
/**
 * Animals can move.
 * 
 * @author iCarnegie av
 *
 */
public class Animal extends LivingBeing {

	// 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 animal is facing.
	 */
	protected static final String RIGHT = "right";

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

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

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

	/**
	 * Which direction am I facing.
	 */
	private String direction; 


	/**
	 * Create an animal at a given location with a 
	 * given energy and store the simulation to which the animal belongs.
	 * Cap row and column within lake boundary.
	 *  
	 * @param initialRow - the row location of animal
	 * @param initialColumn - the column locaiton of animal
	 * @param initialSimulation - the simulation to which this animal belongs
	 * @param initialName - name of the animal
	 * @param initialMinEnergy - minimum energy to survive
	 * @param initialMaxEnergy - max energy the animal can carry 
	 */
	public Animal(
		int initialRow,
		int initialColumn,
		Simulation initialSimulation,
		String initialName,
		int minEnergy,
		int maxEnergy) {

		super(
			initialRow,
			initialColumn,
			initialSimulation,
			initialName,
			minEnergy,
			maxEnergy);
		direction = RIGHT; // Start by facing east.
	}

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

	/** 
	 * Is the animal hungry?
	 * 
	 * @return True, if hungry. False, otherwise.
	 */
	protected boolean isHungry() {
		
		// Hungry, if current energy level is less than twice the 
		// amount needed for survival.
		return (getEnergy() < (2 * getMinEnergy()));
	}

	/**
	 * Move the animal 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
	 */
	protected int moveToRow(int newRow) {
		
		if (isDead()) {
			return -1;
		}

		// Keep the new value within lake boundary.
		if (newRow > simulation.getLastRow()) {
			newRow = simulation.getLastRow();
		} else if (newRow < simulation.getFirstRow()) {
			newRow = simulation.getFirstRow();
		}

		// I might face a new direction.
		if (newRow < row) {
			direction = UP;
		} else if (newRow > row) {
			direction = DOWN;
		}
		row = newRow;

		return row;
	}

	/**
	 * Move the animal 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.
	 */
	protected int moveToColumn(int newColumn) {

		if (isDead()) {
			return -1;
		}

		// System.out.println("column = " + column + ", newCOlumn = " + newColumn);
		// System.out.flush();
		// Keep the new value within lake boundary.
		if (newColumn > simulation.getLastColumn()) {
			newColumn = simulation.getLastColumn();
		} else if (newColumn < simulation.getFirstColumn()) {
			newColumn = simulation.getFirstColumn();
		}

		// I might face a new direction.
		if (newColumn < column) {
			direction = LEFT;
		} else if (newColumn > column) {
			direction = RIGHT;
		}

		column = newColumn;

		return column;
	}

}
/*!End Snippet:Animal*/

⌨️ 快捷键说明

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