Gravity Percolation Mini-Lab

Nathan Sprague
Kalamazoo College


Gravitational Percolation

In yesterday's Lab, you developed a VerticalPercolator class that models the behavior of a solid material. Objects of this class percolate straight down, but they do not percolate left or right. The goal of today's Mini-Lab is to develop a GravitationalPercolator class. Objects of this class will percolate down, left and right. This could be used as a model of a fluid flowing down through down through a medium.

As a first step, you will make a small change to the VerticalPercolator class that will make it easier to adapt that code to this new problem. Currently, the getPercolationLocation method returns a single object of type Location. That design makes sense for the VerticalPercolator because an object of that class can percolate to at most one location. However that approach won't work for our GravitationalPercolator.


Exercise 1 — Modifying VerticalPercolator:

  1. Modify the getPercolationLocation method to return an ArrayList of Location objects rather than a single location object. You should also rename the method to getPercolationLocations. (Keep in mind that we are not changing the behavior of the VerticalPercolator class. The ArrayList of Location objects returned by getPercolationLocations will always have exactly one location in it: the location directly below the current object.)
  2. Next, modify the act method of the VerticalPercolator to iterate through the list of returned locations, calling PercolateTo for each.
  3. Test your program using the modified VerticalPercolator class. Everything should work exactly as it did before.


Using the modified VerticalPercolator class as a starting point, it will be straightforward to implement the GravitationalPercolator.


Exercise 2 — Creating GravitationalPercolator:

  1. Create the new class, and copy the VerticalPercolator code into the resulting file. The only methods you should need to change are getPercolationLocations and PercolateTo. The getPercolationLocations method should be modified to return the three candidate locations for percolation. (How do you find the locations that are down, left, and right?) The PercolateTo method should be changed to add the appropriate type of Percolator to the grid.
  2. Test your new class by uncommenting the line in PercolationApp that adds "GravitationalPercolator" to the array of editableTypes, as well as the lines that associate an image with your newly created class.


At this point your VerticalPercolator and GravitationalPercolator classes are nearly identical. One of the goals of the next Mini-Lab will be to factor out the common code and move it into a common superclass.

The work you have done for this mini-lab will be part of the Percolation programming project, so you don't need to submit anything at this point.