Question

JAVA PROGRAMMING LANGUAGE.............................. Part1: Create a class Herbivore and a class Carnivore. Each herbivore can eat...

JAVA PROGRAMMING LANGUAGE..............................

Part1: 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.

Part2: 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.

output of part1:

.   .   .   .   &  
*   .   .   .   .  
.   .   .   .   .  
&   .   .   .   @  
.   *   .   @   .  
-------------------------------------
.   .   .   .   &  
*   .   .   .   .  
.   .   .   .   .  
&   .   @   .   @  
.   *   .   .   .  
-------------------------------------
.   .   .   .   &  
*   .   .   .   .  
.   @   .   @   .  
.   .   &   .   .  
.   *   .   .   .  
-------------------------------------
.   .   .   .   &  
*   *   @   .   .  
.   .   .   @   .  
.   .   &   .   .  
.   *   .   .   .  
-------------------------------------
.   .   &   @   .  
*   *   .   .   @  
.   &   .   .   .  
.   .   .   .   *  
.   *   .   .   .  
-------------------------------------
.   .   &   @   .  
*   *   *   .   .  
.   &   .   .   .  
@   .   *   .   *  
.   .   .   .   .  
-------------------------------------
*   .   .   @   .  
*   *   *   &   .  
.   *   .   .   .  
.   .   @   .   *  
.   &   .   .   .  
-------------------------------------
*   .   .   @   .  
*   *   *   &   .  
.   *   .   .   .  
@   .   &   .   *  
.   .   .   .   .  
-------------------------------------

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code //MyOrganism.java //Include the needed package. package myorganism; //Abstract Class MyOrganism public abstract class Myif (myorganisms > MAXORGANISMS) isalive = false; if (myherbivo res >-MAXHERBIVORES) isalive -false; //Method toString) public//Method myupdate). public void myupdate (MyOrganism[] myneighbours) super.myupdate (myneighbours); int mypreys-0; for (int i//MySimulation.java //Include the needed package. package myorganism; //class MySimulation. public class MySimulation //Declaif (myrow!- idx II mycol!-idxl) if 《 (myrow>=0 && myrow<myg rid.length) && (mycol>-0 &&mycol<mygrid[myrow].length)) myneighbofor (int myrow-0 myrow<mygrid.length; myrow++) for (int mycolumn-0; mycolumn<mygrid[myrow].length; mycolumn++) if (mygrid[myr//MainO. public static void main (String[] args) MySimulation simul -new MySimulation (5, 5) System.out.println (simul); simu
Executable Code

//MyOrganism.java
//Include the needed package.
package myorganism;

//Abstract Class MyOrganism.
public abstract class MyOrganism
{
   
    //Declare variable.
    protected boolean isalive;
   
    //Constructor.
    public MyOrganism()
    {
        isalive = true;
    }
   
    //Boolean Method isAlive().
    public boolean isAlive()
    {
        return isalive;
    }
   
    //Method myupdate().
    public abstract void myupdate(MyOrganism[] myneighbours);
}

//MyPlant.java
//Include the needed package.
package myorganism;

//Class MyPlant.
public class MyPlant extends MyOrganism
{

    //Declare variables.
    public static final int MAXORGANISMS = 5;
    public static final int MAXHERBIVORES = 2;
   
    //Method myupdate().
    public void myupdate(MyOrganism[] myneighbours)
    {

        //Declare variables.
        int myorganisms = 0;
        int myherbivores = 0;
        for (int idx=0; idx<myneighbours.length; idx++)
            if (myneighbours[idx] != null)
            {
                myorganisms++;
                if (myneighbours[idx] instanceof MyHerbivore)
                {
                    myherbivores++;
                }
            }
            if (myorganisms > MAXORGANISMS)
            isalive = false;

        if (myherbivores >= MAXHERBIVORES)
            isalive = false;
    }
   
    //Method toString().
    public String toString()
    {
        return "*";
    }
}

//MyAnimal.java
//Include the needed package.
package myorganism;

//Class MyAnimal.
public abstract class MyAnimal extends MyOrganism
{
   
    //Declare variable.
    private int myage;
   
    //Constructor.
    public MyAnimal()
    {
        myage = 0;
    }
   
    //Method getAge().
    public int getAge()
    {
        return myage;
    }
   
