Why is my code not running? Here is the assignment link: http://coursera.cs.prin
ID: 3725318 • Letter: W
Question
Why is my code not running?
Here is the assignment link: http://coursera.cs.princeton.edu/algs4/assignments/percolation.html
Here is my code for both Percolation and PercolationStats.java:
import java.util.Scanner;
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
public class Percolation
{
private int n;
private int virtualTop;
private int virtualBottom;
private boolean[][] sites;
private WeightedQuickUnionUF weightedQuickUnionUF;
public Percolation(int n)
{
if (n <= 0)
{
throw new IllegalArgumentException("N must be greater than 0.");
}
this.n = n;
sites = new boolean[n][n];
weightedQuickUnionUF = new WeightedQuickUnionUF(n * n + 2);
virtualTop = n * n;
virtualBottom = n * n + 1;
}
private int xyToIndex(int row, int col)
{
return (row - 1) * n + col - 1;
}
private void validateIndex(int row, int col)
{
if (row < 1 || row > n)
throw new IndexOutOfBoundsException("Row must be range of 1 ~ N.");
if (col < 1 || col > n)
throw new IndexOutOfBoundsException("Col must be range of 1 ~ N.");
}
public void open(int row, int col)
{
validateIndex(row, col);
if (!isOpen(row, col))
{
int idx = xyToIndex(row, col);
sites[row - 1][col - 1] = true;
if (row == 1)
weightedQuickUnionUF.union(idx, virtualTop);
if (row == n)
weightedQuickUnionUF.union(idx, virtualBottom);
if (row > 1 && isOpen(row - 1, col))
weightedQuickUnionUF.union(idx, xyToIndex(row - 1, col));
if (row < n && isOpen(row + 1, col))
weightedQuickUnionUF.union(idx, xyToIndex(row + 1, col));
if (col > 1 && isOpen(row, col - 1))
weightedQuickUnionUF.union(idx, xyToIndex(row, col - 1));
if (col < n && isOpen(row, col + 1))
weightedQuickUnionUF.union(idx, xyToIndex(row, col + 1));
}
}
public boolean isOpen(int row, int col)
{
validateIndex(row, col);
return sites[row - 1][col - 1];
}
public boolean isFull(int row, int col)
{
boolean isfull = false;
validateIndex(row, col);
if (isOpen(row, col))
{
isfull = weightedQuickUnionUF.connected(xyToIndex(row, col), virtualTop);
}
return isfull;
}
public boolean percolates()
{
return weightedQuickUnionUF.connected(virtualBottom, virtualTop);
}
public static void main(String args[])
{
@ SuppressWarnings ("resource")
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter in N for N x N grid: ");
int N = keyboard.nextInt();
Percolation percolation = new Percolation(N);
while (!percolation.percolates())
{
int row = StdRandom.uniform(N) + 1;
int col = StdRandom.uniform(N) + 1;
percolation.open(row, col);
}
int numberOfOpenSites = 0;
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++)
{
if (percolation.isOpen(i, j))
{
numberOfOpenSites++;
}
}
}
double prob = (double) (numberOfOpenSites) / (double) (N * N);
System.out.println(prob);
System.out.println(percolation.isFull(N, N));
}
}
__________________________________________________________
import java.util.Scanner;
public class PercolationStats
{
private int count;
private Percolation perc;
private int[] percentage;
public PercolationStats(int N, int T)
{
if (N <= 0 || T <= 0)
{
throw new IllegalArgumentException("N or T cannot be negative!");
}
count = T;
percentage = new int[count];
for (int i = 0; i < count; i++)
{
perc = new Percolation(N);
int openedSites = 0;
while (!perc.percolates())
{
int x = StdRandom.uniform(1, N + 1);
int y = StdRandom.uniform(1, N + 1);
if (!perc.isOpen(x, y))
{
perc.open(x, y);
openedSites++;
}
}
int fraction = openedSites / (N * N);
percentage[i] = fraction;
}
}
public double mean()
{
return StdStats.mean(percentage);
}
public double stddev()
{
return StdStats.stddev(percentage);
}
public double confidenceLo()
{
return mean() - ((1.96 * stddev()) / Math.sqrt(count));
}
public double confidenceHi()
{
return mean() + ((1.96 * stddev()) / Math.sqrt(count));
}
public static void main (String [] args)
{
@ SuppressWarnings ("resource")
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter int N for N x N grid: ");
int N = keyboard.nextInt ();
System.out.print("Enter T number of experiments: ");
int T = keyboard.nextInt ();
PercolationStats stats = new PercolationStats(N, T);
System.out.println("% java PercolationStats " + N + " " + T);
System.out.println("mean = " + stats.mean());
System.out.println("stddev = " + stats.stddev() + "");
System.out.println("95% confidence interval = " +
stats.confidenceLo() + ", " + stats.confidenceHi());
}
}
eclipse-workspace-CSC-3317 Hw04/src/percolationstats.java-Eclipse Eile Edit Source Refactor Navigate Search Project un Window Help Quick Access! . -B D Perc Task List 3-3 olationStats.jawa × | D-percolation.java 4 public class Percolationstats Package Explorer X > Assignment 1 CSC-3317 Hwo2 cSC-3317HW04 CSC-3317-HW03 csc3317,HW01.zip-expanded private int count; private Percolation perc; private inti] percentage; > Find a-All, Activate > > public PercolationStats (int N, int T) 12 13 if (Explanation / Answer
You need to download "algs4.jar" and add it to dependencies for this code. The jar provides access to functions like StdRandom, Union, WeightedQuickUnionUF by importing:
I just added algs4.jar to the code and it ran successfully.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.