-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse_grade_calculator.java
More file actions
205 lines (182 loc) · 7.22 KB
/
Copy pathcourse_grade_calculator.java
File metadata and controls
205 lines (182 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* A Java GUI application to calculate a course grade based on user-provided
* marks and weightage for assignments, midterms, and finals.
*/
public class CourseGradeCalculator extends JFrame {
// GUI components
private final JTextField assignmentMarksField;
private final JTextField assignmentWeightField;
private final JTextField midtermMarksField;
private final JTextField midtermWeightField;
private final JTextField finalMarksField;
private final JTextField finalWeightField;
private final JButton calculateButton;
private final JTextArea resultArea;
/**
* Constructor to set up the GUI frame and its components.
*/
public CourseGradeCalculator() {
// Set up the main frame
setTitle("Course Grade Calculator");
setSize(450, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // Center the frame on the screen
// Use GridBagLayout for a more structured and professional layout
JPanel mainPanel = new JPanel(new GridBagLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
// -- Input Fields and Labels --
// Header
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 3;
gbc.anchor = GridBagConstraints.CENTER;
mainPanel.add(new JLabel("<html><b style='font-size:14px;'>Course Grade Calculator</b></html>"), gbc);
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridy++;
mainPanel.add(new JLabel("Category"), gbc);
gbc.gridx = 1;
mainPanel.add(new JLabel("Marks (%)"), gbc);
gbc.gridx = 2;
mainPanel.add(new JLabel("Weightage (%)"), gbc);
// Assignments row
gbc.gridx = 0;
gbc.gridy++;
mainPanel.add(new JLabel("Assignments"), gbc);
gbc.gridx = 1;
assignmentMarksField = new JTextField(5);
mainPanel.add(assignmentMarksField, gbc);
gbc.gridx = 2;
assignmentWeightField = new JTextField(5);
mainPanel.add(assignmentWeightField, gbc);
// Midterms row
gbc.gridx = 0;
gbc.gridy++;
mainPanel.add(new JLabel("Midterms"), gbc);
gbc.gridx = 1;
midtermMarksField = new JTextField(5);
mainPanel.add(midtermMarksField, gbc);
gbc.gridx = 2;
midtermWeightField = new JTextField(5);
mainPanel.add(midtermWeightField, gbc);
// Finals row
gbc.gridx = 0;
gbc.gridy++;
mainPanel.add(new JLabel("Finals"), gbc);
gbc.gridx = 1;
finalMarksField = new JTextField(5);
mainPanel.add(finalMarksField, gbc);
gbc.gridx = 2;
finalWeightField = new JTextField(5);
mainPanel.add(finalWeightField, gbc);
// -- Calculation Button --
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = 3;
calculateButton = new JButton("Calculate Grade");
mainPanel.add(calculateButton, gbc);
// -- Result Area --
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = 3;
resultArea = new JTextArea(8, 20);
resultArea.setEditable(false);
resultArea.setMargin(new Insets(5, 5, 5, 5));
resultArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
mainPanel.add(new JScrollPane(resultArea), gbc);
add(mainPanel);
// Add action listener to the button
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
calculateGrade();
}
});
}
/**
* Calculates the final grade based on the input values.
*/
private void calculateGrade() {
try {
// Get marks and weights from text fields
double assignmentMarks = Double.parseDouble(assignmentMarksField.getText());
double assignmentWeight = Double.parseDouble(assignmentWeightField.getText());
double midtermMarks = Double.parseDouble(midtermMarksField.getText());
double midtermWeight = Double.parseDouble(midtermWeightField.getText());
double finalMarks = Double.parseDouble(finalMarksField.getText());
double finalWeight = Double.parseDouble(finalWeightField.getText());
// Validate weights
if (assignmentWeight + midtermWeight + finalWeight != 100) {
resultArea.setText("Error: Total weightage must be 100%.");
return;
}
// Calculate weighted average
double totalPercentage =
(assignmentMarks * (assignmentWeight / 100)) +
(midtermMarks * (midtermWeight / 100)) +
(finalMarks * (finalWeight / 100));
// Determine grade and grade point
String grade = getLetterGrade(totalPercentage);
double gradePoint = getGradePoint(totalPercentage);
// Display results
resultArea.setText(
"Calculation Results:\n" +
"--------------------\n" +
String.format("Total Percentage: %.2f%%\n", totalPercentage) +
String.format("Letter Grade: %s\n", grade) +
String.format("Grade Point: %.2f\n", gradePoint)
);
} catch (NumberFormatException ex) {
resultArea.setText("Error: Please enter valid numbers in all fields.");
}
}
/**
* Determines the letter grade based on the total percentage.
* @param percentage The total calculated percentage.
* @return The corresponding letter grade as a String.
*/
private String getLetterGrade(double percentage) {
if (percentage >= 80) return "A+";
if (percentage >= 75) return "A";
if (percentage >= 70) return "A-";
if (percentage >= 65) return "B+";
if (percentage >= 60) return "B";
if (percentage >= 55) return "B-";
if (percentage >= 50) return "C+";
if (percentage >= 45) return "C";
if (percentage >= 40) return "C-";
return "F";
}
/**
* Determines the grade point based on the total percentage.
* @param percentage The total calculated percentage.
* @return The corresponding grade point as a double.
*/
private double getGradePoint(double percentage) {
if (percentage >= 80) return 4.00;
if (percentage >= 75) return 3.75;
if (percentage >= 70) return 3.50;
if (percentage >= 65) return 3.25;
if (percentage >= 60) return 3.00;
if (percentage >= 55) return 2.75;
if (percentage >= 50) return 2.50;
if (percentage >= 45) return 2.25;
if (percentage >= 40) return 2.00;
return 0.00;
}
/**
* Main method to run the application.
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new CourseGradeCalculator().setVisible(true);
});
}
}