Question

In: Computer Science

In Java, Write a Java program to simulate an ecosystem containing two types of creatures, bears...

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.

Solutions

Expert Solution

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


Related Solutions

Write a Java program to simulate the rolling of two dice. The application should use an...
Write a Java program to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12. Your application should roll the dice 36,000,000 times. Store the results of each roll...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional int array. Your program must (1) read the name of the file from the command-line arguments, (2) open the file, (3) check that each line has the same number of characters and (4) check that the only characters in a line are ones and zeros. If there is no such command-line argument or no such file, or if any of the checks fail, your...
Java 176 Lottery Program in Word. Using ArrayLists to Simulate a Lottery Program Simulate a Lottery...
Java 176 Lottery Program in Word. Using ArrayLists to Simulate a Lottery Program Simulate a Lottery Drawing by choosing 7 numbers at random from a pool containing 30 numbers Each time a number is selected, it is recorded and removed from the pool The pool values are 00 to 29 inclusive Your program must output each selected number from the drawing using a two-digit format. For Example, the number 2 would be represented by 02 on the “Picked” line. The...
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
Write a program in Java, that creates a Jframe with a menu containing only file. Inside...
Write a program in Java, that creates a Jframe with a menu containing only file. Inside file there should be items: Open, Save, and Save As. Selecting open prompts the user to input a file name to a txt document containing employee information and displays a Jtable with the information, which can be edited. With column headers {"First Name" , "Last Name" , "Occupation" , "Office #"} Example: Gary Osbourn Teacher 113 Michelle Ramirez Teacher 101 Ava Gomez Principal 120...
Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
Java: Using ArrayLists to Simulate a Lottery Program Simulate a Lottery Drawing by choosing 7 numbers...
Java: Using ArrayLists to Simulate a Lottery Program Simulate a Lottery Drawing by choosing 7 numbers at random from a pool containing 30 numbers Each time a number is selected, it is recorded and removed from the pool The pool values are 00 to 29 inclusive Your program must output each selected number from the drawing using a two-digit format. For Example, the number 2 would be represented by 02 on the “Picked” line.    The numbers drawn cannot repeat Sort...
Write a program to simulate a bank transaction. There are two bank accounts: checking and savings....
Write a program to simulate a bank transaction. There are two bank accounts: checking and savings. First, ask for the initial balances of the bank accounts; reject negative balances. Then ask for the transactions; options are deposit, withdrawal, and transfer. Then ask for the account; options are checking and savings. Then ask for the amount; reject transactions that overdraw an account. At the end, print the balances of both accounts. in if and else statments only in c++
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
assigsment-Write a program to simulate a bank transaction. There are two bank accounts: checking and savings....
assigsment-Write a program to simulate a bank transaction. There are two bank accounts: checking and savings. First, ask for the initial balances of the bank accounts; reject negative balances. Then ask for the transactions; options are deposit, withdrawal, and transfer. Then ask for the account; options are checking and savings. Then ask for the amount; reject transactions that overdraw an account. At the end, print the balances of both accounts. in c++ work done- output and need help with the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT