This is a little assignment I am working on. I am a beginner Java programmer and would love some advice on how to improve this code.
For more context, here are the assignment details:
Create a complete program that has the ability to store and display integer values in an array. The maximum number of values that your program should be able to handle is 10.
- Add additional elements to the array using screen input (textbox and button).
- Remove array elements based on screen input (textbox and button).
- List all of the elements in the array and compute the sum of all the elements.
- List the even elements in the array and compute the sum of the even elements.
- List the odd elements in the array and compute the sum of the odd elements.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
public class sumElement implements ActionListener {
public static JButton add, remove, sumAll, sumEven, sumOdd;
public static JTextField inputField;
public static JTextArea textArea;
public static int counter = 0;
public static int[] numbers = new int[10];
public static JLabel titleText;
public static void main(String[] args) {
// Frame
JFrame frame = new JFrame("Integer Sums");
frame.setSize(178, 240);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Container panel
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
frame.setContentPane(container);
// Title panel
JPanel titlePane = new JPanel();
titleText = new JLabel("Integer Sums");
// Content panel
JPanel content = new JPanel();
content.setPreferredSize(new Dimension(300, 180));
content.setLayout(null);
// Buttons
add = new JButton("Add");
add.setBounds(4, 50, 80, 20);
add.addActionListener(new sumElement());
remove = new JButton("Remove");
remove.setBounds(88, 50, 80, 20);
remove.addActionListener(new sumElement());
sumAll = new JButton("Sum All");
sumAll.setBounds(4, 74, 164, 20);
sumAll.addActionListener(new sumElement());
sumEven = new JButton("Sum Even");
sumEven.setBounds(4, 98, 164, 20);
sumEven.addActionListener(new sumElement());
sumOdd = new JButton("Sum Odd");
sumOdd.setBounds(4, 122, 164, 20);
sumOdd.addActionListener(new sumElement());
// Text area, and text field
inputField = new JTextField();
inputField.setBounds(4, 21, 164, 25);
JLabel inputLabel = new JLabel("Input integer value below:");
inputLabel.setBounds(12, 6, 164, 13);
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBounds(4, 144, 165, 36);
// Adding everything
container.add(titlePane);
titlePane.add(titleText);
container.add(content);
content.add(inputLabel);
content.add(inputField);
content.add(scrollPane);
content.add(add);
content.add(remove);
content.add(sumAll);
content.add(sumEven);
content.add(sumOdd);
// Extras
frame.toFront();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equals("Add")) {
if (counter == 10) {
titleText.setText("Error: too many values");
} else {
numbers[counter] = Integer.parseInt(inputField.getText());
textArea.setText(Arrays.toString(numbers));
counter++;
}
} else if (event.getActionCommand().equals("Remove")) {
for (int i = 0; i <= counter; i++) {
if (Integer.parseInt(inputField.getText()) == numbers[i]) {
for (int x = i; x <= counter; x++) {
numbers[x] = numbers[x + 1];
}
}
}
counter--;
textArea.setText(Arrays.toString(numbers));
} else if (event.getActionCommand().equals("Sum All")) {
int sum = 0;
for (int i = 0; i <= counter; i++) {
sum += numbers[i];
}
titleText.setText("Sum is " + sum);
} else if (event.getActionCommand().equals("Sum Even")) {
int sum = 0;
for (int i = 0; i <= counter; i++) {
if (numbers[i] % 2 == 0) {
sum += numbers[i];
}
}
titleText.setText("Sum of even values is: " + sum);
} else if (event.getActionCommand().equals("Sum Odd")) {
int sum = 0;
for (int i = 0; i <= counter; i++) {
if (numbers[i] % 2 != 0) {
sum += numbers[i];
}
}
titleText.setText("Sum of odd values is: " + sum);
}
}
}
public class sumElement implements ActionListener
\$\endgroup\$ – Simon Forsberg Oct 22 '16 at 9:09