import java.lang.*;
import java.awt.event.*;
private void ButtonActionPerformed(java.awt.event.ActionEvent
event)
{
String tmp = event.getActionCommand(); //
Returns the command string associated with this action.
try
{
String
c=String.valueOf(Integer.parseInt(tmp)); // Returns the string
representation of the int argument
if (
c.compareTo("0")==0) // if c equal to "0"
{
if (v.length()>0) // if c equal to "0" AND v.length greater than
0
{
v = v.concat(c); // v = v + "0"
}
}
else
{
v =
v.concat(c); // v = v + c
}
}
catch (NumberFormatException ex) // this section
will not be executed unless line 6 creates a
NumberFormatException
{
if
(tmp.compareTo(".")==0) // if tmp is equal "."
{
if (!(v.contains("."))) // if v does not have "."
{
if (v.length()>0) // if v does not have "." AND v.length greater
than 0
{
v = v.concat(tmp); // v = v + "."
}
else // if v does not have "." AND v.length is equal 0
{
v = "0".concat(tmp); // v = "0" + "."
}
}
}
else if
(tmp.compareTo("C")==0) // if tmp is equal "C"
{
v=""; // v is null
}
else if
(tmp.compareTo("+/-")==0) // if tmp is equal "+/-"
{
if ( v.charAt(0)=='-' ) // if tmp is equal "+/-" AND v first char
is equal "-"
{
v = v.substring(1); // removes the "-" from string
}
else // if
tmp is equal "+/-" AND v first char is not equal "-"
{
v = "-".concat(v); // v = "-" + v
}
}
else if
(tmp.compareTo("=")==0) // if tmp is equal "="
{
double a = Double.parseDouble(m);
double b = Double.parseDouble(v);
if(op.compareTo("+")==0) // if op is addition
{
v = String.valueOf(a+b);
}
else if
(op.compareTo("-")==0) // if op is subtraction
{
v = String.valueOf(a-b);
}
else if
(op.compareTo("*")==0) // if op is multiplication
{
v = String.valueOf(a*b);
}
else if
(op.compareTo("/")==0) // if op is division
{
v = String.valueOf(a/b);
}
else if
(op.compareTo("%")==0) // if op is percentage
{
v = String.valueOf(a/100); // MISTAKE->percentage should be
(a*(b/100))
}
m = ""; m
is null
op = "";
op is null
}
else if
(tmp.compareTo("BackSpace")==0) // if tmp is equal
"BackSpace"
{
if (v.length()>0) // if tmp is equal "BackSpace" AND v.length
greater than 0
{
v = v.substring(0,(v.length()-1)); // removes the right most
digit
}
}
else // if tmp is not equal
"." AND not "C" AND not "+/-" AND not "="
{
m=v;
v=""; v is
null
op=tmp;
}
}
jTextField1.setText(v); // always show v in
textbox
}