You have been given a simulator for animals wandering around an environment. You need to define some animals for that simulation. Different kinds of animals will move in different ways and you are defining those differences. As the simulation runs, animals can die by ending up on the same location, in which case the simulator randomly selects one animal to survive the collision.

Setup

You have been given a lot of files to accompany this project. You can basically ignore all of them except Critter.java and Direction.java. The simulator will be responsible for moving the animals around, you just need to tell it what direction the animals want to go next.

Each of your classes will extend the Critter class. Critters move around in a world of finite size, but the word is toroidal (going off the end to the right brings you back to the left and vice versa; going off the end to the top brings you back to the bottom and vice versa).

There is also an interface in CritterInfo.java. An instance of this will be passed into the getMove() method, however it is completely optional (you may make use of it in your Wolf class if you wish, other animals do not need to use this parameter at all). Interfaces are discussed in detail in chapter 9 of the textbook and chapter 8 of the lab manual, but again, you do not need to know interfaces to complete this assignment.

Step 1: Create Your Animal Classes

You will be implementing five classes: Bird, Frog, Mouse, Turtle, and Wolf. These should each live in their own files which should not be part of a package. Create these class files and ensure they each extend the Critter class. Note: You are allowed to include a constructor for your classes if you want, although it must be a zero-argument constructors (one that takes no arguments).

At this point your code should compile and you can run the simulation with the command:

> java CritterMain

The simulation won't be very interesting; it will just appear as a series of dots and spaces, but that is expected.

Step 2: Create Visual Representations

Override the getChar() method in each animal's class to return first character of the animal's species. For example: B for Bird, F for Frog, etc. You can recompile your code at this point and now you should see letters where the spaces had been.

Step 3: Create Movement

Animals should move around in the simulator and when they bump into each other (i.e. they occupy the same place) one will be deleted randomly. The removal of animals is done automatically by the simulator, but your animals each need to move in a certain pattern, described in the table below:

Class Movement
Bird Randomly selects one of the four directions (north, south, east, or west) each time.
Frog Picks a random direction, then moves three times in that direction before choosing a new random direction.
Mouse Moves west one step then north one step and repeats this west-north zig-zag pattern.
Turtle Walks south five steps, then wast five steps, the north five steps, then east five steps and repeats. This forms a clockwise box pattern.
Wolf You define this! Make it interesting for full credit!

You will need to override the getMove() method for each animal you've made to make these patterns. Note that each call to getMove() will make a single move, so animals that do things over a period of time will need to keep track of what they are doing with some internal state.

For example, the first time I call getMove() for a mouse, it will return west and the next call to getMove() will return north, and the next west, etc. These directions are defined in the Direction.java file as an enum. Enums are described in chapter 9 of the lab manual and Appendix C of the textbook, but you do not need to understand enums in detail to do this assignment, just a couple of details:

Referring to an enum is done by: EnumerationName.FIELD. For example, Direction.WEST or Direction.NORTH. These can be compared to each other with == and printed. The following code shows all you need to understand enums for this assignment:

Direction d = Direction.WEST;
if(d == Direction.WEST)
System.out.println(d); //prints WEST

Additional items:

  • You'll need to push the start button to see the movement in the simulator. Step will make all critters take one move.
  • The Critter class has a currentDirection instance variable and a getCurrentDirection() method which need to work in each of your animal classes. So when getMove() is called, currentDirection should be updated.
  • There is a Direction.NONE direction for non-movement if you want to use this in your Wolf class.
  • For the random moves, each possible choice must be equally likely. You may use either a Random object or the Math.random() method to obtain pseudorandom values.

Testing

You need to test this on your own! Here's some ideas:

  • You can make your own class with a main method that creates creatures and calls their methods.
  • You can use the step function of the simulator to see what the animals do.
  • You can comment out lines in CritterMain.java to remove certain animal types from the simulator.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.