import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Grades extends Applet implements ActionListener {

  TextField hw;
  TextField test1, test2, test3, grade, hwgrade;
  Choice section;
  Button calc, clear;

  double[] hwAves =
  { 182.0,  178.7407, 204.0, 0,0,0,0,0,0,0, 174.3950};

  double[] hwDevs =
  { 16.5, 23.4928, 9.6,  0,0,0,0,0,0,0, 23.79387}; 

  double[][] tAves =
  {{76.3, 75.5556, 76.4138, 81.4211, 78.1111, 82.6,
    76.2778, 80.5833, 81.6923, 74.76, 84.6316},
   {77.5714, 79.3077, 79.8346, 79, 71.4375, 80.0556,
    76.9211, 80.2917, 79.625, 74.0417, 79.8095},
   {50.8, 62.7037, 53.5417, 59.1176, 63.7333, 56.7778,
    54.6562, 70.8571, 63.2727, 59.5, 61}
  };
  double[][] tDevs = 
  {{17.4695, 17.3811, 16.1876, 14.3229, 14.2143, 14.3064,
    20.9553, 15.5924, 14.0677, 17.6783, 14.8967},
   {14.9949, 23.03, 15.3312, 18.7231, 18.9437, 16.6679,
    15.1934, 14.4898, 13.8652, 14.5646, 13.4893},
   {20.1039, 21.6898, 19.547, 20.721, 14.3376, 22.4331,
    23.8605, 19.1423, 18.558, 24.884, 20.4528}
  };


  public void init() {
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    setLayout(gbl);

    grade = new TextField(5);
    grade.setEditable(false);
    hwgrade = new TextField(5);
    hwgrade.setEditable(false);

    //addWindowListener (new WindowAdapter () {
    // public void windowClosing (WindowEvent e) {dispose();}});
    test1 = new TextField(5);
    test2 = new TextField(5);
    test3 = new TextField(5);
    hw = new TextField(5);
    calc = new Button("Calculate");
    clear = new Button("Clear All");
    calc.addActionListener(this);
    clear.addActionListener(this);

    section = new Choice();
    section.add("1)  M2   2-131 Darren Crowdy");
    section.add("2)  M2   2-132 Yue Lei");
    section.add("3)  M3   2-131 Darren Crowdy");
    section.add("4)  T10  2-131 Sergiu Moroianu");
    section.add("5)  T10  2-132 Gabrielle Stoy");
    section.add("6)  T11  2-131 Sergiu Moroianu");
    section.add("7)  T11  2-132 Gabrielle Stoy");
    section.add("8)  T12  2-132 Anda Degeratu");
    section.add("9)  T12  2-131 Edward Goldstein");
    section.add("10) T1   2-131 Anda Degeratu");
    section.add("11) T2   2-132 Yue Lei");

    gbc.insets = new Insets(1,1,1,1);
    gbc.weightx = 1;

    Panel p = new Panel();
    p.add(new Label("  "));
    addComponent(gbl, gbc, 2, 0, 1, 1, new Label("Section:"));
    addComponent(gbl, gbc, 3, 0, 2, 1, section);
    addComponent(gbl, gbc, 4, 2, 2, 1,
		 new Label("Test Grades (enter point values)"));
    addComponent(gbl, gbc, 1, 2, 2, 1,
		 new Label("Homework Grades (enter total of 10)"));

    //gbc.fill = GridBagConstraints.HORIZONTAL;
    //p = new Panel(new GridLayout(5,2));


    //gbc.fill = GridBagConstraints.NONE;
    //addComponent(gbl, gbc, 2, 9, 3, 1, new Label("Test scores"));
    addComponent(gbl, gbc, 4, 3, 1, 1, new Label("Test #1"));
    addComponent(gbl, gbc, 4, 4, 1, 1, new Label("Test #2"));
    addComponent(gbl, gbc, 4, 5, 1, 1, new Label("Test #3"));
    addComponent(gbl, gbc, 1, 4, 1, 1, new Label("Homework Sum"));
    addComponent(gbl, gbc, 2, 4, 1, 1, hw);
    addComponent(gbl, gbc, 5, 3, 1, 1, test1);
    addComponent(gbl, gbc, 5, 4, 1, 1, test2);
    addComponent(gbl, gbc, 5, 5, 1, 1, test3);

    /*p = new Panel(new GridLayout(3,1));
      p.add(new Label("Test 1"));
      p.add(new Label("Test 2"));
      p.add(new Label("Test 3"));
      addComponent(gbl, gbc, 2, 10, 1, 3, p);
      
      p = new Panel(new GridLayout(3,1));
      p.add(test1);
      p.add(test2);
      p.add(test3);

      addComponent(gbl, gbc, 4, 10, 1, 3, p);
      */
    addComponent(gbl, gbc, 6, 0, 1, 1, clear);
    addComponent(gbl, gbc, 6, 1, 1, 1, calc);
    addComponent(gbl, gbc, 1, 11, 1, 1,
		 new Label("HW SCORE:"));
    addComponent(gbl, gbc, 4, 11, 1, 1,
		 new Label("Test SCORE:"));
    addComponent(gbl, gbc, 5, 11, 1, 1, grade); 
    addComponent(gbl, gbc, 2, 11, 1, 1, hwgrade); 
  }

  void addComponent(GridBagLayout gbl, GridBagConstraints gbc,
		    int gridx, int gridy,
		    int gridwidth, int gridheight,
		    Component c) {
    gbc.gridx = gridx;
    gbc.gridy = gridy;
    gbc.gridwidth = gridwidth;
    gbc.gridheight = gridheight;
    gbl.setConstraints(c, gbc);
    add(c);
  }    

  void calcGrades() {
    int sectionNum = section.getSelectedIndex();
    boolean error = false;
    String message = "";
    double t1a, t1d, t2a, t2d, t3a, t3d, hwa, hwd;
    t1a = tAves[0][sectionNum];
    t1d = tDevs[0][sectionNum];
    t2a = tAves[1][sectionNum];
    t2d = tDevs[1][sectionNum];
    t3a = tAves[2][sectionNum];
    t3d = tDevs[2][sectionNum];
    hwa = hwAves[sectionNum];
    hwd = hwDevs[sectionNum];
    double h = 0.0, testAve = 0.0;
    try {
      double t1 = (((new Float(test1.getText())).floatValue())-t1a)/t1d;
      double t2 = (((new Float(test2.getText())).floatValue())-t2a)/t2d;
      double t3 = (((new Float(test3.getText())).floatValue())-t3a)/t3d;
      h = (((new Float(hw.getText())).floatValue())-hwa)/hwd;
      testAve = (t1+t2+t3)/3;
      message = "Your test average is "+testAve+" standard deviations above "+
	"average.";
    }
    catch (NumberFormatException e) {
      error = true;
      grade.setText("error");
      hwgrade.setText("error");
    grade.setText((Float.toString((float)testAve)).substring(0,5));
    hwgrade.setText((Float.toString((float)h)));
    }

    String s = Float.toString((float)testAve);
    if (s.length() > 5) s = s.substring(0,5);
    grade.setText(s);
    s = Float.toString((float)h);
    if (s.length() > 5) s = s.substring(0,5);
    hwgrade.setText(s);
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Clear All")) {
      test1.setText("");
      test2.setText("");
      test3.setText("");
      grade.setText("");
      hw.setText("");
      hwgrade.setText("");
    }
    else if (e.getActionCommand().equals("Calculate")) {
      calcGrades();
    }
  }
}






