import java.applet.*; import java.awt.*; public class CliffSolution extends Applet { int wins = 0; int losses = 0; int position = 0; int numRuns = 0; int total = 0; int runCount = -1; String numer, denom; int num = 0, den = 0; boolean wait = false; int[] arrPositionCount = new int[30]; int[] arrPositionSmallCount = new int[30]; Image back, offscreenImage; Graphics offscreenGraphics; Font f; Thread runner; Button start100; Button start10000; Button start1000000; TextField numerator; TextField denominator; public void init() { add(new Label("Homer will step toward the edge")); numerator = new TextField("1", 3); add(numerator); add(new Label("out of")); denominator = new TextField("3", 3); add(denominator); add(new Label("times.")); start100 = new Button("100 Runs"); add(start100); start10000 = new Button("10,000 Runs"); add(start10000); start1000000 = new Button("1,000,000 Runs"); add(start1000000); back = getImage(getCodeBase(), "back.gif"); offscreenImage = createImage(size().width, size().height); offscreenGraphics = offscreenImage.getGraphics(); repaint(); } public void paint(Graphics g) { offscreenGraphics.drawImage(back, 0, 0, this); offscreenGraphics.setFont(new Font("TimesRoman", 1, 12)); offscreenGraphics.drawString("Wins: " + wins, 5, 70); offscreenGraphics.drawString("Losses: " + losses, 5, 85); if (runCount < 0) { for (int i = 0; i < 30; i++) { if (wins == 0 && losses == 0) total = 1; //so we have no division by zero below else total = wins + losses; offscreenGraphics.drawString(i + "", 30, 120 + 15*i); offscreenGraphics.drawString("" + arrPositionCount[i], 70, 120 + 15*i); offscreenGraphics.drawString("" + (double)arrPositionCount[i]/(total), 150, 120 + 15*i); offscreenGraphics.drawString("" + arrPositionSmallCount[i], 230, 120 + 15*i); offscreenGraphics.drawString("" + (double)arrPositionSmallCount[i]/(total), 310, 120 + 15*i); } } else { f = new Font("TimesRoman", 1, 20); offscreenGraphics.setFont(f); offscreenGraphics.setColor(Color.red); offscreenGraphics.drawString("Please Wait - Runs Calculated: " + runCount, 40, 103); offscreenGraphics.setColor(Color.black); wait = false; } g.drawImage(offscreenImage,0,0,this); } public void update(Graphics g) { paint(g); } public boolean action(Event event, Object obj) { if(event.target == start100) { setProbability(); for (int i = 0; i < 30; i++) { arrPositionSmallCount[i] = 0; arrPositionCount[i] = 0; } wins = 0; losses = 0; numRuns = 100; run100(); } if(event.target == start10000) { setProbability(); for (int i = 0; i < 30; i++) { arrPositionSmallCount[i] = 0; arrPositionCount[i] = 0; } wins = 0; losses = 0; numRuns = 10000; run100(); } if(event.target == start1000000) { setProbability(); for (int i = 0; i < 30; i++) //change to 30 steps { arrPositionSmallCount[i] = 0; arrPositionCount[i] = 0; } wins = 0; losses = 0; numRuns = 1000000; wait = true; update(this.getGraphics()); System.out.println("Before run100 call"); run100(); System.out.println("After run100 call"); } return true; } public void setProbability() { numer = numerator.getText(); denom = denominator.getText(); try { num = Integer.parseInt(numer.trim()); } catch (NumberFormatException nfe) { System.out.println("NumberFormatException: " + nfe.getMessage()); } try { den = Integer.parseInt(denom.trim()); } catch (NumberFormatException nfe) { System.out.println("NumberFormatException: " + nfe.getMessage()); } } public void run100() { position = 0; boolean moveForward; boolean[] arrOfPosition = new boolean[30]; arrOfPosition[0] = true; wins = 0; update(this.getGraphics()); for (int i = 0; i < numRuns; i++) { boolean keepGoing = true; position = 0; for (int k = 1; k < 30; k++) arrOfPosition[k] = false; do { if (i % 50000 == 0) { runCount = i; update(this.getGraphics()); } moveForward = move(); if (moveForward) position--; else position++; if (position >= 0 && position <= 29) arrOfPosition[position] = true; if (position < 0) { losses++; for (int j = 0; j < 30; j++) if (arrOfPosition[j]) arrPositionCount[j]++; for (int j = 29; keepGoing; j--) if (arrOfPosition[j]) { arrPositionSmallCount[j]++; keepGoing = false; } } if (position > 29) wins++; } while (position >= 0 && position <=29); } wait = false; runCount = -1; repaint(); } public boolean move() { boolean moveForward; int i = (int)(Math.random() * den); if (i < num) moveForward = true; else moveForward = false; return moveForward; } }