Answered step by step
Verified Expert Solution
Question
1 Approved Answer
programming language: Java this is the driver class and post code has to be in java public class Main { public static void main(String[] args)
programming language: Java Introduction In this assignment, we will implement a simple simulation program to show how a predator hunts a prey in a closed environment (e.g., a 10x10 grid). Simulation process will be shown in the terminal by printing. See sample output for more information. The same as the previous assignment, a driver class has been provided. Task #1 Animal, Predator, and Prey Class For this current version of the simulation, we need an abstract class Animal, which will be extended by two subclasses Predator and Prey. Abstract Class Animal 1. Two integer instance variable x and y to store the currently location of an animal. 2. Constructor that takes two integer arguments x and y. 3. Abstract method move (), look (), and act(). a. That is, each animal can move in the gird, look to check the environment, and act to perform some action. 4. Getters and setters for instance variables. 5. Other necessary methods or variables can be added based on your design. Class Predator 1. Override constructor that takes in a Grid (see next task for more information). 2. Set the starting location of the predator at the bottom-right corner of the grid. 3. Override act() method a. If no prey is found, move randomly. b. If a prey is found, move to catch it. 4. Override look () method a. Look for prey in the directions of up, down, left, and right, for a distance of 1 grid cell. i. That is, if a predator is at the location (3, 4), it will look to see if there are any prey in location (2, 4), (4,4), (3, 3), or (3,5). 5. Override move () method a. Either move randomly or move towards a prey i. Randomly moving 1 grid cell in directions of up, down, left, and right (no diagonal move) 6. Other necessary methods or variables can be added based on your design. Class Prey 1. Override constructor that takes in a Grid (see next task for more information). 2. Set the starting location of the prey at the upper-left corner of the grid. 3. Override act() method a. If it is not chased by a predator, move randomly. b. If it is chased by a predator, try to run away. 4. Override look() method a. Look for predator in the directions of up, down, left, and right, for a distance of 1 grid cell. i. That is, if a prey is at location (3, 4), it will look to see if there are any predator in location (2, 4), (4,4), (3, 3), or (3,5). 5. Override move () method a. Either move randomly or move away from predator i. Randomly moving 1 grid cell in directions of up, down, left, and right (no diagonal move) 6. Other necessary methods or variables can be added based on your design. Task #2 Grid Class The class Grid should be a simple implementation of spaces for animals to move within. 1. For this current version, fix the grid size to 10x10. 2. A draw() method that can output the current status of the grid to the terminal. a. Use B to represent prey, and "W' to represent predator. b. See sample output for more information. Sample Output The driver class will repeatedly print out the grid at every time unit. Each of them will look similar to the following: 1 | BI | 1 I 1 IWI 1 Introduction In this assignment, we will implement a simple simulation program to show how a predator hunts a prey in a closed environment (e.g., a 10x10 grid). Simulation process will be shown in the terminal by printing. See sample output for more information. The same as the previous assignment, a driver class has been provided. Task #1 Animal, Predator, and Prey Class For this current version of the simulation, we need an abstract class Animal, which will be extended by two subclasses Predator and Prey. Abstract Class Animal 1. Two integer instance variable x and y to store the currently location of an animal. 2. Constructor that takes two integer arguments x and y. 3. Abstract method move (), look (), and act(). a. That is, each animal can move in the gird, look to check the environment, and act to perform some action. 4. Getters and setters for instance variables. 5. Other necessary methods or variables can be added based on your design. Class Predator 1. Override constructor that takes in a Grid (see next task for more information). 2. Set the starting location of the predator at the bottom-right corner of the grid. 3. Override act() method a. If no prey is found, move randomly. b. If a prey is found, move to catch it. 4. Override look () method a. Look for prey in the directions of up, down, left, and right, for a distance of 1 grid cell. i. That is, if a predator is at the location (3, 4), it will look to see if there are any prey in location (2, 4), (4,4), (3, 3), or (3,5). 5. Override move () method a. Either move randomly or move towards a prey i. Randomly moving 1 grid cell in directions of up, down, left, and right (no diagonal move) 6. Other necessary methods or variables can be added based on your design. Class Prey 1. Override constructor that takes in a Grid (see next task for more information). 2. Set the starting location of the prey at the upper-left corner of the grid. 3. Override act() method a. If it is not chased by a predator, move randomly. b. If it is chased by a predator, try to run away. 4. Override look() method a. Look for predator in the directions of up, down, left, and right, for a distance of 1 grid cell. i. That is, if a prey is at location (3, 4), it will look to see if there are any predator in location (2, 4), (4,4), (3, 3), or (3,5). 5. Override move () method a. Either move randomly or move away from predator i. Randomly moving 1 grid cell in directions of up, down, left, and right (no diagonal move) 6. Other necessary methods or variables can be added based on your design. Task #2 Grid Class The class Grid should be a simple implementation of spaces for animals to move within. 1. For this current version, fix the grid size to 10x10. 2. A draw() method that can output the current status of the grid to the terminal. a. Use B to represent prey, and "W' to represent predator. b. See sample output for more information. Sample Output The driver class will repeatedly print out the grid at every time unit. Each of them will look similar to the following: 1 | BI | 1 I 1 IWI 1
this is the driver class and post code has to be in java
public class Main
{
public static void main(String[] args) throws InterruptedException
{
Grid grid = new Grid();
Predator predator = new Predator(grid);
Prey prey = new Prey(grid);
grid.setPredator(predator);
grid.setPrey(prey);
for(int i = 0; i
{
grid.draw();
predator.act();
prey.act();
if(predator.getX() == prey.getX() && predator.getY() == prey.getY())
{
System.out.println("Prey caught");
System.exit(15);
}
Thread.sleep(200);
}
System.out.println("End of Processing.");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started