Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello! This is a basic percolation program that I\'ve coded for my homework in J

ID: 3840730 • Letter: H

Question

Hello! This is a basic percolation program that I've coded for my homework in Java. The program mostly works, however, when I run it the square at position 0,1 is ALWAYS opened. Can you help me figure out why? I'm not sure what I'm doing wrong.

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
   int top = N*N;
   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?)
       if (i == 0) {
           WUF.union(position, top);
       }
       // Check left
       if (i > 0 && isOpen(i-1, j)) {
           WUF.union(position, (i-1)*N+j);
       }
       // Check right
       if (i < N-1 && 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-1 && 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()
       int position = i*N+j;
       return WUF.connected(top, position);
   }
  
   // does the system percolate?
   public boolean percolates() {
   // TODO 1 line of code (only connected on the bottom row)
       for (int x = 0; x<N; x++) {
           if (isFull(N-1, x)) {
               return true;
           }
       }
       return false;
   }
}

Standard Draw File 1 open sites does not percolate

Explanation / Answer

You are using below code:

public class Percolation {
   int N;
   boolean[] open;
   // TODO: more fields to add here
   int top = N*N;
   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);
   }


==============================================
In the above code, you are declaring two fields, N and top. Field top is N*N. You have declared both, and in the constructor, you initialized N, but not top. So When the constructor call starts, till then N has a value of 0 (default) and thus, top has value 0 (N*N). Now in the constructor, you assign the new value to N, which is perfectly fine, but you did not change the value of top, hence it is still 0.

Making it similar to below will help:

public class Percolation {
int N;
boolean[] open;
  
// TODO: more fields to add here
int top = N*N;
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);
       this.top = N*N;
}



As you have not provided your dependent files such as WeightedUF file from import algs15.*; i am unable to run your code and give you the solution. Please try the above fix, and then also if you are not able to get rid of the issue, Paste your all the files in the question and i will go ahead and try to help you.

Thanks!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote