import java.applet.Applet; /* John Valentino II jvalentino@student.tcu.edu */ import java.awt.*; import java.awt.event.*; import java.util.*; public class random extends Applet implements ActionListener { static Font boldFont = new Font("Times",Font.BOLD,12); static Label one = new Label("1:"); static Label two = new Label("2:"); static Label three = new Label("3:"); static Label four = new Label("4:"); static Label five = new Label("5:"); static Label six = new Label("6:"); static Label seven = new Label("7:"); static Label eight = new Label("8:"); static Label nine = new Label("9:"); static Label ten = new Label("10:"); static Label on = new Label(""); static Label tw = new Label(""); static Label thre = new Label(""); static Label fou = new Label(""); static Label fiv = new Label(""); static Label si = new Label(""); static Label seve = new Label(""); static Label eigh = new Label(""); static Label nin = new Label(""); static Label te = new Label(""); static Label ave = new Label("Average value:"); static Label value = new Label("Value:"); static Label counter = new Label ("Times Occured in set # 1"); static Label av1 = new Label(""); static Label av2 = new Label(""); static Label av3 = new Label(""); static Label av4 = new Label(""); static Label av5 = new Label(""); static Label av6 = new Label(""); static Label av7 = new Label(""); static Label av8 = new Label(""); static Label av9 = new Label(""); static Label av10 = new Label(""); static TextField size = new TextField(""); static Label sizeLabel = new Label("Number of Random Integers to Generate (n):"); static TextField timesField = new TextField(""); static Label timesLabel = new Label("Times to run through each set of Random Integers (x):"); static long count = 0; static int number; static Button button = new Button("Run Integers x Times"); static Label tThrough = new Label("Times Through: 0"); static boolean loop = true; static Panel top = new Panel(new GridLayout(11,3)); static Panel bottom = new Panel(new GridLayout(2,1)); static Panel pa = new Panel(new GridLayout(2,2)); public void init() { setSize(650,400); setLayout(new BorderLayout()); //From App Version //setTitle("Generates n Random Integers between 1 and 10, x times."); //MenuBar mbar = new MenuBar(); //Menu mf = new Menu("Program Information"); //mf.add(new MenuItem("Programmed and Developed by")); //mf.add(new MenuItem("John Valentino II")); //mf.add(new MenuItem("jvalentino@student.tcu.edu")); //mbar.add(mf); //setMenuBar(mbar); setBackground(Color.white); setForeground(Color.black); setFont(boldFont); top.add(value); top.add(counter); top.add(ave); value.setBackground(Color.black); counter.setBackground(Color.black); ave.setBackground(Color.black); value.setForeground(Color.white); counter.setForeground(Color.white); ave.setForeground(Color.white); top.add(one); top.add(on); top.add(av1); one.setBackground(Color.cyan); on.setBackground(Color.cyan); av1.setBackground(Color.cyan); top.add(two); top.add(tw); top.add(av2); top.add(three); top.add(thre); top.add(av3); three.setBackground(Color.cyan); thre.setBackground(Color.cyan); av3.setBackground(Color.cyan); top.add(four); top.add(fou); top.add(av4); top.add(five); top.add(fiv); top.add(av5); five.setBackground(Color.cyan); fiv.setBackground(Color.cyan); av5.setBackground(Color.cyan); top.add(six); top.add(si); top.add(av6); top.add(seven); top.add(seve); top.add(av7); seven.setBackground(Color.cyan); seve.setBackground(Color.cyan); av7.setBackground(Color.cyan); top.add(eight); top.add(eigh); top.add(av8); top.add(nine); top.add(nin); top.add(av9); nine.setBackground(Color.cyan); nin.setBackground(Color.cyan); av9.setBackground(Color.cyan); top.add(ten); top.add(te); top.add(av10); add(top,"North"); bottom.add(button); bottom.add(tThrough); tThrough.setBackground(Color.black); tThrough.setForeground(Color.white); add(bottom,"Center"); pa.add(sizeLabel); sizeLabel.setBackground(Color.black); sizeLabel.setForeground(Color.white); pa.add(size); pa.add(timesLabel); timesLabel.setBackground(Color.black); timesLabel.setForeground(Color.white); pa.add(timesField); add(pa,"South"); button.addActionListener(this); //App Window Closer, (inner class) /* addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { thisWindowClosing(e); } }); validate(); */ } public void actionPerformed(ActionEvent e) { //using a thread instead of another class allows the window to be closed //during an operation if (e.getActionCommand() == "Run Integers x Times") { //loop = true; Thread t1 = new runNumbers(); t1.start(); } } /* // Close the window when the close box is clicked void thisWindowClosing(java.awt.event.WindowEvent e) { setVisible(false); dispose(); System.exit(0); } */ } public class runNumbers extends Thread { double aveValue[] = new double[10]; double oneSum[] = new double[10]; long counter[] = new long[10]; long numSize = 0; long mainSize = 0; public runNumbers() { } public void run() { for (int i = 0; i < 10; i++) { aveValue[i] =0; oneSum[i] = 0; } random.count = 0; numSize = Long.valueOf(random.size.getText()).longValue(); mainSize = Long.valueOf(random.timesField.getText()).longValue(); for (int i = 0; i < mainSize; i++) { for (int l = 0; l < 10; l++) { counter[l] = 0; } for (int j = 0; j < numSize; j++) { int temp = (int) (Math.random() * 10 + 1); if (temp == 1){ counter[0]++; random.on.setText(counter[0]+""); } else if (temp == 2){ counter[1]++; random.tw.setText(counter[1]+""); } else if (temp == 3){ counter[2]++; random.thre.setText(counter[2]+""); } else if (temp == 4){ counter[3]++; random.fou.setText(counter[3]+""); } else if (temp == 5){ counter[4]++; random.fiv.setText(counter[4]+""); } else if (temp == 6){ counter[5]++; random.si.setText(counter[5]+""); } else if (temp == 7){ counter[6]++; random.seve.setText(counter[6]+""); } else if (temp == 8){ counter[7]++; random.eigh.setText(counter[7]+""); } else if (temp == 9){ counter[8]++; random.nin.setText(counter[8]+""); } else if (temp == 10){ counter[9]++; random.te.setText(counter[9]+""); } } random.count++; random.tThrough.setText("Times throught the set of "+numSize+" : " +random.count+""); random.counter.setText("Times Occured in set # "+random.count); random.ave.setText("Average value:"+" ( Value / "+ random.count+" )"); for (int k = 0; k < 10; k++) { oneSum[k] = (counter[k] + oneSum[k]); aveValue[k] = oneSum[k]/(i+1); } random.av1.setText(aveValue[0]+""); random.av2.setText(aveValue[1]+""); random.av3.setText(aveValue[2]+""); random.av4.setText(aveValue[3]+""); random.av5.setText(aveValue[4]+""); random.av6.setText(aveValue[5]+""); random.av7.setText(aveValue[6]+""); random.av8.setText(aveValue[7]+""); random.av9.setText(aveValue[8]+""); random.av10.setText(aveValue[9]+""); } } }