1. Member
Join Date
Sep 2013
Posts
3
Rep Power
0

The following is a code that I have been working on and it is Breaking when it tries to execute
Java Code:
`P.Open(2, 1);`
It gives the following error: "java.lang.ArrayIndexOutOfBoundsException: 11" at
Java Code:
`WQUUF.union(Map(i,j,n),Map(i-1, j, n));`
in Open() Method. The error only occurs for the value
Java Code:
`P.Open(2, 1);`
if
Java Code:
`Percolation P = new Percolation(10);`
I can't seem to understand why this is going out of bounds.

PS: The WeightedQuickUnionFind class is a custom class and is error free.
================================================== =============================
Java Code:
```public class Percolation {
static WeightedQuickUnionUF WQUUF;
Boolean[][] grid;
static int n;
private boolean Validate (int i, int j) throws ArrayIndexOutOfBoundsException
{
if(i<1||j<1||i>n||j>n)
{
return false;
/*try{
throw new ArrayIndexOutOfBoundsException("e");
}catch(ArrayIndexOutOfBoundsException e){
StdOut.println("Exception caught");
}*/

}else
{
return true;
}
}

static int Map(int i, int j, int N)
{
int t;
t=(i-1)*N+j;
return t;
}
public Percolation(int N)
{
n=N;
WQUUF= new WeightedQuickUnionUF((N^2)+2);
grid = new Boolean[N+1][N+1];
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
grid[i][j] = false;
}
}
/*for(int x=1;x<=N;x++)
{
WQUUF.union(0, x);
}
for(int y=(N^2);y<=(N^2)-N+1;y--)
{
WQUUF.union((N^2)+1, y);
}*/

}
public void Open(int i, int j)
{
if(Validate(i, j))
{
grid[i][j]=true;
if(i==1)
{
WQUUF.union(0, Map(i, j, n));
}
if(i==n)
{
WQUUF.union((n^2)+1, Map(i, j, n));
}
if(Validate(i-1,j)&& isOpen(i-1,j))
{
WQUUF.union(Map(i,j,n),Map(i-1, j, n));
}else if(Validate(i+1, j) && isOpen(i+1, j))
{
WQUUF.union(Map(i,j,n), Map(i+1, j, n));
}else if(Validate(i, j-1) && isOpen(i, j-1))
{
WQUUF.union(Map(i,j,n), Map(i, j-1, n));
}else if(Validate(i, j+1) && isOpen(i,j+1))
{
WQUUF.union(Map(i,j,n),Map(i, j+1, n));
}
}else {
StdOut.println("Out of bound at "+i+","+j);
}
}
public boolean isOpen(int i, int j)
{
if(grid[i][j]== true)
{
return true;
}
return false;
}
public boolean isFull(int i, int j)
{
if(WQUUF.connected(0, Map(i, j, n)))
{
return true;
}else
{
return false;
}
}
public boolean percolates()
{
if(WQUUF.connected(0, (n^2)+2))
{
return true;
}else
{
return false;
}
}
public static void main(String args[])
{
Percolation P = new Percolation(10);
P.Open(1, 1);
P.Open(1, 2);
P.Open(2, 1);
P.Open(1, 3);
P.Open(5,1);
//P.Open(6,1);
StdOut.println(P.isOpen(5, 1));
StdOut.println(WQUUF.connected(Map(1, 2, 10),0));
StdOut.println(P.isFull(2,1));
StdOut.println(n);
}

}

}```
Last edited by Uditt Lamba; 09-02-2013 at 01:49 PM.

2. ## Re: Help debugging the code please!

Originally Posted by Uditt Lamba
Please go through the Forum Rules with special reference to the third paragraph.

db

3. Member
Join Date
Sep 2013
Posts
3
Rep Power
0

## Re: Help debugging the code please!

Originally Posted by DarrylBurke
Please go through the Forum Rules with special reference to the third paragraph.

db
OK, how do I edit the title now?

4. ## Re: Help debugging the code please!

Click 'Edit Post' then 'Go Advanced'

db

5. Member
Join Date
Sep 2013
Posts
3
Rep Power
0