Skip to content

Algorithm 1#3

Open
EnochMbaebie wants to merge 1 commit intosweskills:masterfrom
EnochMbaebie:master
Open

Algorithm 1#3
EnochMbaebie wants to merge 1 commit intosweskills:masterfrom
EnochMbaebie:master

Conversation

@EnochMbaebie
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@erictoguem erictoguem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very well written and structured code. But you could improve it by properly naming your variables (for instance using row and column instead of x and y), properly formating your comment (using javadoc style for method commenting). Finally, you didn't implement numberOfOpenSites

Comment thread Percolation.java
siteStatus = new boolean[SIZE*SIZE + 2];
for (int i = 0; i < SIZE*SIZE; i++) {
siteStatus[i] = false;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed as the default value of boolean is false.

Comment thread Percolation.java
private int OIL_BELOW;

// create N-by-N grid, with all sites blocked
public Percolation(int N) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please could you check that the input N is valid?

Comment thread Percolation.java
if (r < 1 || r > SIZE || c < 1 || c > SIZE) {
return false;
}
return true;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could improve this by returning directly a boolean expression.
return r >=1 && ...

Comment thread Percolation.java

private void connectTwoSite(int x1, int y1, int x2, int y2) {
if (validIndex(x2, y2)) {
if (isOpen(x2, y2)) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could avoid enclosed if by using ANDed expression...

Comment thread Percolation.java
// 1D of site connected to all sites in last row
private int OIL_BELOW;

// create N-by-N grid, with all sites blocked
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please could you use a javadoc like comment here
/** One line comment. */

Comment thread Percolation.java
private boolean[] siteStatus;

// percolation experiment's grid width and height
private int SIZE;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is not static final (constant), then shouldn't be written in capital letters... Same for the two bellow...

Comment thread Percolation.java
public void open(int i, int j) {
if (!validIndex(i, j)) {
throw new IndexOutOfBoundsException("row or/and column index out of bounds");
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're repeating this pattern three times, you could extract it to a checkIndex method and just call that one...

Comment thread Percolation.java
return PercolateTest.connected(OIL_BELOW, H20_SURFACE);
}

// convert 2 dimensional(row, column) pair to 1 dimensional index
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please could you also use one line javadoc comment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants