// Records.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 Records
	extends java.applet.Applet
{
	TextArea disp;		// Area to display HT
	
	Panel dispArea;
	Panel controls;		// Panel for user controls
	
	Label numl1;			// Controls
	TextField num1;
	Label numl2;			// Controls
	TextField num2;
	Button go;
	
	GridBagLayout gbl;
	GridBagConstraints cc;
	
	Combinatorics myC;
	String[] input;
		
	// Initialize applet
	public void init()
	{	
		numl1 = new Label("Number of permutations =");			// Create controls
		num1 = new TextField("100", 4);
		numl2 = new Label("No. of different set sizes =");			// Create controls
		num2 = new TextField("3", 4);
		go = new Button("Go");
		
		disp = new TextArea(15, 20);		// Create display area
		
		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(disp);
		
		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();
	}
	
	// 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 num, int steps)
    {
    	float[] avgRecs = new float[steps];
    	int records;
    	int highest;
    	String[] input;
    	String[] results;
    	for (int size = 0; size < steps; size++) {
    		input = new String[(size + 1) * 10];
	    	for (int i = 0; i < (size + 1) * 10; i++) {
	    		input[i] = String.valueOf(i);
	    	}
	    	myC = new Combinatorics(input);
	    	
    		records = 0;
			for (int i = 0; i < num; i++) {
				highest = -1;
		    	results = myC.randPerm();
		    	for (int j = 0; j < input.length; j++) {
		    		if (Integer.valueOf(results[j]).intValue() > highest) {
		    			highest = Integer.valueOf(results[j]).intValue();
		    			records++;
		    		}
		    	}
		    }
		    avgRecs[size] = (float) records / (float) num;
	    }
	    
	    disp.appendText("Average no. of records for set size:\n\n");
	    for (int i = 0; i < steps; i++) {
	    	disp.appendText((i + 1) * 10 + "    -    " + 
	    		String.valueOf(avgRecs[i]) + "\n");	
	    }
    }

}


