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;
}
}

It looks to me like it is a flag to tell whether something has been inputted or not. If it hasn't, isFixReg is set to true, and pressing a number (for example) starts a new String. Otherwise, isFixReg is false and entering a number will add to the existing text.

It might pay to check this with T.Yamazaki via the email address that is provided, but since the code was written over 11 years ago you may not get a response from that address.

As an aside, this is why it is important to provide meaningful names and/or comments for methods, variables, classes etc. Just because the author of the code knows what it means doesn't mean that they will always be responsible for maintenance of the code.

commented: Nice post :) +2

This is your first post. But, the next time you post your code, please use code tags so that the users can view the code along with proper alignment.

if (isFixReg)
    sText2 = (String) arg;
else 
    sText2 = text.getText() + arg;
text.setText(sText2);
isFixReg = false;

This indicates that if isFixReg is true, then no other operand (argument) is needed to be displayed (For eg when memory/clear buttons or operators are selected). If isFixReg is false, then another argument is to be added to the text being displayed.

For eg if you press 21, then when you press 2 then isFixReg is true and afterwards when you press 1 then isFixReg is false because it has to display the previous argument (which was 2) also to display '21' and not only '1'. I hope I'm clear.

Looks like darkagn and I replied at about the same time. I hadn't refreshed my page while typing my reply.

commented: nice explanation :) +1

Looks like darkagn and I replied at about the same time. I hadn't refreshed my page while typing my reply.

I think you explained it much better than I did though :)

Your reply was more specific. I just gave an example...

And yes,

You can either sort the numbers stored in each structure or sort the nth elements all the structure variables.

This is a golden truth.

Thank you for the compliments :)

Hello,

Thanks. I understand now how isFixReg() works.

One more question: How does the string sOperator work? I understand how it is used inside the function Calculation but I don't see how it could get its sOperator value from the "else if" above it. This is because within that "else if" there is no assignment whatsoever of deg2 as sOperator, so how does sOperator(+,-, *, etc) is passed to Calculation. Any pointers are appreciated.

sOperator gets its value using this statement.

sOperator = (String) arg;

If your problem is solved, mark this thread as solved by clicking on the link below the last post in this thread.

Hello guys,

Sorry to bother you again with the same thing. I still don't understand how does deg1 work in the code.
Looks to me it's always the number pressed before the sOperator(+,-,*, / or =) is clicked. Inside the Calculation function where dReg1 is passed to where does this dReg1 orginate from? Could dReg1 here be passed from the code section below it(refer code below). I doubt it. Then in that case, how does dReg1 go into Calculation at all?Notice it's: dReg1 = Calculation(sOperator, dReg1, dReg2); Having dReg1 as both result and argument is confusing.

//operation

else if{
//..........................(these I understand)

dReg1 = Calculation(sOperator,dReg1, dReg2);
Double dTemp = new Double(dReg1);
sText2 = dTemp.toString();
text.setText(sText2);

Secondly, what's the 3 lines starting from Double dTemp = new Double(dReg1) above do? Is it used inside the Calculation function too?

Any guidance in reading this code would be very helpful. Thanks in advance.

dReg1 in the above code is an Accumulator, a register that is used to keep track of the result of the calculations. If you think about how a calculator works, you enter a number, press the operator, enter a second number (dReg2) then press equals to find the result. Then you can (if you want) press another operator and yet another number and find another result. And so on. This result is stored in a special register (called the Accumulator). The line

dReg1 = Calculation(sOperator, dReg1, dReg2);

is exactly how an accumulator works. It is possible to create a third register to store the result, but not only would this use more memory but it is not how an Accumulator works, accumulating the result in a single register.

As for your second question, those three lines of code simply print the result to the screen. If you mean what do each line do, then the first converts the result dReg1 to a Double object (as opposed to a double variable type), the second line converts it to a String, and the third sets the text of the display to that String.

Hope this has helped,
darkagn

commented: Nice +2

Nicely explained, darkagn.

Inside the Calculation function where dReg1 is passed to where does this dReg1 orginate from? Could dReg1 here be passed from the code section below it(refer code below). I doubt it. Then in that case, how does dReg1 go into Calculation at all?Notice it's: dReg1 = Calculation(sOperator, dReg1, dReg2); Having dReg1 as both result and argument is confusing.

Why not? Remember this much. In any assignment expression, the RHS is evaluated first and then the result is stored in LHS.

Hope this makes it clear..

Hi darkagn,

Thanks. It's now clear to me.

Hello,

I have the code below done in JBuilder for a calculator. Unlike the previous code I've posted which uses an MVC structure, this one does not. For instance while creating a button, it is "Button button1= new Button(); " instead of "pKey.add(new Button("C")); "(refer to previous post where the code's written by T.Yamazaki). I very much would like to create my code in JBuilder using the same structure in T.Yamazakii's code, where each key that is clicked is handled through its event handlers. How do I overcome this problem? Any suggestion is welcomed.

<code>

package assign1;


import java.awt.BorderLayout;
import java.awt.Dimension;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import java.awt.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;


/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/


public class Frame1 extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
TextField textField1 = new TextField();
Button button1 = new Button();
Button button2 = new Button();
Button button3 = new Button();
Button button4 = new Button();
Button button5 = new Button();
Button button6 = new Button();
Button button7 = new Button();
Button button8 = new Button();
Button button9 = new Button();
Button button10 = new Button();
Button button11 = new Button();
Button button12 = new Button();
Button button13 = new Button();
Button button14 = new Button();
Button button15 = new Button();
Button button16 = new Button();


