All pastes #288107 Raw Edit

WaitTime

public java v1 · immutable
#288107 ·published 2006-12-22 01:04 UTC
rendered paste body
import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.util.*;public class WaitTime extends JPanel{    private JTextField triesField;    private JButton triesButton;    Frequency freq;    int[] tries;    boolean yesTry = false;        public WaitTime()    {        JPanel time = new JPanel();        freq = new Frequency();                triesField = new JTextField(40);        triesField.setEditable(false);                tries = new int[30];        triesButton = new JButton("Tries");                TriesListener listen = new TriesListener();        triesButton.addActionListener(listen);                setLayout(new GridLayout(2,2));        add(time);        add(freq);                time.setLayout(new FlowLayout(FlowLayout.LEFT));        time.add(triesButton);        time.add(triesField);        yesTry = false;    }        public class TriesListener implements ActionListener    {        public void actionPerformed(ActionEvent ae)        {            Integer ctr = 0;            int bush = 3, num;            String str = "";            for (int ind=0; ind<tries.length; ind++)            {                do {                    num = (int)(Math.random()*5+1);                    ctr++;                } while(num != bush);                tries[ind] = ctr;                str += ctr.toString() + " ";                ctr = 0;            }            triesField.setText(str);            str = "";            yesTry = true;        }    }        public class Frequency extends JPanel    {        ArrayList freqs, numStore;                public Frequency()        {            String[] fr = new String[1];            freqs = new ArrayList(1);            int size;            if (yesTry)            {                getFreqs();                fr = new String[freqs.size()];                freqs.toArray(fr);                yesTry = false;            }            setLayout(new GridLayout(freqs.size(),1));            for (int ind=0; ind<freqs.size(); ind++)            {                add(new JLabel(fr[ind]));            }        }                public void getFreqs()        {            String str = "";            numStore = new ArrayList();            int num = 0;            int ctr = 0;            Arrays.sort(tries);            for (int ind=0; ind<tries.length; ind++)            {                if (ind == 0)                {                   num = tries[ind];                   numStore.add(num);                    for (int ind2=0; ind<tries.length; ind++)                    {                        if (tries[ind2] == num)                            ctr++;                    }                }                else if (tries[ind] != tries[ind-1])                {                    num = tries[ind];                    numStore.add(num);                    for (int ind2=0; ind<tries.length; ind++)                    {                        if (tries[ind2] == num)                            ctr++;                    }                }                str = num + " came up " + ctr + " times.";                freqs.add(str);                ctr = 0;                str = "";            }        }    }}