In: Computer Science
In Java,
Write a Java program to simulate an ecosystem containing two types of creatures, bears and fish. The ecosystem consists of a river, which is modeled as a relatively large array. Each cell of the array should contain an Animal object, which can be a Bear object, a Fish object, or null. In each time step, based on a random process, each animal either attempts to move into an adjacent array cell or stay where it is. If two animals of the same type are about to collide in the same cell, then they stay where they are, but they create a new instance of that type animal, which is placed in a random empty (i.e., previously null) cell in the array. If a bear and a fish collide, however, then the fish dies (i.e., it disappears). Use actual object creation, via the new operator, to model the creation of new objects, and provide a visualization of the array after each time step.
You will create 4 different classes: River, Animal, Bear and Fish.
The River class will be responsible for simulating the movement of the animals. Animals can move ‘back’, ‘forward’ or stay in place and should do so randomly, with the understanding that they cannot move beyond the bounds of the array. You cannot, for example, move ‘back’ if you are at index 0 and you cannot move ‘forward’ if you are at index river.length-1. Note that the animals are not aware of the environment in which they are placed, they do not know anything about the river, thus the Animal/Bear/Fish classes should not contain methods to control movement. If two animals of the same type and gender collide, only the one with the larger strength survives. The surviving animal’s strength should increase. You should think about the increase and what makes sense, for example, the animal’s strength should not increase by so much that that animal will win every battle from this point forward.
solution:
In implemented type 0 means Bear, type 1 means Fish type 2 means null
Intially created 10 random elements then implemented the logic mentioned above
import java.io.*;
import java.util.*;
class Animal{
int type=2;
public Animal(){
//System.out.println("Animal");
}
}
class Bear extends Animal{
Bear(){
//System.out.println("Bear");
type=0;
}
}
class Fish extends Animal{
Fish(){
type=1;
//System.out.println("Fish");
}
}
public class Demo
{
public static void main(String[] args)throws Exception
{
ArrayList<Animal> river=new
ArrayList<Animal>();
int n=10;
Random rand = new Random();
for(int i=0;i<n;i++){
int
rand_int=rand.nextInt(3);
if(rand_int==0){
Animal b=new Bear();
river.add(b);
}else
if(rand_int==1){
Animal f=new Fish();
river.add(f);
}else{
Animal f=new Animal();
river.add(f);
}
}
for(int i=0;i<n;i++){
int
randint=rand.nextInt(river.size());
int
cur=river.get(randint).type;//checking adjcents index-1
index+1
if(randint-1>=0){
int adj=river.get(randint-1).type;
if(adj==cur){//both same type so creating new
animal of that type
if(adj==1){
Animal
f=new Fish();
river.add(f);
}else{
Animal
b=new Bear();
river.add(b);
}
}else{//removing fish from river
if(adj==1){
river.remove(randint-1);
}else if(cur==1){
river.remove(randint);
}
}
}
if(randint+1<river.size()){
int adj=river.get(randint+1).type;
if(adj==cur){
if(adj==1){
Animal
f=new Fish();
river.add(f);
}else{
Animal
b=new Bear();
river.add(b);
}
}else{
if(adj==1){
river.remove(randint+1);
}else if(cur==1){
river.remove(randint);
}
}
}
for(int
j=0;j<river.size();j++){
int animal=river.get(j).type;
if(animal==1){
System.out.print("Fish
");
}else if(animal==0){
System.out.print("Bear
");
}else{
System.out.print("Empty
");
}
}
System.out.println("");
}
}
}
please give me like