Needs to be written in java.. -I need some help getting setup in the right direc
ID: 3662699 • Letter: N
Question
Needs to be written in java..
-I need some help getting setup in the right direction.. you dont need to program this whole thing.. I dont even know how to disect this assignment nor the questions to ask.. if you could take some time to explain what i need to do , and maybe setup some of the methods which can steer me in the right direction that would be great ! thanks so much
Explanation / Answer
**********Here is the code for your program****************
Percolation.java
public class Percolation
{
private int[][] siteStatus;
private int count;
private WeightedQuickUnionUF quickunion;
private int N;
public Percolation(int N)
{
this.N = N;
siteStatus = new int[N][N];
quickunion = new WeightedQuickUnionUF((N*N)+2);
count = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
//initialise site status as blocked
siteStatus[i][j] = 1;
//initialise site ids
count++;
if (i == 0)
{
quickunion.union(count, 0);
}
else if (i == N-1)
{
quickunion.union(count, (N*N)+1);
}
}
}
}
public void open(int i, int j)
{
int x, y;
x = i-1;
y = j-1;
siteStatus[x][y] = 0;
for (int a = x-1; a <= x+1; a += 2)
{
if (a >= 0 && a < N && y >= 0 && y < N)
{
if (isOpen(a+1, y+1))
{
quickunion.union((a*N)+(y+1), (x*N)+j);
}
}
}
for (int b = y-1; b <= y+1; b += 2)
{
if (x >= 0 && x < N && b >= 0 && b < N)
{
if (isOpen(x+1, b+1))
{
quickunion.union((x*N)+(b+1), (x*N)+j);
}
}
}
}
public boolean isOpen(int i, int j)
{
if (i > 0 && i <= N && j > 0 && j <= N)
{
return siteStatus[i-1][j-1] == 0;
} else {
throw new IndexOutOfBoundsException("Values are out of range");
}
}
public boolean isFull(int i, int j)
{
if (i > 0 && i <= N && j > 0 && j <= N) {
if (quickunion.connected(((i-1)*N) + j, 0))
{
return siteStatus[i-1][j-1] == 0;
}
else
{
return false;
}
}
else
{
throw new IndexOutOfBoundsException("Values are out of range");
}
}
public boolean percolates()
{
return quickunion.connected(0, (N*N)+1);
}
}
PercolationStats.java
public class PercolationStats
{
private Percolation percolation;
private double[] threshold;
private double T;
private int openSites;
public PercolationStats(int T, int N)
{
if (N <= 0 || T <= 0)
{
throw new IllegalArgumentException("Value is out of range");
}
threshold = new double[T];
int randx, randy;
this.T = T;
for (int i = 0; i < T; i++)
{
percolation = new Percolation(N);
randx = StdRandom.uniform(1, N+1);
randy = StdRandom.uniform(1, N+1);
percolation.open(randx, randy);
openSites = 1;
while (!percolation.percolates())
{
randx = StdRandom.uniform(1, N+1);
randy = StdRandom.uniform(1, N+1);
if (!percolation.isOpen(randx, randy))
{
percolation.open(randx, randy);
openSites++;
}
}
threshold[i] = ((double) openSites)/(N*N);
}
}
public double mean()
{
return StdStats.mean(threshold);
}
public double stddev()
{
return StdStats.stddev(threshold);
}
public double confidenceLo()
{
return mean() - (1.96*stddev())/Math.sqrt(T);
}
public double confidenceHi()
{
return mean() + (1.96*stddev())/Math.sqrt(T);
}
public static void main(String[] args)
{
int T, N;
T = Integer.parseInt(args[0]);
N = Integer.parseInt(args[1]);
PercolationStats percolationStats = new PercolationStats(T, N);
//Instead of StdOut.println() function, you can use your own output format
StdOut.println("%Java PercolationStats " + T + " " + N);
StdOut.println("Mean "+percolationStats.mean());
StdOut.println("stddev " + percolationStats.stddev());
StdOut.println("95% confidence interval = "+ percolationStats.confidenceLo()+ ", " + percolationStats.confidenceHi());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.