Create a class Herbivore and a class Carnivore Each herbivor
Create a class Herbivore and a class Carnivore.
Each herbivore can eat a Plant to survive and each carnivore eats herbivore to survive. Herbivores and carnivores can move to a location around themselves. The speed of movement of the carnivore is faster than a herbivore. Animals and plants can leave for certain amount of time and after that they die. Animals have level of energy and if the energy of an animal is less than a specific amount it will die. Animals can get birth to other animals if they are in a certain range of ages and they have enough energy. If the level of energy of an animal is higher than a certain amount it will not eat anything. Plants grow in random locations at certain times ( ex. a random number between 3 to 5 clocks).
Create a simulation for above scenario. Inside your simulation you should have a concept of the clock, at each clock certain events happen.
You have to use all the object-oriented concepts that you learned so far, encapsulation and hierarchy are necessary.
Run your simulation in a loop with a certain number of clocks. At each clock print the earth in a good organized format. Use these characters:
Carnivore: @
Herbivore: &
Plant: *
Free Space: .
Put one space \' \' between each two characters for readability.
I have attached an example of the simulation that uses a grid of 5 x 5 locations. The simulation runs for 4 cycles. You can see all the steps in the that attachment:
Herbivore moves every two cycles, Carnivore moves every cycle. Plant grows ever three cycles. The initial energy of all the animals is 3. Carnivore gets birth after age 4 cycle (It ate a herbivore and got 5 energy points so it has enough energy to get birth to a new animal), the energy of the carnivore drops by three and the new-born animal gets three initial energy. Herbivore starves at the last cycle.
Your simulation should be larger and has more iterations (More clocks) and remember to put the right parameters to keep the ecosystems alive. Its a very good idea to start with a small simulation and extend it.
Make your application more object oriented (Using the topics that you learned in the class). Your agents should become smarter and they should have a small memory to know where they have visited so far (An array of a fixed size). Also they should have randomized parameters this time (Two herbivore do not get the same energy by eating and each eating may cause different energy increase depending on the age of hunt (Or plant). Also you need to get ready to make your environment continues.
Solution
import java.util.*;
abstract class animal{
void eat();
void rebirth();
void plant_growth();
}
class herbivore extends animal{
int plant;
int speed=0;
int energy=3;
int herbcount =0;
int cycle;
void plant_growth()
{
if(cycle>3 || cycle < 5)
{
plant++;
system.out.println(\"***\");
}
}
}
void eat(int plant){
plant = this.plant;
}
void rebitrh(){
if(cycle<4)
hercount++;
system.out.println(\"&\");
}
class carnivore extends animal {
int speed=2;
int energy=3;
int cycle;
int carncount=0;
void rebirth(){
if(rebirth > 4 && cycle > 4)
{
system.out.println(carncount++);
system.out.println(\"@\");}
void eat()
{
herbcount--;
cycle =5;
rebirth=5;
void rebirth();
}
public static void main(string[] args)
{
herbivore obj= new herbivore();
carnivore obj1=new carnivore();
obj.eat();
obj.rebirth();
obj.plant_growth();
obj1.eat();
obj1.rebirth();
}


