public class Percolation {
  
  // Create N-by-N grid, with all sites blocked
  public Percolation(int n){
    private int N = n;
    private int[][] multi = new int[N][N];;
    private WeightedQuickUnionUF uf = new WeightedQuickUnionUF(N*N);; 
   }  
  
  // Open site (row i, column j) if it is not open already
  public void open(int i, int j){
    if (i<0 || j<0|| i>=N || j>=N) { 
      throw new IndexOutOfBoundsException("IndexError: Row/Column Index is not between 0 and " + N);
      }
    else {
      if(multi[i][j]!=1){
        multi[i][j]=1;
        if(i < N-1)  if(isOpen(i+1, j)) {uf.union(id(i, j), id(i+1, j));}
        if(i > 0)  if(isOpen(i-1, j)) {uf.union(id(i, j), id(i-1, j));}
        if(j < N-1)  if(isOpen(i, j+1)) {uf.union(id(i, j), id(i, j+1));}
        if(j > 0)  if(isOpen(i, j-1)) {uf.union(id(i, j), id(i, j-1));}
      }
    }
  }
    
  private int id(int i, int j) {
    /*
     * Take care of the edge cases 
     */
    if(i<0)  {i = 0;}
    if(j<0)  {j = 0;}
    if(i>=N) {i = N-1;}
    if(i>=N) {j = N-1;}
    return N*i + j;
  }
  
  //is site (row i, column j) open?
  public boolean isOpen(int i, int j){
    return multi[i][j] == 1;}     
  
  //is site (row i, column j) full?
  public boolean isFull(int i, int j){
    boolean full = false;
    for(int k=0;k<N;k++) {
//      StdOut.println(k);
      if(uf.connected(id(i,j), k)) {
        full = true;
        break;
      }
    }
    return full;
  }
  
  public boolean percolates() { // Does the system percolate?
    boolean perc = false;
    for(int i=N-1, j=0; j<N; j++) {
      if(isFull(i,j)) {
        return true;
        }
    }
    return perc;
  }             
  
  public static void main(String[] args) { // Test client (optional)
  //     StdOut.println("Testing, Testing, 1,2,3, ... ");
  int N = StdIn.readInt();
  //     StdOut.println("N: "+N);
  Percolation p = new Percolation(N);
  while(true) {
    int i = StdRandom.uniform(N);
    int j = StdRandom.uniform(N);
    //       StdOut.println(i+"  "+j+"  "+p.id(i, j));
    p.open(i,j);
    if(p.percolates()) {
      float P=0;
      for(int ii=0;ii<N; ii++)
        for(int jj=0;jj<N;jj++) {
          P+= p.multi[ii][jj];
        }
      P/=(N*N);
      StdOut.println("P* = "+(P));
      break;
      }
    }     
  }   
}

@@ -20,24 +20,51 @@
 ation %7B%0A  %0A 
+ public Percolation(int n)%7B
  // Create N
@@ -102,37 +102,8 @@
 ked%0A
-  public Percolation(int n)%7B%0A
     
@@ -247,16 +247,48 @@
 %7D  %0A  %0A 
+ public void open(int i, int j)%7B
  // Open
@@ -341,42 +341,8 @@
 ady%0A
-  public void open(int i, int j)%7B%0A
     
@@ -895,22 +895,47 @@
 j) %7B
-%0A    /*
+ // 2D array to id for UnionFind
 %0A    
- *
+//
  Tak
@@ -964,16 +964,8 @@
 es %0A
-     */%0A
     
@@ -1083,44 +1083,8 @@
 %0A  %0A
-  //is site (row i, column j) open?%0A
   pu
@@ -1113,24 +1113,58 @@
 t i, int j)%7B
+ //is site (row i, column j) open?
 %0A    return 
@@ -1194,44 +1194,8 @@
 %0A  %0A
-  //is site (row i, column j) full?%0A
   pu
@@ -1228,16 +1228,50 @@
  int j)%7B
+ //is site (row i, column j) full?
 %0A    boo
@@ -1317,37 +1317,8 @@
 k++)
- %7B%0A//      StdOut.println(k);
 %0A   
@@ -1354,57 +1354,20 @@
 )) %7B
-%0A        full = true;%0A        break;%0A      %7D%0A    
+return true;
 %7D%0A  
@@ -1535,25 +1535,16 @@
 (i,j)) %7B
-%0A        
 return t
@@ -1547,25 +1547,16 @@
 rn true;
-%0A        
 %7D%0A    %7D%0A
@@ -1667,122 +1667,30 @@
 )%0A  
-//     StdOut.println(%22Testing, Testing, 1,2,3, ... %22);%0A  int N = StdIn.readInt();%0A  //     StdOut.println(%22N: %22+N
+int N = StdIn.readInt(
 );%0A 
@@ -1814,63 +1814,8 @@
 N);%0A
-    //       StdOut.println(i+%22  %22+j+%22  %22+p.id(i, j));%0A
     

Update Percolation.java