This is for a Java Project - I am mentioning the entire project below. Howerver
ID: 3574809 • 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
MyOrganism.java
abstract class MyOrganism
{
protected boolean isalive;
public MyOrganism()
{
isalive = true;
}
public boolean isAlive()
{
return isalive;
}
public abstract void myupdate(MyOrganism[] myneighbours);
}
My animal.java
abstract class MyAnimal extends MyOrganism
{
private int myage;
public MyAnimal()
{
myage = 0;
}
public int getAge()
{
return myage;
}
public void myupdate(MyOrganism[] myneighbors)
{
myage++;
}
}
MyPlant.java
class MyPlant extends MyOrganism
{
public static final int MAXORGANISMS = 5;
public static final int MAXHERBIVORES = 2;
public void myupdate(MyOrganism[] myneighbours)
{
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;
}
public String toString()
{
return "*";
}
}
Carnivore.java
class MyCarnivore extends MyAnimal
{
public static final int MYLIFETIME = 15;
public static final int MAXFAST = 4;
private int myfast = 0;
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;
}
public String toString()
{
return "@";
}
}
Herbivore.java
class MyHerbivore extends MyAnimal
{
public static final int MIN_NO_OF_PLANTS = 1;
public static final int MYLIFETIME = 20;
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;
}
public String toString()
{
return "&";
}
}
Main.java
class Main
{
private MyOrganism[][] mygrid;
private int mygen;
public Main(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();
}
}
}
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;
}
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;
}
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++;
}
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;
}
public void mytabulate()
{
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++;
}
}
}
}
}
public static void main(String[] args)
{
Main simul = new Main(5,5);
System.out.println(simul);
simul.mytabulate();
for (int idx=0; idx<10; idx++)
{
simul.mysimulate();
System.out.println(simul);
simul.mytabulate();
}
}
}
Output:-
Generation = 0
.&...
...@.
**.@@
.@*..
*.&.*
Generation = 1
....&
@**@.
**.@@
.@*@.
*.&**
Generation = 2
..*..
@**@.
*.*@@
@@.@.
*.&**
Generation = 3
&.*&.
@**@.
**.@@
@@.@.
*.&**
Generation = 4
&.*&.
@..@@
*..@@
@@@@.
**&**
Generation = 5
.**&*
@.*@@
*..@@
@@@@.
**&**
Generation = 6
.**&*
@.*@@
*..@@
@@@@.
**&**
Generation = 7
.**&*
@**@@
*&.@@
@@@@@
**&**
Generation = 8
.**&*
@..@@
*&.@@
@@@@@
**&**
Generation = 9
***&*
@.@@@
*&.@@
@@@@@
**&**
Generation = 10
***&*
@.@@@
*&.@@
@@@@@
**&**
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.