Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is for a Java Project - I am mentioning the entire project below. Howerver

ID: 3576065 • Letter: T

Question

This is for a Java Project - I am mentioning the entire project below. Howerver I only need help with part 3! if someone can provide me the code then it would really be helpful!

Part 1 -

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.

Part 2 -

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.

Part 3 - I NEED HELP WITH THIS PART!!!

Complete your simulation and put a GUI over that. The GUI shows the location of each animal and has a next button. By pressing the next button 1 Cycle goes. Your GUI should have an option to increase the number of cycles (ex. pressing the next button will iterate 3 cycles and shows the result at the third cycle).

Your application MUST be object oriented and you really have to take advantage of object orientation (remember a code might be object oriented but it may not have any usages)

Part 1 and 2

import java.util.Random;


public class Carnivore extends LivingCreatures {
    private String B;

    private int EnergyLevel=3;
    Carnivore(boolean[][] array){
        super(array);
        this.B="@";

    }
    public String getString(){

        return B;
    }

    public String Born(){
        Random Numbers= new Random();
        int n = Numbers.nextInt(8);
        EnergyLevel-=n;
        return "@";

    }

    public String GainEnergy(){
        Random Numbers= new Random();
        int n = Numbers.nextInt(8);
        EnergyLevel+=n;
        return ".";
    }



    public void print(){


        for(int i = 0;i<array.length;i++){
            for(int j=0;j<array.length;j++) {
                if(array[i][j]==true) {

                    System.out.println("Carnivore - Column visited " + j );
                    System.out.println("Carnivore - Row visited " + i );

                }
            }
        }
    }


}

==============================

import java.util.Random;

public class Herbivore extends LivingCreatures{
    private String A;
    public int EnergyLevel = 3;
    Herbivore(boolean[][]array ){
       super(array);
        this.A = "&";

    }
    public String getString (){

        return A;
    }
    public String born() {
        Random Numbers = new Random();
        int n = Numbers.nextInt(8);
        EnergyLevel -= n;
        return "&";
    }

    public String New(){
        Random Numbers = new Random();
        int n = Numbers.nextInt(8);
        EnergyLevel-=n;
        return "&";
    }


public void GainEnergy(){
    Random Numbers = new Random();
    int n = Numbers.nextInt(8);
    EnergyLevel+=n;

}
public String eat()
{

    return ".";
}

public void print(){
    for (int i = 0; i<array.length; i++){
        for (int j = 0; j<array.length;j++){
            if (array[i][j]== true){
                System.out.println("Herbivore - Column visited " + j);
                System.out.println("Herbivore - Row visited " + i);
            }
        }
    }
}
}


======================


public abstract class LivingCreatures {
    public boolean [][] array;
    LivingCreatures (boolean [][] array){

        this.array = array;
    }

    public boolean [][] getArray(){

        return array;
    }
    public void print(){
        for (int i = 0; i<array.length;i++){
            for (int j=0; j<array.length;j++){
                if (array[i][j]){
                    System.out.println("Column visited " + j);
                    System.out.println("Row visited " + i);

                }
            }
        }
    }
    public void visit(int a, int b) {

        array[a][b] = true;
    }
    }


=================================

import java.util.Random;
import java.util.Scanner;


