Thursday, October 30, 2014

Java Simple Pro



Beckett.java

public class Beckett
 {
 public static void moves(int n, boolean forward) 
 {
        if (n == 0) return;
        moves(n-1, true);
        if (forward) 
          System.out.println("enter " + n);
        else          
           System.out.println("exit  " + n);
        moves(n-1, false);
    }
                public static void main(String[] args) 
                {
        int N = Integer.parseInt(args[0]);
        moves(N, true);
    }
}

Htree.java

public class Htree 
{
 public static void drawH(double x, double y, double size) 
{
        double x0 = x - size/2;
        double x1 = x + size/2;
        double y0 = y - size/2;
        double y1 = y + size/2;
        StdDraw.line(x0, y0, x0, y1);    
        StdDraw.line(x1, y0, x1, y1);    
        StdDraw.line(x0,  y, x1,  y);    
    }
    public static void draw(int n, double x, double y, double size) 
    {
        if (n == 0) return;
        drawH(x, y, size);
        double x0 = x - size/2;
        double x1 = x + size/2;
        double y0 = y - size/2;
        double y1 = y + size/2;
        draw(n-1, x0, y0, size/2);    
        draw(n-1, x0, y1, size/2);    
        draw(n-1, x1, y0, size/2);    
        draw(n-1, x1, y1, size/2);    
    }
    public static void main(String[] args) 
    {
        int N = Integer.parseInt(args[0]);
                     double x = 0.5, y = 0.5;  
        double size = 0.5;        
        draw(N, x, y, size);
    }
}

Brownian.java

public class Brownian 
{ 
 public static void curve(double x0, double y0, double x1, double y1, double var, double s) 
{
        if (Math.abs(x1 - x0) < .01) 
         {
            StdDraw.line(x0, y0, x1, y1);
            return;
        }
 
        double xm = (x0 + x1) / 2;
        double ym = (y0 + y1) / 2;
        ym = ym + StdRandom.gaussian(0, Math.sqrt(var));
        curve(x0, y0, xm, ym, var/s, s);
        curve(xm, ym, x1, y1, var/s, s);
    } 
    public static void main(String[] args) 
   { 
        double H = Double.parseDouble(args[0]); 
        double s = Math.pow(2, 2*H); 
        curve(0.0, 0.5, 1.0, 0.5, .01, s); 
    } 
} 

Percolation.java

public class Percolation 
{
 public static boolean[][] flow(boolean[][] open) 
 {
        int N = open.length;
        boolean[][] full = new boolean[N][N];
        for (int j = 0; j < N; j++) 
        {
            flow(open, full, 0, j);
        }
        return full;
    }
            public static void flow(boolean[][] open, boolean[][] full, int i, int j) 
            {
        int N = open.length;
        if (i < 0 || i >= N) return;    
        if (j < 0 || j >= N) return;    
        if (!open[i][j]) return;        
        if (full[i][j]) return;         
        full[i][j] = true;
        flow(open, full, i+1, j);   
        flow(open, full, i, j+1);   
        flow(open, full, i, j-1);   
        flow(open, full, i-1, j);   
    }
                  public static boolean percolates(boolean[][] open) 
     {
        int N = open.length;
        boolean[][] full = flow(open);
        for (int j = 0; j < N; j++) 
       {
            if (full[N-1][j]) return true;
        }
        return false;
    }
    public static void show(boolean[][] a, boolean which) 
   {
        int N = a.length;
        StdDraw.setXscale(-1, N);
        StdDraw.setYscale(-1, N);
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                if (a[i][j] == which)
                    StdDraw.filledSquare(j, N-i-1, .5);
    }
    public static boolean[][] random(int N, double p) 
   {
        boolean[][] a = new boolean[N][N];
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                a[i][j] = StdRandom.bernoulli(p);
        return a;
    }
     public static void main(String[] args) 
     {
        boolean[][] open = StdArrayIO.readBoolean2D();
        StdArrayIO.print(flow(open));
        StdOut.println(percolates(open));
    }
}

VerticalPercolation.java

public class VerticalPercolation
             {
              public static boolean[][] flow(boolean[][] open) 
             {
        int N = open.length;     
        boolean[][] full = new boolean[N][N];
       for (int j = 0; j < N; j++)
       {
            full[0][j] = open[0][j]; 
        }      
                     for (int i = 1; i < N; i++) 
           {
            for (int j = 0; j < N; j++) 
           {
                full[i][j] = open[i][j] && full[i-1][j];
            }
        }
          return full;
    } 
                public static boolean percolates(boolean[][] open) 
                  {
        int N = open.length;
        boolean[][] full = flow(open);
        for (int j = 0; j < N; j++) 
      {
            if (full[N-1][j]) return true;
        }
        return false;
    }
    public static void show(boolean[][] a, boolean which)
    {
        int N = a.length;
        StdDraw.setXscale(-1, N);
        StdDraw.setYscale(-1, N);
        for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        if (a[i][j] == which)
                    StdDraw.filledSquare(j, N-i-1, .5);
    }
    public static boolean[][] random(int N, double p) 
    {
        boolean[][] a = new boolean[N][N];
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                a[i][j] = StdRandom.bernoulli(p);
        return a;
    }
                 public static void main(String[] args) 
                {
        boolean[][] open = StdArrayIO.readBoolean2D();
        StdArrayIO.print(flow(open));
        StdOut.println(percolates(open));
    }       
            }

No comments:

Post a Comment