//selfmade attributes
int number1, number2,number3;
String operator;


public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}


/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(xYLayout1);
setSize(new Dimension(400, 300));
setTitle("Calculator");
textField1.setText("");
button1.setLabel("1");
button1.addActionListener(new Frame1_button1_actionAdapter(this));
button2.setLabel("+");
button2.addActionListener(new Frame1_button2_actionAdapter(this));
button3.setLabel("=");
button3.addActionListener(new Frame1_button3_actionAdapter(this));
button4.setLabel("4");
button4.addActionListener(new Frame1_button4_actionAdapter(this));
button5.setLabel("2");
button5.addActionListener(new Frame1_button5_actionAdapter(this));
button6.setLabel("3");
button6.addActionListener(new Frame1_button6_actionAdapter(this));
button7.setLabel("5");
button7.addActionListener(new Frame1_button7_actionAdapter(this));
button8.setLabel("6");
button8.addActionListener(new Frame1_button8_actionAdapter(this));
button9.setLabel("7");
button9.addActionListener(new Frame1_button9_actionAdapter(this));
button10.setLabel("8");
button10.addActionListener(new Frame1_button10_actionAdapter(this));
button11.setLabel("9");
button11.addActionListener(new Frame1_button11_actionAdapter(this));
button12.setLabel("/");
button13.setLabel("*");
button14.setLabel("-");
button15.setLabel("0");
button15.addActionListener(new Frame1_button15_actionAdapter(this));
button16.setLabel(".");
contentPane.add(textField1, new XYConstraints(14, 19, 366, 54));
contentPane.add(button9, new XYConstraints(179, 84, 41, 28));
contentPane.add(button10, new XYConstraints(227, 85, 47, 28));
contentPane.add(button8, new XYConstraints(227, 118, 48, 33));
contentPane.add(button7, new XYConstraints(179, 118, 42, 34));
contentPane.add(button1, new XYConstraints(179, 157, 46, 33));
contentPane.add(button14, new XYConstraints(335, 155, 45, 33));
contentPane.add(button15, new XYConstraints(180, 194, 44, 35));
contentPane.add(button6, new XYConstraints(279, 156, 49, 34));
contentPane.add(button4, new XYConstraints(280, 119, 47, 34));
contentPane.add(button11, new XYConstraints(279, 84, 50, 30));
contentPane.add(button13, new XYConstraints(335, 119, 44, 31));
contentPane.add(button12, new XYConstraints(335, 86, 43, 29));
contentPane.add(button5, new XYConstraints(229, 157, 45, 34));
contentPane.add(button2, new XYConstraints(279, 194, 47, 33));
contentPane.add(button3, new XYConstraints(336, 192, 43, 35));
contentPane.add(button16, new XYConstraints(235, 198, 40, 30));
}


public void button1_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"1");
}


public void button5_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"2");


}


public void button6_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"3");
}


public void button4_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"4");
}


public void button7_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"5");
}


public void button8_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"6");
}


public void button9_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"7");
}


public void button10_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"8");
}


public void button11_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"9");
}


public void button15_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"0");
}


public void button2_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
number1 = Integer.parseInt(help);
textField1.setText("");
operator = "+";


}


public void button3_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
number2 = Integer.parseInt(help);
String si = String.valueOf(number1+number2);
textField1.setText(si);
}



class Frame1_button3_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button3_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}


public void actionPerformed(ActionEvent e) {
adaptee.button3_actionPerformed(e);
}
}



class Frame1_button2_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button2_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}


public void actionPerformed(ActionEvent e) {
adaptee.button2_actionPerformed(e);
}
}



class Frame1_button15_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button15_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}


public void actionPerformed(ActionEvent e) {
adaptee.button15_actionPerformed(e);
}
}



class Frame1_button11_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button11_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}


public void actionPerformed(ActionEvent e) {
adaptee.button11_actionPerformed(e);
}
}



class Frame1_button10_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button10_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}


public void actionPerformed(ActionEvent e) {
adaptee.button10_actionPerformed(e);
}
}



class Frame1_button9_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button9_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}


public void actionPerformed(ActionEvent e) {
adaptee.button9_actionPerformed(e);
}
}



class Frame1_button8_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button8_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}


public void actionPerformed(ActionEvent e) {
adaptee.button8_actionPerformed(e);
}
}



class Frame1_button7_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button7_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}


public void actionPerformed(ActionEvent e) {
adaptee.button7_actionPerformed(e);
}
}



class Frame1_button4_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button4_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}


public void actionPerformed(ActionEvent e) {
adaptee.button4_actionPerformed(e);
}
}



class Frame1_button6_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button6_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}


public void actionPerformed(ActionEvent e) {
adaptee.button6_actionPerformed(e);
}
}



class Frame1_button5_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button5_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}


public void actionPerformed(ActionEvent e) {
adaptee.button5_actionPerformed(e);
}
}



class Frame1_button1_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}


public void actionPerformed(ActionEvent e) {
adaptee.button1_actionPerformed(e);
}
}
}

Thank you.

Hi nljavaingineur, I suggest you these things:

-Start a new thread for a new problem. It is related to this thread but you are using a different approach.
-Use code tags.
-Use proper alignment in code.

commented: Absolutely. +20
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.