import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;

/**
 * Used to perform Monte-Carlo simulation upon N x N matrix
 */
public class PercolationStats {
    // threshold factor
    private static final double THRESHOLD_FACTOR = 1.96;
    //array of the threshold values
    private double[] threshold;
    //number of experiments
    private int numberOFExperiments;
    //size of the percolation grid

    /**
     * performs independent experiments on an size-by-size grid
     * @param size number of grid
     * @param numberOFExperiments number of experiments
     */
    public PercolationStats(int size, int numberOFExperiments) {
        threshold = new double[numberOFExperiments];
        this.numberOFExperiments = numberOFExperiments;

        for (int i = 0; i < numberOFExperiments; i++) {
            int openCounter = calculate(size);
            threshold[i] = (double) openCounter / (size * size);
        }
    }

    /**+
     * calculates the percolation of the grid
     * @param n size of the grid
     * @return number of opened positions
     */
    private int calculate(final int n) {
        Percolation percolation = new Percolation(n);
        int openCounter = 0;

        while (!percolation.percolates()) {
            int x = StdRandom.uniform(1, n + 1);
            int y = StdRandom.uniform(1, n + 1);

            if (!percolation.isOpen(x, y)) {
               percolation.open(x, y);
               openCounter++;
            }
        }
        return openCounter;
    }

    /**
     * calculates the percolation threshold
     * @return value of the threshold
     */
    public double mean() {
        return StdStats.mean(threshold);
    }

    /**
     * calculates the sample standard deviation of percolation threshold,
     * sharpness of the threshold
     * @return value of the sharpness of the threshold
     */
    public double stddev() {
        if (numberOFExperiments == 1) {
            return Double.NaN;
        }
        return StdStats.stddev(threshold);
    }

    /**
     * low endpoint of 95% confidence interval
     * @return value of the low endpoint interval
     */
    public double confidenceLo() {
        return mean() - (THRESHOLD_FACTOR * stddev()
                / Math.sqrt(numberOFExperiments));
    }

    /**
     * high endpoint of 95% confidence interval
     * @return value of the high endpoint interval
     */
    public double confidenceHi() {
        return mean() + (THRESHOLD_FACTOR * stddev()
                / Math.sqrt(numberOFExperiments));
    }

    /**
     *  test client
     * @param args values to enter
     */
    public static void main(String[] args) {
        StdOut.println("Please state the number of experiments ");
//        int numberOfExperiments = StdIn.readInt();
        StdOut.println("Please state the size of area ");
//        int size = StdIn.readInt();

//        PercolationStats percolationStats = new PercolationStats(size, numberOfExperiments);
        PercolationStats percolationStats = new PercolationStats(0, 2);
        double mean = percolationStats.mean();
        double stddev = percolationStats.stddev();
        double min = percolationStats.confidenceLo();
        double max = percolationStats.confidenceHi();

        StdOut.println("mean " + mean);
        StdOut.println("stddev " + stddev);
        StdOut.println("95% confidence interval = " + min + " - " + max);
    }
}

@@ -467,43 +467,8 @@
 nts;
-%0A    //size of the percolation grid
 %0A%0A  
@@ -584,33 +584,33 @@
 * @param numberO
-F
+f
 Experiments numb
@@ -676,33 +676,81 @@
 ize, int numberO
-F
+fExperiments) %7B%0A        if (size %3C= 0 %7C%7C numberOf
 Experiments) %7B%0A 
@@ -736,36 +736,176 @@
 berOfExperiments
+ %3C= 0
 ) %7B%0A
+            throw new IllegalArgumentException(%22Please check the size %22 +%0A                    %22and number of experiments%22);%0A        %7D%0A%0A
         threshol
@@ -922,25 +922,25 @@
 uble%5BnumberO
-F
+f
 Experiments%5D
@@ -975,33 +975,33 @@
 iments = numberO
-F
+f
 Experiments;%0A%0A  
@@ -1025,33 +1025,33 @@
 = 0; i %3C numberO
-F
+f
 Experiments; i++
@@ -2973,26 +2973,24 @@
 riments %22);%0A
-//
         int 
@@ -3086,18 +3086,16 @@
 rea %22);%0A
-//
         
@@ -3123,18 +3123,16 @@
 Int();%0A%0A
-//
         
@@ -3220,80 +3220,8 @@
 s);%0A
-        PercolationStats percolationStats = new PercolationStats(0, 2);%0A
     

check for size and number of experiments size