/* From: "COMPUTATIONAL PHYSICS, 2nd Ed" by RH Landau, MJ Paez, and CC Bordeianu Copyright Wiley-VCH, 2007. Electronic Materials copyright: R Landau, Oregon State Univ, 2007; MJ Paez, Univ Antioquia, 2007; and CC Bordeianu, Univ Bucharest, 2007. Support by National Science Foundation */ /* Coastline.java: Fractal deposition to form "coastline" if command line argument, print average height over time else; print the coastline itself */ import java.util.Random; import java.io.*; public class Coastline { public static void main(String[] args)throws IOException, FileNotFoundException { PrintWriter q = new PrintWriter( new FileOutputStream("coastline.dat"), true); boolean printavg = args.length > 0; int[] coast = new int[200]; int i; Random random = new Random(); // Deposit particle loop for ( i = 0; i < coast.length; i++) coast[i] = 0; for ( i = 0; i < 20000; i++) { // number of particles dropped int spot = random.nextInt(200); // hit edge fills hole if (spot == 0) { if (coast[spot] < coast[spot+1]) coast[spot] = coast[spot+1]; else coast[spot]++; } else if (spot == coast.length - 1) { if (coast[spot] < coast[spot-1]) coast[spot] = coast[spot-1]; else coast[spot]++; } else if ((coast[spot] coast[spot+1]) coast[spot] = coast[spot-1]; else coast[spot] = coast[spot+1]; } else coast[spot]++; if (printavg) { // Print the average height int sum = 0; for (int j = 0; j < coast.length; j++) sum += coast[j]; System.out.println(sum*1./coast.length); // 1. for float } } if (!printavg) for (i = 0; i < coast.length; i++){ System.out.println(coast[i]); q.println(" "+i+" "+coast[i]+" "); } } }