Physics, Chemistry, Geology, and Material Science, among other fields, all can include the study of Percolation Theory. Put simply, percolation concerns the movement and filtering of fluids through porous materials. (Thank you, Wikipedia.) Other applications of percolation modeling can examine movement of electricity or fire, but the idea is the same.
Given a grid consisting of open squares and closed squares, (think of the game "Minesweeper",) a simple percolation model checks whether there is a path through the open squares from the top of the grid to the bottom, without passing through closed squares or moving diagonally.
To make things more interesting, the selection of which blocks are closed is typically generated using some random probability distribution. This technique belongs to a much larger class of computational algorithms called Monte Carlo methods, widely used to simulate complex physical and mathematical systems that cannot otherwise be well understood.
Create a class RandomGrid that generates a randomized grid of size n. Your class should take two command-line parameters: a positive integer grid size, "n", and a probability threshhold, "p", in the range 0.0 - 1.0, inclusive. Use Java's exception handling mechanisms to make your program robust against all kinds of input errors.
Once you have valid values for n and p, create an n x n grid of integers. Use the class java.util.Random to generate a random value between 0.0 and 1.0 for each element in the grid; if the random number is less than or equal to the threshhold p, close that grid square off.
Please declare the following methods for the RandomGrid class. I may replace your RandomGrid class with my own, non-random version during testing, so it is important that you conform to this API.
Create a Percolate class that uses the RandomGrid class to run a percolation simulation on a random grid. Percolation starts with any open squares in the top row of the grid, and continues to all adjacent squares that can be reached vertically and horizontally without passing though a blocked square.
A recursive exploration of the grid will be more elegant than an iterative algorithm.
Print out the final grid with percolation noted, and a final judgement at the bottom showing whether flow is possible from the top row through to the bottom row.