    //Method myupdate().
    public void myupdate(MyOrganism[] myneighbors)
    {
        myage++;
    }
}

//MyCarnivore.java
//Include the needed package.
package myorganism;

//Class MyCarnivore.
public class MyCarnivore extends MyAnimal
{

    //Declare variables.
    public static final int MYLIFETIME = 15;
    public static final int MAXFAST = 4;
    private int myfast = 0;
   
    //Method myupdate().
    public void myupdate(MyOrganism[] myneighbours)
    {
        super.myupdate(myneighbours);
        int mypreys = 0;
        for (int idx=0; idx<myneighbours.length; idx++)
            if (myneighbours[idx] != null && myneighbours[idx] instanceof MyAnimal)
                mypreys++;
        if (mypreys > 0)
            myfast = 0;
        else
            myfast++;
        if (myfast >= MAXFAST)
            isalive = false;
        if (getAge() >= MYLIFETIME)
            isalive = false;
    }
   
    //Method toString().
    public String toString()
    {
        return "@";
    }
}

//MyHerbivore.java
//Include the needed package.
package myorganism;

//Class MyHerbivore.
public class MyHerbivore extends MyAnimal
{
   
    //Declare variables.
    public static final int MIN_NO_OF_PLANTS = 1;
    public static final int MYLIFETIME = 20;
   
    //Method myupdate().
    public void myupdate(MyOrganism[] myneighbours)
    {
        super.myupdate(myneighbours);
        int myplants = 0;
        for (int idx=0; idx<myneighbours.length; idx++)
            if (myneighbours[idx] != null && myneighbours[idx] instanceof MyPlant)
                myplants++;
        if (myplants < MIN_NO_OF_PLANTS)
            isalive = false;
        if (getAge() >= MYLIFETIME)
            isalive = false;
    }
   
    //Method toString().
    public String toString()
    {
        return "&";
    }
}

//MySimulation.java
//Include the needed package.
package myorganism;

//Class MySimulation.
public class MySimulation
{

    //Declare variables.
    private MyOrganism[][] mygrid;
    private int mygen;
   
    //Method MySimulation().
    public MySimulation(int myrow, int mycolumn)
    {
        mygrid = new MyOrganism[myrow][mycolumn];
        myinitialization();
    }
    public void myinitialization()
    {
        mygen = 0;
        for (int myrow=0; myrow<mygrid.length; myrow++)
        {
            for (int mycolumn=0; mycolumn<mygrid[myrow].length; mycolumn++)
            {
                mygrid[myrow][mycolumn] = createOrganisms();
            }
        }
    }
   
    //Method createOrganisms().
    private MyOrganism createOrganisms()
    {
        MyOrganism results = null;
        double number = Math.random();
        if (number < 0.2)
            results = new MyPlant();
        else if (number < 0.30)
            results = new MyHerbivore();
        else if (number < 0.35)
            results = new MyCarnivore();
        return results;
    }
   
    //Method returnNeighbours().
    private MyOrganism[] returnNeighbours(int idx, int idx1)
    {
        MyOrganism[] myneighbors = new MyOrganism[8];
        int position = 0;
        for (int myrow=idx-1; myrow<=idx+1; myrow++)
        {
            for (int mycol=idx1-1; mycol<=idx1+1; mycol++)
            {
                if (myrow!= idx || mycol!=idx1)
                {
                    if ((myrow>=0 && myrow<mygrid.length) && (mycol>=0 && mycol<mygrid[myrow].length))
                        myneighbors[position] = mygrid[myrow][mycol];
                    position++;
                }
            }
        }
        return myneighbors;
    }
   
    //Method mysimulate().
    public void mysimulate()
    {
        for (int myrow=0; myrow<mygrid.length; myrow++)
        {
            for (int mycol=0; mycol<mygrid[myrow].length; mycol++)
            {
                if (mygrid[myrow][mycol] != null)
                {
                    MyOrganism[] myneighbors = returnNeighbours(myrow,mycol);
                    mygrid[myrow][mycol].myupdate(myneighbors);
                }
            }
        }
        for (int myrow=0; myrow<mygrid.length; myrow++)
        {
            for (int mycol=0; mycol<mygrid[myrow].length; mycol++)
            {
                if (mygrid[myrow][mycol] == null)
                {
                    mygrid[myrow][mycol] = createOrganisms();
                }
            }
        }
        for (int myrow=0; myrow<mygrid.length; myrow++)
        {
            for (int mycol=0; mycol<mygrid[myrow].length; mycol++)
            {
                if (mygrid[myrow][mycol] != null && (! mygrid[myrow][mycol].isAlive()))
                {
                    mygrid[myrow][mycol] = null;
                }
            }
        }
        mygen++;
    }
   
