// NFoldConvolution.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 NFoldConvolution	extends java.applet.Applet{	Float[] xCoords;		// Variables for simulation	Float[] yCoords;		SpikeGraph sg;				// AWT elements		Panel dispArea;	Panel controls;		// Panel for user controls		Label numl1, numl2;			// Controls	TextField num;	Button go;		GridBagLayout gbl;	GridBagConstraints cc;		float[] probs = {36f/52f, 4f/52f, 4f/52f, 4f/52f, 4f/52f};			// Initialize applet	public void init()	{			numl1 = new Label("P(C >=");			// Create controls		num = new TextField("13", 4);		numl2 = new Label(") =                    ");		go = new Button("Go");				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(1, 1));		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(num, cc);		controls.add(num);				cc.gridx = 2;		gbl.setConstraints(numl2, cc);		controls.add(numl2);				cc.gridx = 0;		cc.gridy = 1;		cc.gridwidth = 3;		gbl.setConstraints(go, cc);		controls.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			{        		simulate(Integer.valueOf(num.getText()).intValue());        		return true;					// Generate correct number of tosses			}		}		return super.handleEvent(evt);	// Handle other events as usual	}		public float[] convolve(float[] d1, float[] d2) {		float[] results = new float[d1.length + d2.length - 1];		for (int i = 0; i < results.length; i++) {			results[i] = 0;			}		for (int i = 0; i < d1.length; i++) {			for (int j = 0; j < d2.length; j++) {				results[i + j] += d1[i] * d2[j];				}			}			return results;	}		// Calculate probabilities    public void simulate(int score)    {	    	xCoords = new Float[31];    	yCoords = new Float[31];    	float[] temp;    	float openings = 0;    	    	for (int i = 0; i < xCoords.length; i++) {    		xCoords[i] = new Float(i);       	}    	    	temp = probs;    	for (int i = 1; i < 13; i++) {    		temp = convolve(temp, probs);    	}    		    	    	for (int i = 0; i < yCoords.length; i++) {    		yCoords[i] = new Float(temp[i]);    		if (i >= score)    			openings += temp[i];    	}    			dispArea.remove(sg);		sg = new SpikeGraph(xCoords, yCoords);	// Create new SpikeGraph		dispArea.add(sg);							// Put up the graph		numl2.setText(") = " + String.valueOf(openings));		validate();	}}
