// Birthday.java // Written by Julian Devlin, 8/97, for the text book // "Introduction to Probability," by Charles M. Grinstead & J. Laurie Snell import java.applet.Applet; import java.awt.*; public class Birthday extends java.applet.Applet { TextArea disp; // Area to display HT Panel contp; // Panel for user controls Label numl1; // Controls TextField num1; Label numl2; // Controls TextField num2; Button go; // Initialize applet public void init() { numl1 = new Label("Min no. of people ="); // Create controls num1 = new TextField("20", 4); numl2 = new Label("Max no. of people ="); // Create controls num2 = new TextField("25", 4); go = new Button("Go"); disp = new TextArea(10, 15); // Create display area setLayout(new GridLayout(2, 1)); add(disp); contp = new Panel(); // Set up control panel add(contp); contp.setLayout(new FlowLayout()); contp.add(numl1); contp.add(num1); contp.add(numl2); contp.add(num2); contp.add(go); validate(); } // Handle events public boolean handleEvent(Event evt) { if (evt.target instanceof Button) { if (evt.target == go && evt.id == Event.ACTION_EVENT) // When button is clicked { disp.setText(""); // Reset output window simulate(Integer.valueOf(num1.getText()).intValue(), Integer.valueOf(num2.getText()).intValue()); return true; // Generate correct number of tosses } } return super.handleEvent(evt); // Handle other events as usual } // Calculate probabilities public void simulate(int min, int max) { float prob; for (int i = 0; i < max - min + 1; i++) { prob = 1f; for (int j = 0; j < min + i; j++) { prob *= ((365f - (float) j) / 365f); } disp.appendText(String.valueOf(min + i) + " - " + String.valueOf(prob)); disp.appendText("\n"); } } }