There are three types of characters:Animals, Robots and Plants. Plant is simply located in its cell. Animals & Robots can move. Robot can move only one cell each step, by random direction. It cannot kill anyone, but either animal that catches the robot, will turn it into one of its kind (e.g: Mosquito that catches robot will turn it to Mosquito). There are only three types of animals: Mosquito, Frog and Flamingo. Flamingos would like to eat all Frogs. Frogs would like to eat all Mosquitos. And Mosquitos would like to sting and kill all Flamingos. The purpose of each population is to become the only population left in the world. When animal chooses to move, firstly, it looks for the animal it eats, with in its radius of action, and then moves and eats it. If there is no such animal around, it will look for a robot to reach it. If there is no robot, it will choose a random location with in its radius of action. We will specify each animal's movement in the API of animals classes.
You should write the following classes:
World, WorldInit, Character, Robot, Plant, Animal, Mosquito, Frog, Flamingo and the Moveable interface.
Your classes should follow the following hierarchy:
You can download here the trial version of the World class that you need to complete. Read carefully this file. It defines the constructor of World as private, so the only way to reach World object from other class, is by static getInstance() method defined there. This design allows creating just one instance of the world that can always be reached from any other class by World.getInstance() call. Such class is called Singleton, and it is very useful and popular design pattern.
World class should hold all the Characters living in the world. You can implement it by any common data structure (such as ArrayList, Hashtable or Vector [see Java API] ). You need to implement the following methods of class World:
|public boolean add (Character c)||Add Character to the world if it is possible.
Should check that the cell corresponding to the Character's coordinates exists and available. Return true for successful adding and false otherwise. To each added character a serial id number should be given (0 for the first one, 1 for a second, etc.).
|public Character getAt(int x, int y)||Return Character located at the given position (x,y). And null if nobody is there.|
|public int numberOfCharacters()||Return the number of current existing Characters in the world|
|public Character getById(int id)||Returns the Character with the given id. If it doesn't exist (there is no such id or it is not alive) - return null.|
|public String toString()||Returns the world's current string representation of all the existing characters.|
The world is initialized by class named WorldInit. This class contains one world property:
WorldInit has one method only: init() which is called
when the World is created and adds all the Characters to the world. Click
here for the example of this class. You should
change it with your own specific characters.
|protected int x, y||The coordinates of this character in the world. x,y should be between the numbers 0 till WorldInit.DIM - 1 (as mentioned above).|
|public Character(int x, int y)||Constructor that sets the character's coordinates.|
|public int getX()||returns x coordinate of the Character|
|public int getY()||returns y coordinate of the Character|
|public String toString()||returns the string representation of the Character (its type,coordinates and id).|
|public Plant(int x, int y)||Constructor that sets the Plant's coordinates.|
|public boolean move ()||moves the Character - sets the Character's new coordinates. Returns false if Character didn't succeed to move.|
|public Robot (int x, int y)||Constructor that sets the Robot's coordinates.|
|public boolean move()||Robot moves horizontal or vertical only, by random choice of direction and distance. May move to free cells only. Returns false if didn't succeed to move.|
|public Animal(int x, int y)||Constructor that sets the Plant's coordinates.|
|public abstract boolean move()||no implementaion*|
* - Although you can implement a general move() method for all animals (think how...)
|public Mosquito(int x, int y)||Constructor that sets the Mosquito's coordinates.|
|public boolean move()||Mosquito: can move only to a neighbour cell (4 options, no need to consider diagonals): empty or with Flamingo or Robot. Prefering a Flamingo. If there is no Flamingo around, it will look for a Robot. If there is no Robot, it will choose a random neighbour free location. Returns false, if didn't succeed to move.|
|public Frog(int x, int y)||Constructor that sets the Frog's coordinates.|
|public boolean move()||Frog: can move only to an empty or with Mosquito or Robot cell, that may be located in any of 3 cells radius from it. Will prefer one with Mosquito, if there is no Mosquito around, it will look for a Robot. If there is no Robot, it will choose a free random location with in this area. Returns false if didn't succeed to move. No need to consider diagonal moves.|
|public Flamingo(int x, int y)||Constructor that sets the Flamingo's coordinates.|
|public boolean move()||Flamingo: can move only to an empty or with Frog or Robot cell, that may be located in any of 10 cells radius from it. Will prefer one with Frog, if there is no Frog around, it will look for a Robot. If there is no Robot, it will choose a free random location with in this area. Returns false if didn't succeed to move. No need to consider diagonal moves.|
Once you have implemented all classes and you are sure that your implementation is correct, you may try our applet. The applet sequently calls move() method of each moveable Character in the world and demonstrates its motion on the screen with the gif that represents it. You will need also a world.html file, which includes this applet. Additionally, you will need our gifs and should be located in the same directory with the world.html file. These gifs are graphic representation for each of Characters.
Here you can download all needed files:
We recommend to use
appletviewer world.html command from the
DOS prompt to browse your applet during your work.
All your sources files should be belong to package world.
This assignment is worth a maximum of 100 points.
You will need to demonstrate your assignment to a grader to receive a grade.
In addition, you will need to show the grader the following:
The above should be packaged in a jar file called a1.jar. For creating
a jar file you can run the following command in from DOS prompt in your
jar cvf a1.jar * . The jar helper contains additional information on this