// StockSystem.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 StockSystem	extends java.applet.Applet{	Float[] xCoords;		// Variables for simulation	Float[] yCoords;		Float[] stockX;	Float[] stockY;		StockGraph stockg;	SpikeGraph sg;				// AWT elements		Panel dispArea;	Panel controls;		// Panel for user controls		Label numl1, numl2;			// Controls	TextField num1, num2;	Button go;		GridBagLayout gbl;	GridBagConstraints cc;		JRandom myRand;			// Initialize applet	public void init()	{			numl1 = new Label("Number of days =");			// Create controls		num1 = new TextField("20", 4);		numl2 = new Label("Number of trials");			// Create controls		num2 = new TextField("100", 4);		go = new Button("Go");				stockg = new StockGraph();		sg = new SpikeGraph(); // initialize a graphing space				dispArea = new Panel();				// Set up window		controls = new Panel();		setLayout(new BorderLayout(5, 5));				add("South", controls);		add("Center", dispArea);				dispArea.setLayout(new GridLayout(2, 1));		dispArea.add(stockg);		dispArea.add(sg);				gbl = new GridBagLayout();		controls.setLayout(gbl);				cc = new GridBagConstraints();				cc.gridx = 0;		cc.gridy = 0;		gbl.setConstraints(numl1, cc);		controls.add(numl1);				cc.gridx = 1;		gbl.setConstraints(num1, cc);		controls.add(num1);				cc.gridx = 0;		cc.gridy = 1;		gbl.setConstraints(numl2, cc);		controls.add(numl2);				cc.gridx = 1;		gbl.setConstraints(num2, cc);		controls.add(num2);				cc.gridx = 0;		cc.gridy = 2;		cc.gridwidth = 2;		gbl.setConstraints(go, cc);		controls.add(go);				validate();				myRand = new JRandom();	}		// 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			{        		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 days, int num)    {	    	xCoords = new Float[(int) (1.5f * (float) days) + 1];    	yCoords = new Float[(int) (1.5f * (float) days) + 1];    	stockX = new Float[days + 1];    	stockY = new Float[days + 1];    	int[] temp = new int[(int) (1.5f * (float) days) + 1];    	    	int stock;    	int earnings;    	boolean owner;    	int rnd;    	    	for (int i = 0; i < xCoords.length; i++) {    		xCoords[i] = new Float(i - days);    		temp[i] = 0;    	}    	for (int i = 0; i < num; i++) {    		stock = 0;    		earnings = 0;    		owner = true;    		if (i == 0) {    			stockX[0] = new Float(0);    			stockY[0] = new Float(0);    		}    		for (int j = 0; j < days; j++) {    			rnd = myRand.nextInt(0, 1);    			if (rnd == 1)    				stock++;    			else    				stock--;    			if (owner == true && stock == 1) {    				earnings++;    				owner = false;    			}    			else if (owner == false && stock == 0) 					owner = true; 				if (i == 0) { 					stockX[j + 1] = new Float(j + 1); 					stockY[j + 1] = new Float(stock); 				}    		}    		if (owner == true)    			earnings += stock;    			    		temp[earnings + days]++;    	}    	    	for (int i = 0; i < yCoords.length; i++) {    		yCoords[i] = new Float((float) temp[i] / (float) num);    	}    	    	dispArea.remove(stockg);		dispArea.remove(sg);		stockg = new StockGraph(stockX, stockY);		sg = new SpikeGraph(xCoords, yCoords);	// Create new SpikeGraph		dispArea.add(stockg);		dispArea.add(sg);							// Put up the graph		validate();	}}
