I need help with my Java class homework to create the code needed for this progr
ID: 3600686 • Letter: I
Question
I need help with my Java class homework to create the code needed for this program
This is for my Java programming class:
Write a simulation of predators and prey as follows:
The world is modeled as a 20x20 array. Each slot can contain at most one animal.
Rabbits like hoping around the world, and work as follows:
• Each time step, the rabbit will move in a direction randomly. If the move takes the rabbit off the field (such as moving left from column 0), or if the destination cell is already occupied, the rabbit stays in the current square.
• Every third timestep that the rabbit is still alive, it breeds by creating a new rabbit in an adjacent square (if all adjacent squares are occupied, or off of the map, the rabbit can not breed and must wait another three time steps).
Foxes enjoy eating rabbits and work as follows:
• Each time step, the fox will move in a direction randomly. If the move takes the fox off of the world, or on top of another fox, the fox stays in the same square. If the move takes the fox on top of a rabbit, the fox eats the rabbit.
• If the fox goes three moves without eating any rabbits, the fox dies of starvation.
• If the fox is still alive, it will breed every 8 moves.
Write routines to display the state of the field, and simulate the behavior of the foxes and rabbits (which should be derived classes from the base class Animal). Initialize the simulation with 5 foxes and 100 rabbits.
Explanation / Answer
It is very easy to note that if you reach a position (i,j) in the grid, you must have come from one cell higher, i.e. (i-1,j) or from one cell to your left , i.e. (i,j-1). This means that the cost of visiting cell (i,j) will come from the following recurrence relation:
The above statement means that to reach cell (i,j) wit minimum cost, first reach either cell(i-1,j) or cell (i,j-1) in as minimum cost as possible. From there, jump to cell (i,j). This brings us to the two important conditions which need to be satisfied for a dynamic programming problem:
Optimal Sub-structure:- Optimal solution to a problem involves optimal solutions to sub-problems.
Overlapping Sub-problems:- Subproblems once computed can be stored in a table for further use. This saves the time needed to compute the same sub-problems again and again.
(You can google the above two terms for more details)
The problem of finding the min-Cost Path is now almost solved. We now compute the values of the base cases: the topmost row and the leftmost column. For the topmost row, a cell can be reached only from the cell on the left of it. Assuming zero-based index,
i.e. cost of reaching cell (0,j) = Cost of reaching cell (0,j-1) + Cost of visiting cell (0,j) Similarly,
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.