From 8f57108b678f1fc63f901060a6bac9c8df84f7c7 Mon Sep 17 00:00:00 2001 From: "Jakk.Wong@gmail.com" Date: Mon, 3 Sep 2012 21:36:26 -0700 Subject: [PATCH] Change commenting style. --- HW1/Percolation.java | 39 ++++++++++++++++----------------------- HW1/PercolationStats.java | 24 +++++++++++------------- 2 files changed, 27 insertions(+), 36 deletions(-) diff --git a/HW1/Percolation.java b/HW1/Percolation.java index f531545..c231054 100644 --- a/HW1/Percolation.java +++ b/HW1/Percolation.java @@ -7,25 +7,24 @@ public class Percolation { private WeightedQuickUnionUF ufTwo; public Percolation(int N) { - // First site in N*N matrix starts at index 0 + // First site in N*N matrix starts at index 0. gridSize = N; - virtualTop = N*N; // For 4x4 matrix, the virtualTop will be at index 16 - virtualBottom = (N*N)+1; // For 4x4 matrix, the virtualBottom will be at index 17 - openSites = new int[N*N]; // Matrix used to keep track of opened/closed sites using 1's (opened) and 0's (closed) + virtualTop = N*N; // For 4x4 matrix, the virtualTop will be at index 16. + virtualBottom = (N*N)+1; // For 4x4 matrix, the virtualBottom will be at index 17. + openSites = new int[N*N]; // Matrix used to keep track of opened/closed sites using 1's (opened) and 0's (closed). - ufOne = new WeightedQuickUnionUF((N*N)+1); // Obj used in checking for isFull - no virtual bottom - ufTwo = new WeightedQuickUnionUF((N*N)+2); // Obj used in checking for percolation + ufOne = new WeightedQuickUnionUF((N*N)+1); // Obj used in checking for isFull - no virtual bottom. + ufTwo = new WeightedQuickUnionUF((N*N)+2); // Obj used in checking for percolation. for (int i = 0; i < N; i++) { - ufOne.union(i, virtualTop); // establish virtual top for uf object 1 - ufTwo.union(i, virtualTop); // establish virtual top for uf object 2 - ufTwo.union((N*N)-(i+1), virtualBottom); // establish virtual bottom for uf object 2 + ufOne.union(i, virtualTop); // Establish virtual top for uf object 1. + ufTwo.union(i, virtualTop); // Establish virtual top for uf object 2. + ufTwo.union((N*N)-(i+1), virtualBottom); // Establish virtual bottom for uf object 2. } } - /* Open a given site at i, j coordinates */ - + // Opens a given site at i, j coordinates. public void open(int i, int j) { validateIndices(i, j); int newSite1D = xyTo1D(i, j); @@ -36,16 +35,14 @@ public void open(int i, int j) { connectNeighborOf(newSite1D, i, j-1); // left } - /* Converts i, j coordinates to 1D index */ - + // Converts i, j coordinates to 1D index. private int xyTo1D(int i, int j) { int a = i-1; int b = j-1; return (gridSize*a)+b; } - /* Connects a site to its neighbor if it is opened */ - + // Connects a site to its neighbor if it is opened. private void connectNeighborOf(int site, int i, int j) { if ((i > 0 && i <= gridSize) && (j > 0 && j <= gridSize)) { if (isOpen(i, j)) { @@ -56,8 +53,7 @@ private void connectNeighborOf(int site, int i, int j) { } } - /* Checks whether a site at i, j is opened */ - + // Checks whether a site at i, j is opened. public boolean isOpen(int i, int j) { // an open site is not blocked validateIndices(i, j); @@ -65,8 +61,7 @@ public boolean isOpen(int i, int j) { return openSites[siteToCheck] == 1; } - /* Checks whether a site at i, j is opened and connected to another site in top row */ - + // Checks whether a site at i, j is opened and connected to another site in top row. public boolean isFull(int i, int j) { validateIndices(i, j); int siteToCheck = xyTo1D(i, j); @@ -76,8 +71,7 @@ public boolean isFull(int i, int j) { return false; } - /* Checks if any cell in bottom row is connected to another cell in top row through virtual sites */ - + // Checks if any cell in bottom row is connected to another cell in top row through virtual sites. public boolean percolates() { if (gridSize == 1 && !isOpen(1, 1)) { return false; @@ -85,8 +79,7 @@ public boolean percolates() { return ufTwo.connected(virtualTop, virtualBottom); } - /* Throws exception when given i and j coordinates are out of bounds */ - + // Throws exception when given i and j coordinates are out of bounds. private void validateIndices(int i, int j) { if (i <= 0 || i > gridSize) throw new IndexOutOfBoundsException("row index i out of bounds"); if (j <= 0 || j > gridSize) throw new IndexOutOfBoundsException("row index j out of bounds"); diff --git a/HW1/PercolationStats.java b/HW1/PercolationStats.java index be4d7ea..47c54a8 100644 --- a/HW1/PercolationStats.java +++ b/HW1/PercolationStats.java @@ -2,34 +2,34 @@ public class PercolationStats { private Percolation pc; private double[] thresholdList; - /* N is N in N*N matrix. T is number of experiments ran */ - + // N is N in N*N matrix. T is number of experiments ran. public PercolationStats(int N, int T) { if (N <= 0 || T <= 0) { throw new IllegalArgumentException("N and T cannot be less than or equal to 0."); } - thresholdList = new double[T]; // Array used to keep track of thresholds generated in all T experiments + thresholdList = new double[T]; // Array used to keep track of thresholds generated in all T experiments. + // Performs Monte Carlo Simulation. for (int i = 0; i < T; i++) { double threshold = calculateThreshold(N); - thresholdList[i] = threshold; // Each threshold produced from an experiment is added to the array + thresholdList[i] = threshold; // Each threshold produced from an experiment is added to the array. } } + // Main method used in the simulations. private double calculateThreshold(int N) { pc = new Percolation(N); double count = 0; while (!pc.percolates()) { - int i = StdRandom.uniform(1, N+1); // generates a random number from 1 to N+1 + int i = StdRandom.uniform(1, N+1); // Generates a random number from 1 to N+1. int j = StdRandom.uniform(1, N+1); if (!pc.isOpen(i, j)) { pc.open(i, j); count += 1; - } - + } } - // threshold is calculated as (# of opened sites required to percolate)/# of sites in matrix + // Threshold is calculated as (# of opened sites required to percolate)/# of sites in matrix. return count/(N*N); } @@ -44,13 +44,12 @@ public double stddev() { private String confidenceInterval(double mean, double stddev, int T) { double lessThan = mean - ((1.96*stddev)/Math.sqrt(T)); double greaterThan = mean + ((1.96*stddev)/Math.sqrt(T)); - return String.format("%f, %f", lessThan, greaterThan); - + return String.format("%f, %f", lessThan, greaterThan); } public static void main(String[] args) { - int N = Integer.parseInt(args[0]); // N is provided in command line as argument 1 - int T = Integer.parseInt(args[1]); // T is provided in command line as argument 2 + int N = Integer.parseInt(args[0]); // N is provided in command line as argument 1. + int T = Integer.parseInt(args[1]); // T is provided in command line as argument 2. PercolationStats ps = new PercolationStats(N, T); double mean = ps.mean(); double stddev = ps.stddev(); @@ -59,5 +58,4 @@ public static void main(String[] args) { StdOut.println("95% confidence interval = " + ps.confidenceInterval(mean, stddev, T)); } - }