Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Select Healthcare Classification Systems And Databases

Authors: Katherine S. Rowell, Ann Cutrell

1st Edition

0615909760, 978-0615909769

More Books

Students also viewed these Databases questions