import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class StudentGrades extends JFrame{
private static final long serialVersionUID = 1L;
private JLabel test1Label;
private JLabel test2Label;
private JLabel test3Label;
private JLabel studentNameLabel;
private JLabel averageLabel;
private JTextField test1TextField;
private JTextField test2TextField;
private JTextField test3TextField;
private JTextField studentNameTextField;
private JTextField averageTextField;
private JTextArea studentScoreTextArea;
private JButton submitGradesJButton;
private TitledBorder inputTitledBorder;
private TitledBorder averageScoreTitledBorder;
private JRadioButton numericJRadioButton;
private JRadioButton letterJRadioButton;
private JPanel inputScoreJPanel;
private JPanel averageScoreJPanel;
private final int MAXIMUM_STUDENTS = 10;
private final int NUMBER_OF_TESTS = 3;
private int classCounter = 0;
private double studentGrades[][] = new double[MAXIMUM_STUDENTS][NUMBER_OF_TESTS+1];
private String[] strudentNames = new String[MAXIMUM_STUDENTS];
private double totalGrades = 0;
private DecimalFormat gradesFormat;
public JPanel getAverageScoreJPanel() {
averageScoreJPanel = new JPanel();
studentScoreTextArea = new JTextArea();
studentScoreTextArea.setBounds(12,20,425,200);
studentScoreTextArea.setText("Name\tTest 1\tTest 2\tTest 3\tAverage\n");
averageScoreJPanel.add(studentScoreTextArea);
averageScoreTitledBorder = new TitledBorder("Average of each student");
averageScoreJPanel.setBorder(averageScoreTitledBorder);
averageScoreJPanel.setBounds(230,20,445,230);
averageScoreJPanel.setLayout(null);
return averageScoreJPanel;
}
public JPanel getInputScoreJPanel() {
studentNameLabel = new JLabel("student name:");
studentNameLabel.setBounds(10,30,90,25);
test1Label = new JLabel("test1:");
test1Label.setBounds(10,70,80,25);
test2Label = new JLabel("test2:");
test2Label.setBounds(10,110,80,25);
test3Label = new JLabel("test3:");
test3Label.setBounds(10,140,80,25);
studentNameTextField = new JTextField();
studentNameTextField.setBounds(120,30,80,25);
test1TextField = new JTextField();
test1TextField.setBounds(120,70,80,25);
test2TextField = new JTextField();
test2TextField.setBounds(120,110,80,25);
test3TextField = new JTextField();
test3TextField.setBounds(120,140,80,25);
submitGradesJButton = new JButton();
submitGradesJButton.setText("Student Grades");
submitGradesJButton.setBounds(80,180,130,25);
submitGradesJButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
submitGradesJButtonactionPerformed(e);
}
});
inputTitledBorder = new TitledBorder("input Grades");
inputScoreJPanel = new JPanel();
inputScoreJPanel.setBounds(5,20,245,230);
inputScoreJPanel.setLayout(null);
inputScoreJPanel.add(studentNameLabel);
inputScoreJPanel.add(test1Label);
inputScoreJPanel.add(test2Label);
inputScoreJPanel.add(test3Label);
inputScoreJPanel.add(studentNameTextField);
inputScoreJPanel.add(test1TextField);
inputScoreJPanel.add(test2TextField);
inputScoreJPanel.add(test3TextField);
inputScoreJPanel.setBorder(inputTitledBorder);
inputScoreJPanel.add(submitGradesJButton);
return inputScoreJPanel;
}
public StudentGrades(){
createGUI();
}
private void createGUI(){
gradesFormat = new DecimalFormat("0.00");
Container contentPane = getContentPane();
contentPane.setLayout(null);
ButtonGroup buttonGroup = new ButtonGroup();
numericJRadioButton = new JRadioButton();
numericJRadioButton.setText("numeric");
numericJRadioButton.setBounds(50,250,80,25);
numericJRadioButton.setSelected(true);
numericJRadioButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
numericJRadioButtonactionPerformed(e);
}
});
letterJRadioButton = new JRadioButton();
letterJRadioButton.setText("letter");
letterJRadioButton.setBounds(140,250,80,25);
letterJRadioButton.setSelected(false);
letterJRadioButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
letterJRadioButtonactionPerformed(e);
}
});
buttonGroup.add(letterJRadioButton);
buttonGroup.add(numericJRadioButton);
averageLabel = new JLabel();
averageLabel.setText("class average:");
averageLabel.setBounds(450,250,100,25);
averageLabel.setHorizontalAlignment((int) numericJRadioButton.TOP_ALIGNMENT);
averageTextField = new JTextField();
averageTextField.setBounds(555,250,80,25);
averageTextField.setEditable(false);
setBounds(300,250,690,330);
contentPane.add(getInputScoreJPanel());
contentPane.add(getAverageScoreJPanel());
contentPane.add(numericJRadioButton);
contentPane.add(letterJRadioButton);
contentPane.add(averageLabel);
contentPane.add(averageTextField);
setTitle("Average Student Grades");
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
private void numericJRadioButtonactionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
studentScoreTextArea.setText("Name\tTest 1\tTest 2\tTest 3\tAverage\n");
for(int i=0; i
String str = strudentNames[i]+"\t"+studentGrades[i][0]+"\t"
+studentGrades[i][1]+"\t"+studentGrades[i][2]+"\t"
+gradesFormat.format(studentGrades[i][3])+"\n";
if(strudentNames[i] != null){
System.out.println(str);
}
studentScoreTextArea.append(str);
String t =""+ totalGrades/classCounter;
averageTextField.setText(t);
}
}
private void letterJRadioButtonactionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
studentScoreTextArea.setText("Name\tTest 1\tTest 2\tTest 3\tAverage\n");
for(int i=0; i
String str = strudentNames[i]+"\t"+gradesLevel(studentGrades[i][0])+"\t"
+gradesLevel(studentGrades[i][1])+"\t"
+gradesLevel(studentGrades[i][2])+"\t"
+gradesLevel(studentGrades[i][3])+"\n";
if(strudentNames[i] != null){
System.out.println(str);
}
studentScoreTextArea.append(str);
String t =""+ gradesLevel(totalGrades/classCounter);
averageTextField.setText(t);
}
}
private void submitGradesJButtonactionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String strName = studentNameTextField.getText();
String strTest1 = test1TextField.getText();
String strTest2 = test2TextField.getText();
String strTest3 = test3TextField.getText();
double test1Grade = Double.valueOf(strTest1);
double test2Grade = Double.valueOf(strTest2);
double test3Grade = Double.valueOf(strTest3);
double averageGrade = (test1Grade+test2Grade+test3Grade)/NUMBER_OF_TESTS;
strudentNames[classCounter] = strName;
studentGrades[classCounter][0] = test1Grade;
studentGrades[classCounter][1] = test2Grade;
studentGrades[classCounter][2] = test3Grade;
studentGrades[classCounter][3] = averageGrade;
// String strAverage = ""+averageGrade;
String str = strName+"\t"+strTest1+"\t"+strTest2
+"\t"+strTest3+"\t"+gradesFormat.format(averageGrade)+"\n";
studentScoreTextArea.append(str);
totalGrades += averageGrade;
classCounter++;
// String t =""+ totalGrades/classCounter;
averageTextField.setText(gradesFormat.format(totalGrades/classCounter));
if(classCounter == MAXIMUM_STUDENTS){
submitGradesJButton.setEnabled(false);
}
}
private char gradesLevel(double grade){
if(grade>89 && grade<=100){
return 'A';
}
if(grade>79 && grade<90){
return 'B';
}
if(grade>69 && grade<80){
return 'C';
}
if(grade>59 && grade<70){
return 'D';
}
if(grade<60){
return 'E';
}
return 'F';
}
public static void main(String args[]){
new StudentGrades();
}
}