ImportEdu.princeton.cs.algs4.StdOut; ImportEdu.princeton.cs.algs4.StdRandom;ImportEdu.princeton.cs.algs4.StdStats; /**how do I generate a site uniformly at the random among all blocked sites for use in Percolationstats? * Pick a site at random (by using stdrandom to generate, integers between 1 and N) * and use this site if it is Blocke D If not, repeat. */ Public classPercolationstats {Private intT//T Independent Experiments Private Double[] fraction; PublicPercolationstats (intNintT) {//perform T independent experiments on an n-by-n grid if(N <= 0 | | T <= 0) { Throw NewIllegalArgumentException ("N and T must be bigger than 0"); } This. T =T; Fraction=New Double[T]; for(intCount = 0; Count < T; count++) {percolation PR=Newpercolation (N); intOpenedsites = 0; while(!pr.percolates ()) { inti = Stdrandom.uniform (1, n+1); intj = Stdrandom.uniform (1, n+1); if(!Pr.isopen (i, J)) {Pr.open (I, j); Openedsites++; }} Fraction[count]= (Double) Openedsites/(N *N); } } Public DoubleMean () {//sample mean of percolation threshold returnStdstats.mean (fraction); } Public DoubleStdDev () {//sample standard deviation of percolation threshold returnStdstats.stddev (fraction); } Public DoubleConfidencelo () {//Low endpoint of 95% confidence interval returnMean ()-1.96 * StdDev ()/math.sqrt (T); } Public DoubleConfidencehi () {//High endpoint of 95% confidence interval returnMean () + 1.96 * StdDev ()/math.sqrt (T); } Public Static voidMain (string[] args)//test Client (described below) { intN = Integer.parseint (args[0]); intT = Integer.parseint (args[1]); Percolationstats PS=Newpercolationstats (N, T); Stdout.println ("mean =" +Ps.mean ()); Stdout.println ("StdDev =" +Ps.stddev ()); Stdout.println ("95% Confidence interval =" +ps.confidencelo () + "," +Ps.confidencehi ()); }}
Algorithmsi Programming Assignment 1:percolationstats.java