    //Method toString().
    public String toString()
    {
   
        String nl = System.getProperty("line.separator");
        String results = "Generation = " + mygen + nl;
        for (int myrow=0; myrow<mygrid.length; myrow++)
        {
            for (int mycolumn=0; mycolumn<mygrid[myrow].length; mycolumn++)
            {
                if (mygrid[myrow][mycolumn] != null)
                {
                    results += mygrid[myrow][mycolumn];
                }
                else
                {
                    results += ".";
                }
            }
            results += nl;
        }
        return results;
    }
   
    //Method mytabulate().
    public void mytabulate()
    {

        //Declare variables.
        int myorganisms = 0;
        int myplants = 0;
        int myanimals = 0;
        int myherbivores = 0;
        int mycarnivores = 0;

        for (int myrow=0; myrow<mygrid.length; myrow++)
        {
            for (int mycolumn=0; mycolumn<mygrid[myrow].length; mycolumn++)
            {

                if (mygrid[myrow][mycolumn] != null)
                {
                    MyOrganism o = mygrid[myrow][mycolumn];

                    myorganisms++;
                   
                    if (o instanceof MyPlant)
                    {
                        myplants++;
                    }
                    if (o instanceof MyAnimal)
                    {
                        myanimals++;
                    }
                    if (o instanceof MyHerbivore)
                    {
                        myherbivores++;
                    }
                    if (o instanceof MyCarnivore)
                    {
                        mycarnivores++;
                    }
                }
            }
        }
    }
   
