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 should be 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, Certain time means for example a random number between 3 to 5 clocks (Every 3 or 4 or 5 clocks).

Create a simulation for above scenario. Inside your simulation you should have a concept of the clock, at each clock certain things happens.

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 gird of 5 x 5 locations. I ran the simulation for 4 cycles. You can see all the steps in the that attachement:

Herbivore moves every two cycles, Carnivore moves every cycle. Plant grows ever three cycles. The initial energy of all the animals is 3. Carnivore get birth after age 4 cycle (It ate an herbivore and got 5 energy points so it has enough energy to get birth to new animal), the energy of the carnivor 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 remmember to put the right parameteres to maintain the ecosystems alive. Its a very good idea to start with a small simulation and extend it.

Solution

public abstract class Animal {

public abstract void accept(AnimalVisitor visitor);

}

public interface AnimalVisitor {

public void visit(Omnivore omnivore);

public void visit(Herbivore herbivore);

public void visit(Carnivore carnivore);

}

public class Carnivore extends Animal {

@Override

public void accept(AnimalVisitor visitor) {

    visitor.visit(this);

}

public void eat(Meat meat) {

    System.out.println(\"Carnivore eating Meat...\");

}

}

public class Herbivore extends Animal {

@Override

public void accept(AnimalVisitor visitor) {

    visitor.visit(this);

}

public void eat(Plant plant) {

    System.out.println(\"Herbivore eating Plant...\");

}

}

public class Omnivore extends Animal {

@Override

public void accept(AnimalVisitor visitor) {

    visitor.visit(this);

}

public void eat(Food food) {

    System.out.println(\"Omnivore eating \" + food.getClass().getSimpleName() + \"...\");

}

}

public abstract class Food implements AnimalVisitor {

public void visit(Omnivore omnivore) {

    omnivore.eat(this);

}

}

public class Meat extends Food {

@Override

public void visit(Carnivore carnivore) {

    carnivore.eat(this);

}

   @Override

public void visit(Herbivore herbivore) {

   

}

}

public class Plant extends Food {

   @Override

public void visit(Carnivore carnivore) {

}

   @Override

public void visit(Herbivore herbivore) {

    herbivore.eat(this);

}

}

public class Zoo {

private List<Animal> animals = new ArrayList<Animal>();

public void addAnimal(Animal animal) {

   animals.add(animal);

}

public void receiveFood(Food food) {

    for (Animal animal : animals) {

      animal.accept(food);

    }

}

public static void main(String[] args) {

    Zoo zoo = new Zoo();

    zoo.addAnimal(new Herbivore());

    zoo.addAnimal(new Carnivore());

    zoo.addAnimal(new Omnivore());

    zoo.receiveFood(new Plant());

    zoo.receiveFood(new Meat());

}

}

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 carnivore
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 carnivore
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 carnivore
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 carnivore

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site