public class Main {
    public static void main(String [] args){
        Scanner input = new Scanner(System.in);
        System.out.println("The Ecosystem ");

        System.out.println("Input cycles: ");
        int cycles = input.nextInt();

        System.out.println("Input size: ");
        int size =input.nextInt();


        int start =1;
        boolean [][] visited_Herbivore = new boolean[size][size];
        boolean [][] visited_Carnivore = new boolean[size][size];
        boolean [][] visited_Plant = new boolean[size][size];


        Herbivore first = new Herbivore(visited_Herbivore);
        Carnivore second = new Carnivore(visited_Carnivore);
        Plant third = new Plant(visited_Plant);

        String[][] environment= new String[size][size];
        System.out.println("Input animals # : ");
        int population = input.nextInt();

        System.out.println("Herbivore, Carnivore, and Plant are born!");
        Random randomNumbers= new Random();
        int ate=0;
        int A_i,A_j,B_i,B_j,C_i,C_j;
        while(start<=cycles){
            if(start==1) {
                int copies = 0;
                while (copies < population) {
                    A_i = randomNumbers.nextInt(environment.length);
                    A_j = randomNumbers.nextInt(environment.length);
                    environment[A_i][A_j] = first.getString();
                    first.visit(A_i,A_j);
                    B_i = randomNumbers.nextInt(environment.length);
                    B_j = randomNumbers.nextInt(environment.length);
                    environment[B_i][B_j] = second.getString();
                    second.visit(B_i,B_j);
                    C_i = randomNumbers.nextInt(environment.length);
                    C_j = randomNumbers.nextInt(environment.length);
                    environment[C_i][C_j] = third.getString();
                  third.visit(C_i,C_j);
                    copies++;
                }
                for (int i = 0; i < environment.length; i++) {
                    for (int j = 0; j < environment.length; j++) {
                        if (environment[i][j] == null) {
                            environment[i][j] = ".";
                        }
                    }
                }

                for (int i = 0; i < environment.length; i++) {
                    for (int j = 0; j < environment.length; j++) {
                        System.out.print(environment[i][j]);
                    }
                    System.out.println("");
                }

            }
            if(start!=1) {
                if (start % 2 == 0) {
                    for (int row = 0; row < environment.length; row++) {
                        for (int column = 0; column < environment.length; column++) {
                            if (environment[row][column].equals("&")) {
                                if (column == 0) {
                                    if (environment[row][column + 1].equals("*")) {
                                        first.GainEnergy();
                                        environment[row][column + 1] = first.eat();
                                    } else if(row<environment.length-1 && environment[row+1][column].equals("*") ){

                                        first.GainEnergy();
                                        environment[row+1][column] = first.eat();
                                    }else if(environment[row][column + 1].equals("&")) {
                                        continue;
                                    } else if (environment[row][column + 1].equals("@")) {
                                        continue;
                                    } else {
                                        environment[row][column] = ".";
                                        first.visit(row,column+1);
                                        environment[row][++column] = first.getString();
                                    }
                                } else if (column < environment.length-1) {
                                    if (environment[row][column + 1].equals("*")) {
                                        first.GainEnergy();
                                        environment[row][column + 1] = first.eat();
                                    } else if (environment[row][column - 1].equals("*")) {
                                        environment[row][column - 1] = first.eat();
                                        first.GainEnergy();
                                    } else if(row==0 && environment[row+1][column].equals("*")){
                                        first.GainEnergy();
                                        environment[row+1][column] = first.eat();

                                    }else if (row>0&&row<4&&environment[row+1][column].equals("*")){
                                        first.GainEnergy();
                                        environment[row+1][column] = first.eat();

                                    }else if(row>0&&row<4&&environment[row-1][column].equals("*")){
                                        first.GainEnergy();
                                        environment[row-1][column] = first.eat();

                                    } else if(environment[row][column + 1].equals("&")) {
                                        continue;
                                    } else if (environment[row][column + 1].equals("@")) {
                                        continue;
                                    } else {
                                        environment[row][column] = ".";
                                        first.visit(row,column+1);
                                        environment[row][++column] = first.getString();
                                    }

                                } else if (column == environment.length-1 && environment[row][column].equals("&") && row < environment.length-1) {
                                    if (environment[row + 1][0].equals("*")) {
                                        environment[row + 1][0] = first.eat();
                                        first.GainEnergy();
                                    } else if (environment[row + 1][0].equals("&")) {
                                        continue;
                                    } else if (environment[row + 1][0].equals("@")) {
                                        continue;
                                    } else {
                                        environment[row][column] = ".";
                                        first.visit(row+1,0);
                                        environment[row + 1][0] = first.getString();
                                    }

                                }else if(column == environment.length-1 && environment[row][column].equals("&") && row==environment.length-1){
                                    if(environment[row][column - 1].equals("*")) {
                                        environment[row][column - 1] = first.eat();
                                        first.GainEnergy();
                                    }

                                }


                            }


                        }
                    }
                }
                for (int row = 0; row < environment.length; row++) {
                    for (int column = 0; column < environment.length; column++) {
                        if (environment[row][column].equals("@")) {
                            if (column == 0) {
                                if (environment[row][column + 1].equals("&")) {
                                    environment[row][column + 1] = second.GainEnergy();

                                }else if(row<environment.length-1 && environment[row+1][column].equals("*") ){

                                    environment[row+1][column] = second.GainEnergy();

                                }else if (environment[row][column + 1].equals("@")) {
                                    continue;
                                } else if (environment[row][column + 1].equals("*")) {
                                    continue;
                                } else {
                                    environment[row][column] = ".";
                                    second.visit(row,column+1);
                                    environment[row][++column] = second.getString();
                                }
                            } else if (column < environment.length-1) {
                                if (environment[row][column + 1].equals("&")) {
                                    environment[row][column + 1] = second.GainEnergy();
                                } else if (environment[row][column - 1].equals("&")) {
                                    environment[row][column - 1] = second.GainEnergy();
                                } else if(row==0 && environment[row+1][column].equals("&")){
                                    environment[row+1][column] = second.GainEnergy();

                                }else if (row>0&&row<4&&environment[row+1][column].equals("&")){
                                    environment[row+1][column] = second.GainEnergy();

                                }else if(row>0&&row<4&&environment[row-1][column].equals("*")){
                                    environment[row-1][column] = second.GainEnergy();

                                } else if(environment[row][column + 1].equals("*")) {
                                    continue;
                                } else if (environment[row][column + 1].equals("@")) {
                                    continue;
                                } else {
                                    environment[row][column] = ".";
                                    second.visit(row, column + 1);
                                    environment[row][++column] = second.getString();
                                }
                            } else if (column ==environment.length-1 && environment[row][column].equals("@") && row < environment.length-1) {
                                if (environment[row + 1][0].equals("&")) {
                                    environment[row + 1][0] = second.GainEnergy();
                                } else if (environment[row + 1][0].equals("&")) {
                                    continue;
                                } else if (environment[row + 1][0].equals("*")) {
                                    continue;
                                } else {
                                    environment[row][column] = ".";
                                    second.visit(row+1,0);
                                    environment[row + 1][0] = first.getString();
                                }

                            }


                        }
                    }
                }

                if(first.EnergyLevel%5==0){
                    A_i = randomNumbers.nextInt(environment.length);
                    A_j = randomNumbers.nextInt(environment.length);
                    environment[A_i][A_j] = first.getString();
                    first.visit(A_i,A_j);
                }
                if (start % 4 == 0) {
                    B_i = randomNumbers.nextInt(environment.length);
                    B_j = randomNumbers.nextInt(environment.length);
                    environment[B_i][B_j] = second.Born();
                    second.visit(B_i,B_j);
                }

                if(start%3 == 0){
                    C_i = randomNumbers.nextInt(environment.length);
                    C_j = randomNumbers.nextInt(environment.length);
                    environment[C_i][C_j] = third.Born();
                    third.visit(C_i,C_j);
                }

                System.out.println("");
                for (int i = 0; i < environment.length; i++) {
                    for (int j = 0; j < environment.length; j++) {
                        System.out.print(environment[i][j]);
                    }
                    System.out.println("");
                }

            }
            start++;

        }
        System.out.println("Visited columns and rows : ");
        third.print();
        first.print();
        second.print();

    }
}


===============================


public class Plant {
    private String C;
    public boolean [][] array;

    Plant(boolean [][] array){
        this.array=array;
        this.C= "*";
    }
    public String getString(){

        return C;
    }

    public String Born(){

        return C;
    }

    public void visit(int a, int b){

        this.array[a][b]= true;
    }

    public void print(){

        for(int i = 0;i<array.length;i++){
            for(int j=0;j<array.length;j++) {
                if(array[i][j]==true) {
                    System.out.println("Plant - Column visited " + j );
                    System.out.println("Plant - Row visited " + i );
                }
            }
        }
    }

}

Explanation / Answer

public class Food { } public abstract class Animal : Meat where T:Food { public abstract void Eat(T food); } public class Herbivore : Animal { public override void Eat(Plant food) { Console.WriteLine("Herbivore eats plants."); } } public class Omnivore : Animal { public override void Eat(Food food) { Console.WriteLine("Omnivore eats food."); } } public class Carnivore : Animal { public override void Eat(Meat food) { Console.WriteLine("Carnivore eats meat."); } } public class Plant : Food { } public class Meat : Food { } public class Cow : Herbivore { } public class Tiger : Carnivore { } public class Human : Omnivore { }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote