//These are some of the standard "imports" we'll be using, explained in class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;

public class MinMax extends Applet implements ActionListener {

   JTextArea  output;
   JButton button;
   
   String[] names = { "Courtney" , "Rishi" ,  "Emerald" , "Austin" , "Maulik" ,
           "Karmela Marie" , "Andrew" , "Arvind" , "Preetam" , "Deepak" , "Curtis Maxwell" ,
           "Vibhor" , "Harry" , "Nelson " , "Adam" , "Michelle" , "Ethan" , "Yasmine" ,
           "Brianne" , "Kent" , "Alex" , "Aarti" , "Cody" };



   public void init() {

      setLayout(new BorderLayout());

      // Create the text-area and button we'll use
      output = new JTextArea();
      button = new JButton("Find Min & Max");

      // Next we create a "panel" that we add button to
      JPanel p = new JPanel();
      p.setLayout(new FlowLayout());

      p.add(button);

      // Finally, to finish building the UI, we simply add the text and the panel
      // (which contains the button) to the applet
      add("Center", new JScrollPane(output));
      add("South", p);

      // Lastly, we instruct the applet to "listen" for the button to be pressed
      button.addActionListener(this);
      
      output.append("The current list of students is:\n");
      for (int i = 0; i < names.length ; i = i + 1 ) {
    	  output.append((i + 1) + ". " + names[i] + "\n");
      }
   }
   
   void sleepForASecond() {
	   try {
		   Thread.sleep(500);
	   } catch (Exception e) {
	   }
   }

   public void actionPerformed(ActionEvent evt) {

	   String min = names[0];
	   String max = names[0];
	   
	   for (int i = 1; i < names.length; i = i + 1) {
		   if (names[i].compareTo(min) < 0) {
			   min = names[i];
		   }
		   
		   if (names[i].compareTo(max) > 0) {
			   max = names[i];
		   }
		   
		   System.out.println("i = " + i + ", names[i] = " + names[i] + ", min = " + min + ", max = " + max);
		   sleepForASecond();
	   }
	   
	   output.append("first name is: " + min + "\n");
	   output.append("last name is: " + max + "\n");
   }
}






