Here are requirements and deliverables required from this question I need the he
ID: 3587476 • Letter: H
Question
Here are requirements and deliverables required from this question I need the help of some java expert in doing this ASAP:
Deliverables.
1) Submit only Percolation.java (using the weighted quick-union algorithm as implemented in the WeightedQuickUnionUF class) and PercolationStats.java.stdlib.jar is available at
http://introcs.cs.princeton.edu/java/stdlib/ and WeightedQuickUnionUF.java is available at http://algs4.cs.princeton.edu/code/. On this assignment, the only library functions you may call are those in java.lang,stdlib.jar and WeightedQuickUnionUF.java.
2) Submit a readme.txt file and answer question.
Explanation / Answer
PercolationStats.java
public class PercolationStats {
private double[] mX;
private double mMean, mStdDev;
// perform T independent computational experiments on an N-by-N grid
public PercolationStats(final int n, final int t) {
if (n <= 0 || t <= 0) {
throw new java.lang.IllegalArgumentException();
}
mX = new double[t];
for (int i = 0; i < t; i++) {
Percolation p = new Percolation(n);
int openSites = 0;
while (!p.percolates()) {
int row, col;
do {
row = StdRandom.uniform(1, n + 1);
col = StdRandom.uniform(1, n + 1);
} while (p.isOpen(row, col));
p.open(row, col);
openSites++;
}
mX[i] = ((double) openSites) / (n * n);
}
mMean = StdStats.mean(mX);
mStdDev = StdStats.stddev(mX);
}
// sample mean of percolation threshold
public final double mean() {
return mMean;
}
// sample standard deviation of percolation threshold
public final double stddev() {
return mStdDev;
}
// test client, described below
public static void main(final String[] args) {
// TODO Auto-generated method stub
if (args.length < 2) {
throw new java.lang.IllegalArgumentException();
}
int n = Integer.parseInt(args[0]);
int t = Integer.parseInt(args[1]);
PercolationStats ps = new PercolationStats(n, t);
double mean = ps.mean();
double stddev = ps.stddev();
System.out.printf("mean = %f ", mean);
System.out.printf("stddev = %f ", stddev);
final double intervalConstant = 1.96;
double interval = (stddev * intervalConstant) / Math.sqrt(t);
System.out.printf("95%% confidence interval = %f, %f ", mean
- interval, mean + interval);
}
}
Percolation.java
public class Percolation {
private WeightedQuickUnionUF mUf, mIsFullUf;
private boolean[][] mSites;
private int mN;
private boolean mPercolates;
public Percolation(final int n) {
mN = n;
// N-by-N grid with 2 virtual sites
mUf = new WeightedQuickUnionUF(mN * mN + 2);
mIsFullUf = new WeightedQuickUnionUF(mN * mN + 1);
mSites = new boolean[mN][mN];
for (int i = 0; i < mN; i++) {
for (int j = 0; j < mN; j++) {
mSites[i][j] = false;
}
}
mPercolates = false;
}
public final void open(final int i, final int j) {
if (i < 1 || j < 1 || i > mN || j > mN) {
throw new java.lang.IndexOutOfBoundsException();
}
if ((i + 1) <= mN && isOpen(i + 1, j)) {
mUf.union(site(i, j), site(i + 1, j));
mIsFullUf.union(site(i, j), site(i + 1, j));
}
if ((i - 1) >= 1 && isOpen(i - 1, j)) {
mUf.union(site(i, j), site(i - 1, j));
mIsFullUf.union(site(i, j), site(i - 1, j));
}
if (j > 1 && isOpen(i, j - 1)) {
mUf.union(site(i, j), site(i, j - 1));
mIsFullUf.union(site(i, j), site(i, j - 1));
}
if (j < mN && isOpen(i, j + 1)) {
mUf.union(site(i, j), site(i, j + 1));
mIsFullUf.union(site(i, j), site(i, j + 1));
}
// Connect to top virtual site
if (i == 1) {
mUf.union(0, site(i, j));
mIsFullUf.union(0, site(i, j));
}
// Connect to bottom virtual site
if (i == mN) {
mUf.union(site(i, j), mN * mN + 1);
}
mSites[i - 1][j - 1] = true;
}
public final boolean isOpen(final int i, final int j) {
if (i < 1 || j < 1 || i > mN || j > mN) {
throw new java.lang.IndexOutOfBoundsException();
}
return mSites[i - 1][j - 1];
}
public final boolean isFull(final int i, final int j) {
if (i < 1 || j < 1 || i > mN || j > mN) {
throw new java.lang.IndexOutOfBoundsException();
}
return mIsFullUf.connected(0, site(i, j));
}
public final boolean percolates() {
if (!mPercolates) {
mPercolates = mUf.connected(0, mN * mN + 1);
}
return mPercolates;
}
private int site(final int i, final int j) {
return (i - 1) * mN + j;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.