I am creating a program were the user enters their grade of all their assignment and the weight of each individual assignment and the program calculates the aggregate grade.
Nearly completed the program with loads of help from here/internet but I have no idea how to do this last section.
So when user clicks enter the program will aggregate all marks taking into account their specific weight and output an average. For example:
The equation for something like this is weight/100*mark. (Adding them all together). So... (40/100)*60 + (40/100)*80... etc
So how can I do this with my program? I need to implement some type of for loop because the number of assignments changes all the time.
I have already implemented an actionlistener on the Enter button were the Mark/Weight/Assignment counter gets passed on to the next class.
However the actionlistener only records the last mark/weight. so 50 and 20 in the example above. This is another problem I'm facing.
So basically I ask for a way to get inputs from all JTextField and analyse them to create one output which aggregates all the scores. (Considering their weight)
Code:
public class test{
public JButton button;
private static final Insets normalInsets = new Insets(10,10,0,10);
private static final Insets finalInsets = new Insets(10,10,10,10);
private static JPanel createMainPanel(){
GridBagConstraints gbc = new GridBagConstraints();
//Adding the JPanels. Panel for instructions
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
int gridy = 1;
//JLabel for the Instructions
JTextArea instructionTextArea = new JTextArea(5, 30);
instructionTextArea.setEditable(false);
instructionTextArea.setLineWrap(true);
instructionTextArea.setWrapStyleWord(true);
JScrollPane instructionScrollPane = new JScrollPane(instructionTextArea);
addComponent(panel, instructionScrollPane, 0, gridy++, 3, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
//JLabels for Assignment
JLabel label1 = new JLabel("Assignment");
label1.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, label1, 0, gridy, 1, 1, finalInsets,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL);
//JLabel for Mark
JLabel label2 = new JLabel("Mark");
label2.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, label2, 1, gridy, 1, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
//JLabel for Weight.
JLabel label3 = new JLabel("Weight");
label3.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, label3, 2, gridy++, 1, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
//JLabel Number for the number list of assignments at the side.
gbc.gridy = 3;
for(int i = 1; i<=3; i++){
String kok = String.valueOf(i);
JLabel number = new JLabel(kok);
number.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, number, 0, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
}
gbc.gridy =3;
//JTextfield for Mark
for(int i=0; i<3; i++){
JTextField mark = new JTextField(20);
mark.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, mark, 1, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.NONE);
}
gbc.gridy = 3;
//JTextfield for Weight
for(int i=0; i<3; i++){
JTextField weight = new JTextField(20);
weight.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, weight, 2, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.NONE);
}
JButton button = new JButton("Enter");
button.setHorizontalAlignment(SwingConstants.CENTER);
addComponent(panel, button, 3, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.CENTER);
return panel;
}
private static void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, Insets insets, int anchor, int fill ){
GridBagConstraints grid = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0D,1.0D,anchor,fill,insets,0,0);
container.add(component,grid);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel());
frame.pack();
frame.setVisible(true);
new test();
}
The next class that has the mark/weight info.
public class test1 implements ActionListener {
private final JTextField mark;
private final JTextField weight;
public test1(JTextField mark, JTextField weight) {
this.mark = mark;
this.weight = weight;
}
@Override
public void actionPerformed(ActionEvent e) {
String markstring = mark.getText();
String weightstring = weight.getText();
double markdouble = Double.parseDouble(markstring);
double weightdouble = Double.parseDouble(weightstring);
}
JTextFields
in a vector, so you can access them later.The listener for your button was also created.
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Test {
public JButton button;
private static final Insets normalInsets = new Insets(10,10,0,10);
private static final Insets finalInsets = new Insets(10,10,10,10);
public final static int NUM_OF_FIELDS = 3;
private static JTextField[] markTextFields = new JTextField[NUM_OF_FIELDS];
private static JTextField[] weightTextFields = new JTextField[NUM_OF_FIELDS];
private static JPanel createMainPanel() {
GridBagConstraints gbc = new GridBagConstraints();
//Adding the JPanels. Panel for instructions
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
int gridy = 1;
//JLabel for the Instructions
JTextArea instructionTextArea = new JTextArea(5, 30);
instructionTextArea.setEditable(false);
instructionTextArea.setLineWrap(true);
instructionTextArea.setWrapStyleWord(true);
JScrollPane instructionScrollPane = new JScrollPane(instructionTextArea);
addComponent(panel, instructionScrollPane, 0, gridy++, 3, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
//JLabels for Assignment
JLabel label1 = new JLabel("Assignment");
label1.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, label1, 0, gridy, 1, 1, finalInsets,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL);
//JLabel for Mark
JLabel label2 = new JLabel("Mark");
label2.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, label2, 1, gridy, 1, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
//JLabel for Weight.
JLabel label3 = new JLabel("Weight");
label3.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, label3, 2, gridy++, 1, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
//JLabel Number for the number list of assignments at the side.
gbc.gridy = 3;
for (int i = 1; i <= 3; i++) {
String kok = String.valueOf(i);
JLabel number = new JLabel(kok);
number.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, number, 0, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
}
gbc.gridy = 3;
//JTextfield for Mark
for (int i = 0; i < NUM_OF_FIELDS; i++) {
JTextField mark = new JTextField(20);
markTextFields[i] = mark;
mark.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, mark, 1, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER, GridBagConstraints.NONE);
}
gbc.gridy = 3;
//JTextfield for Weight
for (int i = 0; i < NUM_OF_FIELDS; i++) {
JTextField weight = new JTextField(20);
weightTextFields[i] = weight;
weight.setHorizontalAlignment(JLabel.CENTER);
addComponent(panel, weight, 2, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.NONE);
}
JButton button = new JButton("Enter");
button.setHorizontalAlignment(SwingConstants.CENTER);
addComponent(panel, button, 3, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.CENTER);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double finalScore = 0;
for (int i = 0; i < NUM_OF_FIELDS; i++) {
double weight = Double.parseDouble(weightTextFields[i].getText());
double mark = Double.parseDouble(markTextFields[i].getText());
finalScore += weight / 100 * mark;
}
JOptionPane.showMessageDialog(null, "Final score: " + finalScore, "Final Score", JOptionPane.OK_OPTION);
}
});
return panel;
}
private static void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, Insets insets, int anchor, int fill) {
GridBagConstraints grid = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
container.add(component, grid);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel());
frame.pack();
frame.setVisible(true);
new Test();
}
}