All Directions Percolation Mini-Lab

Nathan Sprague
Kalamazoo College

 


All Directions Percolation

The goal of today's Mini-Lab is to create a new class that implements our third and final style of percolation. As the name suggests, the AllDirectionsPercolator will be able to percolate in all four directions. This could be used to model a gas diffusing through a medium.

It would be straightforward to implement this new class by copying the GravitationalPercolator developed in the previous Mini-Lab and making a slight change to the getPercolationLocations method. The drawback of this approach is that it results in three different classes that are nearly identical: the act method would be exactly the same in all three and the percolateTo method would be nearly the same. The classes would differ substantially only in their getPercolationLocation methods. This type of code repetition leads to confusing programs that are difficult to maintain. The solution is to move the shared methods into an abstract superclass that all three Percolators will inherit from.

There is a small difference between the percolateTo methods of the VerticalPercolator and the GravitationalPercolator: When a VerticalPercolator percolates to a location it creates a new VerticalPercolator. When a GravitationalPercolator percolates to a new location it creates a new GravitationalPercolator. In order to address this you will add a new abstract method to the superclass called duplicate which will return an object of type AbstractPercolator. All off the percolator classes will need to override this method to generate an object of the appropriate type. In this way, the percolateTo method of AbstractPercolator will be able to generate the new object to add by using the duplicate method. It will not need to know the actual type of the new object.

 

Exercise 1 — Refactoring VerticalPercolator and Gravitational Percolator:

  1. Currently both VerticalPercolator and GravitationalPercolator are subclasses of the GridObject class. Modify these classes so that instead of extending GridObject, they extend AbstractPercolator.
  2. Copy the act method and the percolateTo methods from either VerticalPercolator or GravitationalPercolator to AbstractPercolator. Delete those methods from both VerticalPercolator and GravitationalPercolator.
  3. Add an abstract method called duplicate to the AbstractPercolator. The return type for this method will be AbstractPercolator. You will also need to add an abstract getPercolationLocations method.
  4. Override the duplicate method in both the VerticalPercolator and GravitationalPercolator classes. Keep in mind that the method signatures must exactly match the declaration in the superclass (with the exception of the "abstract" keyword).
  5. Modify the percolateTo method of the AbstractPercolator so that it uses the duplicate method instead of creating a new object directly.
  6. Test the modified version of your program. Both percolators should work exactly the same way they did before you made your changes.

 

Exercise 2 — Implementing AllDirectionPercolator:

  1. Create an AllDirectionPercolator that extends the AbstractPercolator class. You can use the GravitationalPercolator as a guide. (If you want, you may use the neighborsOf method within the Grid class. Class documentation for all classes in the Grid Package can be found here.)

  2. Test your new class by uncommenting the line in PercolationApp that adds "AllDirectionsPercolator" to the array of editableTypes, as well as the lines that associate an image with your newly created class.

 

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. You can get started on the programming project any time after completing this Mini-Lab.