Hi everyone,
Found a code listing from www.yamaza.com/java/Calc.java that helps me understand a better way of programming a calculator. Trying to understand this code. Can anyone tell me what does the boolean isFixReg() below mean? Thanks in advance.
//*******************************
// Java script of Calculator
// << Calc.java >>
//===============================
// Ver 0.0 96/01/19 T.Yamazaki
//-------------------------------
// email : [email]yamaza@st.rim.or.jp[/email]
//*******************************
import java.awt.*;
import java.lang.*;
import java.applet.Applet;
//===================
// Calculator Applet
//===================
public class Calc extends java.applet.Applet
{
//--------
// member
//--------
TextField text;
String sText1, sText2;
double dReg1, dReg2, dMem;
String sOperator;
boolean isFixReg;
//-------------
// constructor
//-------------
public Calc()
{
Panel pFrame = new Panel();
pFrame.setLayout(new FlowLayout());
text = new TextField("");
text.setForeground(Color.yellow);
text.setEditable(false);
Panel pCalc = new Panel();
pCalc.setLayout(new BorderLayout(0, 10));
pCalc.add("North", text);
pFrame.add("Center", pCalc);
Dimension dSize= pCalc.size();
dSize.width = dSize.width + 20;
dSize.height = dSize.height + 20;
pFrame.resize(dSize);
Panel pKey = new Panel();
pKey.setLayout(new GridLayout(5, 4, 5, 5));
add("Center", pKey);
pKey.add(new Button("C"));
pKey.add(new Button("MR"));
pKey.add(new Button("M-"));
pKey.add(new Button("M+"));
pKey.add(new Button("7"));
pKey.add(new Button("8"));
pKey.add(new Button("9"));
pKey.add(new Button("/"));
pKey.add(new Button("4"));
pKey.add(new Button("5"));
pKey.add(new Button("6"));
pKey.add(new Button("*"));
pKey.add(new Button("1"));
pKey.add(new Button("2"));
pKey.add(new Button("3"));
pKey.add(new Button("-"));
pKey.add(new Button("0"));
pKey.add(new Button("."));
pKey.add(new Button("="));
pKey.add(new Button("+"));
pCalc.add("South", pKey);
setLayout(new BorderLayout(0, 0));
add("North", pFrame);
setBackground(Color.darkGray);
dReg1 = 0.0d;
dReg2 = 0.0d;
dMem = 0.0d;
sOperator = "";
text.setText("0");
isFixReg = true; //What does this do?What is it for?It's used below.
}
//---------------
// event handler
//---------------
public boolean action(Event evt, Object arg)
{
//
// numeric key input
//
if ("C".equals(arg))
{
dReg1 = 0.0d;
dReg2 = 0.0d;
sOperator = "";
text.setText("0");
isFixReg = true;
}
else if (("0".equals(arg)) | ("1".equals(arg)) | ("2".equals(arg))
| ("3".equals(arg)) | ("4".equals(arg)) | ("5".equals(arg))
| ("6".equals(arg)) | ("7".equals(arg)) | ("8".equals(arg))
| ("9".equals(arg)) | (".".equals(arg)))
{
if (isFixReg)
sText2 = (String) arg;
else
sText2 = text.getText() + arg;
text.setText(sText2);
isFixReg = false;
}
//
// operations
//
else if (("+".equals(arg)) | ("-".equals(arg))
| ("*".equals(arg)) | ("/".equals(arg)) | ("=".equals(arg)))
{
sText1 = text.getText();
dReg2 = (Double.valueOf(sText1)).doubleValue();
dReg1 = Calculation(sOperator, dReg1, dReg2);
Double dTemp = new Double(dReg1);
sText2 = dTemp.toString();
text.setText(sText2);
sOperator = (String) arg;
isFixReg = true;
}
//
// memory read operation
//
else if ("MR".equals(arg))
{
Double dTemp = new Double(dMem);
sText2 = dTemp.toString();
text.setText(sText2);
sOperator = "";
isFixReg = true;
}
//
// memory add operation
//
else if ("M+".equals(arg))
{
sText1 = text.getText();
dReg2 = (Double.valueOf(sText1)).doubleValue();
dReg1 = Calculation(sOperator, dReg1, dReg2);
Double dTemp = new Double(dReg1);
sText2 = dTemp.toString();
text.setText(sText2);
dMem = dMem + dReg1;
sOperator = "";
isFixReg = true;
}
//
// memory sub operation
//
else if ("M-".equals(arg))
{
sText1 = text.getText();
dReg2 = (Double.valueOf(sText1)).doubleValue();
dReg1 = Calculation(sOperator, dReg1, dReg2);
Double dTemp = new Double(dReg1);
sText2 = dTemp.toString();
text.setText(sText2);
dMem = dMem - dReg1;
sOperator = "";
isFixReg = true;
}
return true;
}
//-------------
// Calculation
//-------------
private double Calculation(String sOperator, double dReg1, double dReg2)
{
if ("+".equals(sOperator)) dReg1 = dReg1 + dReg2;
else if ("-".equals(sOperator)) dReg1 = dReg1 - dReg2;
else if ("*".equals(sOperator)) dReg1 = dReg1 * dReg2;
else if ("/".equals(sOperator)) dReg1 = dReg1 / dReg2;
else dReg1 = dReg2;
return dReg1;
}
}