patternjavaMinor
Basic Calculator using Java Swing and AWT
Viewed 0 times
awtjavaswingusingandcalculatorbasic
Problem
I created a basic desktop calculator using Java. Does anyone have any suggestions as far as coding best practices, readability issues, or just generic coding mistakes I might have missed? Any professional input you could provide would be greatly appreciated.
Calculator.java
CalculatorGUI.java
```
package calc;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class CalculatorGUI extends JFrame implements ActionListener, KeyListener {
private static final long serialVersionUID = 1L;
private Integer [] placeholder = new Integer[15];
private long placeholderBuilder;
private double numberOne = 0;
private double numberTwo = 0;
private double total = 0;
private String operand;
private boolean positiveNumberSwitch = true;
private int testingConsoleDisplayCounter = 1;
private long beforeDecimal;
private long afterDecimal;
private boolean isDecimal;
private GridBagLayout layout = new GridBagLayout();
private JLabel displayLabel = new JLabel("0");
private JButton nineButton = new JButton("9");
private JButton eightButton = new JButton("8");
private JButton sevenButton = new JButton("7");
private JButton sixButton = new JButton("6");
private JButton fiveButton = new JBut
Calculator.java
package calc;
import javax.swing.JFrame;
public class Calculator {
public static void main(String[] args) {
CalculatorGUI window = new CalculatorGUI("Basic Calculator");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300, 400);
window.setFocusable(true);
window.setVisible(true);
}
}CalculatorGUI.java
```
package calc;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class CalculatorGUI extends JFrame implements ActionListener, KeyListener {
private static final long serialVersionUID = 1L;
private Integer [] placeholder = new Integer[15];
private long placeholderBuilder;
private double numberOne = 0;
private double numberTwo = 0;
private double total = 0;
private String operand;
private boolean positiveNumberSwitch = true;
private int testingConsoleDisplayCounter = 1;
private long beforeDecimal;
private long afterDecimal;
private boolean isDecimal;
private GridBagLayout layout = new GridBagLayout();
private JLabel displayLabel = new JLabel("0");
private JButton nineButton = new JButton("9");
private JButton eightButton = new JButton("8");
private JButton sevenButton = new JButton("7");
private JButton sixButton = new JButton("6");
private JButton fiveButton = new JBut
Solution
This isn't a full review (yet), but some hopefully helpful comments.
-
You should always start your Swing application by calling
-
Your calculator would be able to support longer equations in a much simpler fashion if you instead placed your symbols in a stack. Then you'd be able to support an arbitrary number of statements, plus parentheticals, and would find your total by just popping each successive element from the stack and applying the appropriate operation (if any). This would make the
-
Using a
-
You don't have any checks against invalid operations. What if a user enters
-
Instead of the
-
I'm not understanding the purpose of the
-
You should always start your Swing application by calling
SwingUtilities.invokeLater. This Oracle/Sun tutorial explains why very well.-
Your calculator would be able to support longer equations in a much simpler fashion if you instead placed your symbols in a stack. Then you'd be able to support an arbitrary number of statements, plus parentheticals, and would find your total by just popping each successive element from the stack and applying the appropriate operation (if any). This would make the
functionForMultipuleEntryPastTwoNumbers() obsolete since it would support any number of numbers and operands.-
Using a
double for the numberOne, numberTwo, and total variables is inviting a loss of precision. Consider float BigDecimal instead.-
You don't have any checks against invalid operations. What if a user enters
2 / 0? Or if they enter a number that's too long to store in a double or the placeholder array?-
Instead of the
testingStringOutputToConsole() method, you may want to consider writing unit tests using a framework like JUnit to verify your functionality.-
I'm not understanding the purpose of the
placeholder array. Maybe, instead have a String for the entered value? Then when the user enters a symbol, use Double.parseDouble or Integer.parseInt as appropriate to convert the previously entered value to the appropriate numeric type. Right now, if you enter 1.02, your method to convert that to a number would result in 1.2 because it converts the digits prior to the decimal to an int (1) and then the digits after the decimal to an int (2), builds a new string (1.2), and parses the decimal.Context
StackExchange Code Review Q#56718, answer score: 6
Revisions (0)
No revisions yet.