// HTSimulation.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 HTSimulation extends java.applet.Applet{	Float[] yCoords;		// Variables for simulation	Float[] xCoords;		LineGraph lg;				// AWT elements	Label message;	Button go;	TextField num;			Panel graphArea;	Panel controls;	// Set up all controls and put them in the window	public void init() {		message = new Label("Number of coin tosses =");	// Create controls		go = new Button("Simulate");		num = new TextField(4);				graphArea = new Panel();				// Set up window		controls = new Panel();		setLayout(new BorderLayout());			controls.setLayout(new FlowLayout());		controls.add(message);		controls.add(num);		controls.add(go);				lg = new LineGraph(); // initialize a graphing space		add("Center", lg);		add("South", controls);	}		// Does the simulation, creating two arrays to store game states, then passes them	// to a LineGraph	public void simulate() {		float yCoord = 0.0f;		// Running count of game score		int n = 0;		try {			n = Integer.parseInt(num.getText().trim());		// Get user input		}		catch (Exception e) {			message.setText("Please, number of coin tosses =");			num.setText("");			validate();		}		xCoords = new Float[n + 1];					// Make arrays		yCoords = new Float[n + 1];		xCoords[0] = new Float(0);		yCoords[0] = new Float(0);		for (int i = 0; i < n; i++) {				// Do actual simulation			if (Math.random() < 0.5) 				yCoord++;			else 				yCoord--;			xCoords[i + 1] = new Float(i + 1);			yCoords[i + 1] = new Float(yCoord);		}		message.setText("Number of coin tosses =");		remove(lg);		lg = new LineGraph(xCoords, yCoords);	// Create new LineGraph		add("Center", lg);							// Put up the graph		validate();	}		// Watch for a click on the go button	public boolean action(Event evt, Object arg) {		if (evt.target instanceof Button) {			if ((String)arg == "Simulate") {				simulate();			}		}		return true;	}		public Insets insets() {    	return new Insets(5,5,5,5);	}}