    //Main().
    public static void main(String[] args)
    {
        MySimulation simul = new MySimulation(5,5);
        System.out.println(simul);
        simul.mytabulate();
        for (int idx=0; idx<10; idx++)
        {
            simul.mysimulate();
            System.out.println(simul);
            simul.mytabulate();
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
JAVA PROGRAMMING LANGUAGE.............................. Part1: Create a class Herbivore and a class Carnivore. Each herbivore can eat...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • mammals.txt squirrel,2,0.5,Herbivore,Sciurus griseus kangaroo,7,90,Herbivore,Macropus rufus gorilla,20,150,Herbivore,Gorilla beringei sea lion,12,250,Carnivore,Zalophus californianus guinea pig,4,3,Herbivore,Cavia porcellus whale (grey),70,36000,Herbivor

    mammals.txt squirrel,2,0.5,Herbivore,Sciurus griseus kangaroo,7,90,Herbivore,Macropus rufus gorilla,20,150,Herbivore,Gorilla beringei sea lion,12,250,Carnivore,Zalophus californianus guinea pig,4,3,Herbivore,Cavia porcellus whale (grey),70,36000,Herbivore,Eschrichtius robustus cat,12,4,Carnivore,Felis catus sheep,12,180,Herbivore,Ovis aries giraffe,10,1000,Herbivore,Giraffa mouse,3,0.04,Omnivore,Mus musculus puma,12,50,Carnivore,Puma concolor orca,50,4000,Carnivore,Orcinus orca beaver,5,20,Herbivore,Castor canadensis fox,7,6,Omnivore,Vulpes vulpes donkey,12,300,Herbivore,Equus asinus buffalo,15,650,Herbivore,Bison bison cow,15,700,Herbivore,Bos taurus baboon,20,40,Omnivore,Papio ursinus horse,20,500,Herbivore,Equus ferus caballus moose,12,450,Herbivore,Alces alces goat,9,90,Herbivore,Capra aegagrus hircus chimpanzee,20,45,Omnivore,Pan troglodytes bear (grizzly),25,270,Omnivore,Ursus arctos leopard,12,55,Carnivore,Panthera pardus camel,40,500,Herbivore,Camelus dromedarius rhinoceros,40,2000,Herbivore,Ceratotherium simum simum hippopotamus,25,1400,Herbivore,Hippopotamus amphibius opposum,1,4,Omnivore,Didelphis virginiana howler monkey,15,7,Herbivore,Alouatta palliata deer,8,55,Herbivore,Odocoileus virginianus elephant,65,4500,Herbivore,Elephas maximus dog,12,25,Carnivore,Canis lupus familiaris bat,15,1,Omnivore,Pteropus vampyrus wolf,5,40,Carnivore,Canis lupus zebra,15,300,Herbivore,Equus quagga elk,15,280,Herbivore,Cervus...

  • Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and...

    Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and a Zoo class that holds all the animals. You should create a general Animalclass where all other classes are derived from (except for the Zoo class). You should then create general classes such as Mammal, Reptile, and whatever else you choose based off of the Animalclass. For...

  • Organism Consumer (1°or 2°) or Producer Herbivore, Omnivore, Carnivore, or Autotroph Bear Bison Camel Cat Cow...

    Organism Consumer (1°or 2°) or Producer Herbivore, Omnivore, Carnivore, or Autotroph Bear Bison Camel Cat Cow Donkey Elephant Goat Hippo Horse Lion Rhino Tiger Great Goldfish Low Shrubs Verdigrass Dry Goatweed Plains Bushtail Grass Indigo Waterweed 1. How might the extinction of the dry goatweed cause a decline in the tiger population? 2. A pesticide is slowly leaked into the plains region. The plains bushgrass survives but contains trace amounts of the pesticide within it. Scientists find that the pesticide...

  • Please help me with the following question. This is for Java programming. In this assignment you...

    Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and a Zoo class that holds all the animals. You should create a general Animalclass where all other classes are derived from (except for the Zoo class). You should then create general classes such as Mammal, Reptile, and whatever else you choose based off of the Animalclass. For...

  • Needs Help with Java programming language For this assignment, you need to write a simulation program...

    Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...

  • Assembly Language Using CodeWarrior to create a new project that uses assembly language. Write an assembly...

    Assembly Language Using CodeWarrior to create a new project that uses assembly language. Write an assembly program that turns on the red LED at the beginning. It switches to off 2 seconds later and switches back to on after three more seconds. (That is, it stays on for 2 seconds and off for 3 seconds.) This switching process lasts forever. Use a loop (or nested loops) for each of the 2/3-second delay where the loop body of the inner-most loop...

  • help with java OOP, here is the started code: package P2; public class Farm {   ...

    help with java OOP, here is the started code: package P2; public class Farm {    private double availableFood;    private Animal[] animals;    public Farm() {        setAvailableFood(1000);        animals = new Animal[4];        animals[0] = new Chicken();        animals[1] = new Cow();        animals[2] = new Llama();        animals[3] = new Llama();    }    public void makeNoise(){           // all animals make their sound (Moo, Cluck, etc)        for(Animal...

  • Use HCS12 assembly language only - not C code Using CodeWarrior to create a new project...

    Use HCS12 assembly language only - not C code Using CodeWarrior to create a new project that uses assembly language. Write an assembly program that turns on the red LED at the beginning. It switches to off 2 seconds later and switches back to on after three more seconds. (That is, it stays on for 2 seconds and off for 3 seconds.) This switching process lasts forever. Use a loop (or nested loops) for each of the 2/3-second delay where...

  • part1 It should have no inputs and four input wires consolidated by a splitter into one...

    part1 It should have no inputs and four input wires consolidated by a splitter into one 4-bit output pin. The outputs should be thought of as four digits of a binary number. Your circuit should initially output the number 0 (four 0s in a row). Your circuit should cycle through the first six nonnegative multiples of three (0, 3, 6, ... 12, 15, 0, 3, ...). Each clock cycle, your circuit should output the next number in this sequence. part...

  • IN C# Objectives: Create an application that uses a dictionary collection to store information ...

    IN C# Objectives: Create an application that uses a dictionary collection to store information about an object. Understanding of abstract classes and how to use them Utilize override with an abstract class Understanding of Interfaces and how to use them Implement an Interface to create a “contract” between classes. Compare and contrast inheritance and interfaces. Instructions: Interface: Create an interface called ITrainable which contains the following: Dictionary<string, string> Behaviors{ get; set; } string Perform(String signal); string Train(String signal, string behavior);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT