Write a class Hyena that extends the Critter class, along with its movement and
ID: 3909998 • Letter: W
Question
Write a class Hyena that extends the Critter class, along with its movement and eating behavior. All unspecified aspects of Hyena use the default behavior. Write the complete class with any fields, constructors, etc. necessary.
A Hyena object moves in a rectangular pattern looking for food, walking NORTH, then EAST, then SOUTH, then WEST. Each time the Hyena walks an entire rectangle or eats food, it starts the rectangle pattern over again but with a rectangle 1 step wider than before. The general pattern is as follows, if the Hyena doesn't find any food:
N, E, S, W, N, E, E, S, W, W, N, E, E, E, S, W, W, W, N, E, E, E, E, S, W, W, W, W, ...
If the Hyena encounters food at any point during its movement pattern, it eats the food and starts the pattern over, lengthening the rectangular pattern by 1 in the process. For example:
N, E, S, W, N, E, E (eats food), N, E, E, E, S, W, W (eats food), N, E, E, E, E, S, W, W, W, W, N, E, E, E, E, E, S (eats food), N, E, E, E, E, E, E, S, W, W, ...
Explanation / Answer
Please find the java class below with the specifications :
--------
import info.gridworld.actor.Critter;
import info.gridworld.grid.Location;
/**
* @author
*
*/
public class Hyena extends Critter {
private int hyenaMoves;
private int rectangleWidth;
/**
*
*/
public Hyena() {
hyenaMoves = 0;
rectangleWidth = 1;
}
/**
* @return
*/
public boolean eat() {
hyenaMoves = 0;
rectangleWidth= rectangleWidth + 1;
return true;
}
/**
* @return
*/
public int getMove() {
hyenaMoves= hyenaMoves + 1;
if (hyenaMoves > 2 * rectangleWidth + 2) {
hyenaMoves = 1;
rectangleWidth= rectangleWidth + 1;
}
if (hyenaMoves == 1) {
return Location.NORTH;
} else if (hyenaMoves <= rectangleWidth + 1) {
return Location.EAST;
} else if (hyenaMoves == rectangleWidth + 2) {
return Location.SOUTH;
} else {
return Location.WEST;
}
}
}
-------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.