Question
Write a set of classes that define the behavior of certain animals. They can be used in a simulation of a world with many animals
Write a set of classes that define the behavior of certain animals. They can be used in a simulation of a world with many animals moving around in it. Different kinds of animals will move in different ways (you are defining those differences). As the simulation runs, animals can die when two or more of them end up in the same location, in which case the simulator randomly selects one animal to survive the collision. See your course web site or www.buildingjavaprograms.com for supporting files to run such a simulation.
The following is an example set of animals and their respective behavior:
Here is inside of animalmain java:
// Stuart Reges // 9/6/00 (original code with "Building Java Programs" textbook // Changed 2019,21 by W.P. Iverson, for CS211 project at Bellevue College //
// AnimalMain provides method main for a simple simulation program.
public class AnimalMain { public static void main(String[] args21) { // start work with two animals Animal[] tester = {new Bird(), new Mouse()}; // once done, all seven animals should work //Animal[] tester = {new Bird(),new Mouse(),new Frog(),new Rabbit(),new Snake(),new Turtle(),new Wolf()}; for (int i=0; i import java.awt.Color; // Stuart Reges // 12/03/04 // // Changed 2019 by W.P. Iverson, for CS211 project at Bellevue College // // The Animal interface defines the methods necessary for an animal // to participate in the animal scramble simulation. It must return a // character when getChar is called that is used for displaying it on // the screen. The getMove method must return a legal move (one of // the constants NORTH, SOUTH, EAST, WEST, CENTER). public interface Animal { // Each direction resolves to int // based on layout of keypad, 8 at top, 2 at bottom, etc... public static final int NORTH = 8; public static final int SOUTH = 2; public static final int EAST = 6; public static final int WEST = 4; public static final int HOLD = 0; // methods required by interface public String toString(); public int getMove(); public Color getColor(); } Here is inside of bird.java: import java.awt.Color; //Stuart Reges //9/6/00 (original code with "Building Java Programs" textbook //Changed 2019 by W.P. Iverson, for CS211 project at Bellevue College public class Bird implements Animal { // Constructor public Bird() { super(); // nothing to do here, so calling obvious (redundant) // Iverson CS211, always requests a zero parameter constructor } // over-rides Object, and satisfies interface public String toString() { return "B"; } // required by interface, not even using AnimalInfo in code below public int getMove() { //AnimalInfo info) { double r = Math.random(); if (r // millions of options here public Color getColor() { return new Color(0,0,255); } } Here is inside of mouse.java: import java.awt.Color; //Stuart Reges //9/6/00 (original code with "Building Java Programs" textbook //Changed 2019 by W.P. Iverson, for CS211 project at Bellevue College public class Mouse implements Animal { private boolean myZig; // determines which way to go for each mouse public Mouse() { myZig = true; } // required by interface public String toString() { return "M"; } // toggle boolean, so alternates up and left public int getMove() { //AnimalInfo info) { myZig = !myZig; if (myZig) return NORTH; // up else return WEST; // left } // using basic grey public Color getColor() { return Color.GRAY; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started