Java code for percolation, code compiles correctly but when it takes values for Input10.txt , the code is suppose to comeback that the system percolates but instead it comes back with the exception. Photos below show the exception. I belive there may be an issue with my logic in the for loop for when to throw the exception. also, i have put the values of Input10.txt at the bottom to try and use to see the exception.
The exception i get is: Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds
for length 10
at Percolation.isOpen(Percolation.java:83)
at Percolation.open(Percolation.java:45)
at Percolation.main(Percolation.java:121)
// Models an N-by-N percolation system.
public class Percolation {
private int N;
private int virtualTop;
private int virtualBottom;
private boolean[][] sites; // N-by-N grid
// Used for determining whether the system is percolate
private WeightedQuickUnionUF weightedQuickUnionUF1;
// Used for opening sites
private WeightedQuickUnionUF weightedQuickUnionUF2;
private int openSiteNumber;
// Creates an N-by-N grid, with all sites blocked.
public Percolation(int N) {
if (N <= 0) {
throw new IllegalArgumentException("N must be greater than
0.");
}
this.N = N;
sites = new boolean[N][N];
// Has both virtualTop and virtualBottom
weightedQuickUnionUF1 = new WeightedQuickUnionUF(N * N + 2);
// Has only virtualTop
weightedQuickUnionUF2 = new WeightedQuickUnionUF(N * N + 1);
virtualTop = N * N;
virtualBottom = N * N + 1;
openSiteNumber = 0;
}
// throw an exception for invalid indices
private void validateIndex(int i, int j) {
// col and row are range of 1 ~ N
if (i < 0 || i > N)
throw new IndexOutOfBoundsException("Row must be range of 0 ~
N.");
if (j < 0 || j > N)
throw new IndexOutOfBoundsException("Col must be range of 0 ~
N.");
}
// Opens site (i, j) if it is not open already.
public void open(int i, int j) {
validateIndex(i, j);
if (!isOpen(i, j)) {
int idx = encode(i, j);
sites[i - 1][j - 1] = true;
openSiteNumber++;
if (i == 1) {
weightedQuickUnionUF1.union(idx, virtualTop);
weightedQuickUnionUF2.union(idx, virtualTop);
}
if (i == N)
weightedQuickUnionUF1.union(idx, virtualBottom);
if (i > 1 && isOpen(i - 1, j)) {
weightedQuickUnionUF1.union(idx, encode(i - 1, j));
weightedQuickUnionUF2.union(idx, encode(i - 1, j));
}
if (i < N && isOpen(i + 1, j)) {
weightedQuickUnionUF1.union(idx, encode(i + 1, j));
weightedQuickUnionUF2.union(idx, encode(i + 1, j));
}
if (j > 1 && isOpen(i, j - 1)) {
weightedQuickUnionUF1.union(idx, encode(i, j - 1));
weightedQuickUnionUF2.union(idx, encode(i, j - 1));
}
if (j < N && isOpen(i, j + 1)) {
weightedQuickUnionUF1.union(idx, encode(i, j + 1));
weightedQuickUnionUF2.union(idx, encode(i, j + 1));
}
}
}
// Checks if site (i, j) is open.
public boolean isOpen(int i, int j) {
validateIndex(i, j);
return sites[i - 1][j - 1];
}
// Checks if site (i, j) is full.
public boolean isFull(int i, int j) {
boolean isfull = false;
validateIndex(i, j);
if (isOpen(i, j)) {
isfull = weightedQuickUnionUF2.connected(encode(i, j),
virtualTop);
}
return isfull;
}
// Returns the number of open sites.
public int numberOfOpenSites() {
return openSiteNumber;
}
// Checks if the system percolates.
public boolean percolates() {
return weightedQuickUnionUF1.connected(virtualBottom,
virtualTop);
}
// Returns an integer ID (1...N) for site (i, j).
private int encode(int i, int j) {
return (i - 1) * N + j - 1;
}
// Test client. [DO NOT EDIT]
public static void main(String[] args) {
String filename = args[0];
In in = new In(filename);
int N = in.readInt();
Percolation perc = new Percolation(N);
while (!in.isEmpty()) {
int i = in.readInt();
int j = in.readInt();
perc.open(i, j);
}
StdOut.println(perc.numberOfOpenSites() + " open sites");
if (perc.percolates()) {
StdOut.println("percolates");
}
else {
StdOut.println("does not percolate");
}
// Check if site (i, j) optionally specified on the command
line
// is full.
if (args.length == 3) {
int i = Integer.parseInt(args[1]);
int j = Integer.parseInt(args[2]);
StdOut.println(perc.isFull(i, j));
}
}
}
these are the values in Input10.txt:
10
9 1
1 9
5 7
1 5
0 3
7 3
9 0
3 1
3 7
8 2
1 1
8 0
3 2
4 4
4 6
1 7
5 3
6 4
8 5
2 6
3 6
6 0
8 3
2 9
0 9
9 9
8 6
0 4
8 7
5 0
1 4
2 3
5 8
4 7
2 1
3 5
0 6
6 8
2 8
3 3
3 9
2 4
2 7
0 7
2 0
5 6
1 2
6 3
8 9
6 5
4 1
7 2
9 7
6 9
3 4
7 9
// Checks if site (i, j) is open.
public boolean isOpen(int i, int j) {
validateIndex(i, j);
return sites[i - 1][j - 1];
}
In the above function you checked the validity of indexes i and j, but you are using i-1 and j-1 in the return statement. Therefore, you should check the validity if i-1 and j-1 instead if i and i. Your method should be like this:
// Checks if site (i, j) is open.
public boolean isOpen(int i, int j) {
validateIndex(i-1, j-1);
return sites[i - 1][j - 1];
}
N.B.
I hope your problem will be solve if you change the code accordingly. If you face problem then share with me in the comment section.I'll help you.
Java code for percolation, code compiles correctly but when it takes values for Input10.txt , the...
Java: Return an array of booleans in a directed graph. Please complete the TODO section in the mark(int s) function import algs13.Bag; import java.util.HashSet; // See instructions below public class MyDigraph { static class Node { private String key; private Bag<Node> adj; public Node (String key) { this.key = key; this.adj = new Bag<> (); } public String toString () { return key; } public void addEdgeTo (Node n) { adj.add (n); } public Bag<Node> adj () { return adj;...
*JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> { private int top, size; private E arrS[]; private static final int MAX_STACK_SIZE = 10; public ArrayStack() { this.arrS = (E[]) new Object[MAX_STACK_SIZE]; this.top = size; this.size = 0;...
Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...
2. Write MinheapPriorityQueue constructor, which takes an array of data, and construct the max heap priority queue using bottom-up algorithm. The expected run time should be O(n), where n is the total number of data. BubbleDown method is provided. You may test it in this minHeap public class MinHeapPriorityQueue<E extends Comparable<? super E>>{ private E data[]; private int size; public MinHeapPriorityQueue(){ this(100); } public MinHeapPriorityQueue(int cap){ size = 0; data = (E[]) new Comparable[cap]; } public MinHeapPriorityQueue(int[] a){ } public...
Please update this java code so 1. A scanner can be used to import the files. Then compute and test with a randomly generated array instead of a file. Compute all possible distinctive pair of values from an an array. [A,B,C, A] A,B A,C B,C - i.e. (A,B)==(B,A) (A, A) is not valid hint: sort the array first Example: input [1, 2, 3, 2, 3, 4, 3] result 4 (with distinct pairs) and 8 (with symmetric pairs import java.util.Arrays; import...
package algs24; import stdlib.StdIn; import stdlib.StdOut; /** * The <tt>PtrHeap</tt> class is the priorityQ class from Question 2.4.24. * It represents a priority queue of generic keys. * * It supports the usual <em>insert</em> and <em>delete-the-maximum</em> * operations, along with methods for peeking at the maximum key, * testing if the priority queue is empty, and iterating through * the keys. * For additional documentation, see <a href="http://algs4.cs.princeton.edu/24pq">Section 2.4</a> of * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne....
JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 The program will assign X to Player 1, and O to Player The program will ask Player 1, to...
My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...
In the class GraphAlgorithm, insert java code for the method
prim
public class MyGraph {
public static final int MAXSIZE = 100;
public static final double BIGM = 10000000;
public MyGraph(int size) {
n = size;
nodeStart = new Edge[MAXSIZE];
for (int i=0; i<n; i++) {
nodeStart[i] = null;
}
}
public int getSize() {
return n;
}
public double getCost(int i, int j) {
double value = BIGM;
Edge e = nodeStart[i];
while (e !=null) {
if (e.dest ==...
software testing without making any changes to the java classes provided below, come up with some junit test cases to test the code. To get you started, here are four test cases you must implement: • Use the setString() function in MyCustomStringInterface to set the value to “H3y, l3t'5 put s0me d161ts in this 5tr1n6!11!!”. Then test to see if the CountNumbers() function is equal to the number of sequential digits in the original string (in this case, 9). •...