Question: The code below is one of three Java files for a percolation program. I've added bits and pieces of my code but need help finishing

The code below is one of three Java files for a percolation program. I've added bits and pieces of my code but need help finishing it. Any insight and explanation would be much appreciated.

package algs15.perc;

import stdlib.*; import algs15.*;

// Uncomment the import statements above.

// You can test this using InteractivePercolationVisualizer and PercolationVisualizer // All methods should make at most a constant number of calls to the UF data structure, // except percolates(), which may make up to N calls to the UF data structure.

// You can use a second UF to answer percolates. For the second UF, you can connect // all of the elements of the top row and separately all of the elements of the bottom row. // Then you can implement percolates as a single call to "connected".

public class Percolation { int N; boolean[] open; // TODO: more fields to add here WeightedUF WUF; public Percolation(int N) { this.N = N; this.open = new boolean[N*N]; // TODO: more to do here this.WUF = new WeightedUF(N*N); } // open site (row i, column j) if it is not already public void open(int i, int j) { int position = i*N+j; open[position] = true; // TODO: more to do here. 4 cases. (Is is connect on both the top and the bottom?) // make a special case WUF. // Check left if (i >= 0 && isOpen(i-1, j)) { WUF.union(position, (i-1)*N+j); } // Check right if (i <= N && isOpen(i+1, j)) { WUF.union(position, (i+1)*N+j); } // Check above if (j >= 0 && isOpen(i, j-1)) { WUF.union(position, i*N+(j-1)); } // Check below if (j <= N && isOpen(i, j+1)) { WUF.union(position, i*N+(j+1)); } } // is site (row i, column j) open? public boolean isOpen(int i, int j) { return open[i*N+j]; } // is site (row i, column j) full? public boolean isFull(int i, int j) { // TODO 1 line of code (only connected on the top row) // Need a loop here to connect all of the top elements together to answer isFull() return false; } // does the system percolate? public boolean percolates() { // TODO 1 line of code (only connected on the bottom row) return false; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!