I am currently working on a GUI calculator in NetBeans. I have my addition operator working well, but as for my subtraction, multiplication, and division it returns wrong values. I don't think I have problem in my logic with the calculation.
Value that the operator returns:
Subtraction: sum of the input values with negative sign
Multiplication 0
Division 0
Here are my buttons and functions for the operators:
// add button
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
operator = "+";
calculation(operator);
}
// subtract button
private void btnSubtractActionPerformed(java.awt.event.ActionEvent evt) {
operator = "-";
calculation(operator);
}
// multiply button
private void btnMultiplyActionPerformed(java.awt.event.ActionEvent evt) {
operator = "*";
calculation(operator);
}
// divide button
private void btnDivideActionPerformed(java.awt.event.ActionEvent evt) {
operator = "/";
calculation(operator);
}
// equals button
private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {
calculation(operator);
txtInput.setText(String.valueOf(accum2));
}
// calculation method
public void calculation(String operator){
if(operator.equals("+")) {
accum2 += accum1;
accum1 = 0;
} else if(operator.equals("-")){
accum2 -= accum1;
accum1 = 0;
} else if(operator.equals("*")){
accum2 *= accum1;
accum1 = 0;
} else if(operator.equals("/")){
accum2 /= accum1;
accum1 = 0;
} else {
accum2 = accum1;
accum1 = 0;
}
}