0% found this document useful (0 votes)
1 views4 pages

Final Java Project

The document is a Java program that creates a graphical user interface (GUI) for a Grade Weighted Average (GWA) calculator using Swing. It allows users to add subjects with corresponding grades and units, and calculates the GWA based on the input. The program includes error handling for invalid inputs and displays the result in the GUI.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

Final Java Project

The document is a Java program that creates a graphical user interface (GUI) for a Grade Weighted Average (GWA) calculator using Swing. It allows users to add subjects with corresponding grades and units, and calculates the GWA based on the input. The program includes error handling for invalid inputs and displays the result in the GUI.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

import javax.swing.

*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class GWACalculatorGUI extends JFrame {


private JPanel subjectPanel;
private JButton addButton, calculateButton;
private JLabel resultValueLabel;
private ArrayList<JTextField> gradeFields = new ArrayList<>();
private ArrayList<JTextField> unitFields = new ArrayList<>();
private int subjectCount = 0;

public GWACalculatorGUI() {
setTitle("GWA Calculator");
setSize(450, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

subjectPanel = new JPanel();


subjectPanel.setLayout(new BoxLayout(subjectPanel,
BoxLayout.Y_AXIS));
JScrollPane scrollPane = new JScrollPane(subjectPanel);
scrollPane.setBorder(new LineBorder(Color.GREEN, 5));

addButton = new JButton("Add Subject");


calculateButton = new JButton("Calculate GWA");
addButton.setBorder(new LineBorder(Color.CYAN, 3));
calculateButton.setBorder(new LineBorder(Color.cyan, 3));
addButton.addActionListener(e -> addSubjectFields());
calculateButton.addActionListener(e -> calculateGWA());

JPanel buttonPanel = new JPanel(new


FlowLayout(FlowLayout.CENTER));
buttonPanel.add(addButton);
buttonPanel.add(calculateButton);
buttonPanel.setBorder(new LineBorder(Color.BLUE, 3));

JPanel resultPanel = new JPanel();

TitledBorder titledBorder = BorderFactory.createTitledBorder("GWA


Result");
titledBorder.setTitleFont(new Font("Arial", Font.BOLD, 16));
titledBorder.setTitleJustification(TitledBorder.CENTER);
resultPanel.setBorder(titledBorder);
resultPanel.setPreferredSize(new Dimension(400, 80));
resultValueLabel = new JLabel("");
resultValueLabel.setFont(new Font("Arial", Font.BOLD, 16));
resultPanel.add(resultValueLabel);

JPanel mainPanel = new JPanel(new BorderLayout(10, 10));


mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10,
10));
mainPanel.add(scrollPane, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
mainPanel.add(resultPanel, BorderLayout.NORTH);
mainPanel.setBorder(new LineBorder(Color.BLUE, 3));
add(mainPanel);

addSubjectFields();
}

private void addSubjectFields() {


subjectCount++;

JPanel row = new JPanel(new FlowLayout(FlowLayout.LEFT));


row.setBorder(new LineBorder(Color.BLUE, 2)); // Blue border for
each subject row
JLabel subjectLabel = new JLabel("Subject " + subjectCount +
":");

JTextField gradeField = new JTextField(5);


gradeField.setBorder(new LineBorder(Color.BLUE, 2)); // Orange
border for grade field
JTextField unitField = new JTextField(5);
unitField.setBorder(new LineBorder(Color.BLUE, 2)); // Pink
border for unit field

row.add(subjectLabel);
row.add(new JLabel("Grade:"));
row.add(gradeField);
row.add(new JLabel("Units:"));
row.add(unitField);

subjectPanel.add(row);
subjectPanel.revalidate();

gradeFields.add(gradeField);
unitFields.add(unitField);
}

private void calculateGWA() {


double totalWeightedGrades = 0;
int totalUnits = 0;

for (int i = 0; i < gradeFields.size(); i++) {


String gradeText = gradeFields.get(i).getText().trim();
String unitText = unitFields.get(i).getText().trim();

if (gradeText.isEmpty() || unitText.isEmpty()) {
resultValueLabel.setText("You have a missing grade or
unit. Your GWA can't be calculated.");
return;
}

try {
Double.parseDouble(gradeText);
Integer.parseInt(unitText);
} catch (NumberFormatException ex) {
resultValueLabel.setText("Please enter valid numbers for
all grades and units.");
return;
}
}
try {
for (int i = 0; i < gradeFields.size(); i++) {
String gradeText = gradeFields.get(i).getText().trim();
String unitText = unitFields.get(i).getText().trim();

double grade = Double.parseDouble(gradeText);


int units = Integer.parseInt(unitText);
totalWeightedGrades += grade * units;
totalUnits += units;
}

if (totalUnits == 0) {
resultValueLabel.setText("Total units cannot be zero.");
return;
}

double gwa = totalWeightedGrades / totalUnits;


resultValueLabel.setText(String.format("Your GWA is: %.2f",
gwa));
} catch (NumberFormatException ex) {
resultValueLabel.setText("Please enter valid numbers.");
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
GWACalculatorGUI calculator = new GWACalculatorGUI();
calculator.setVisible(true);
});
}
}

You might also like