| | |
isFixReg() ???
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 15
Reputation:
Solved Threads: 0
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.
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 Syntax (Toggle Plain Text)
//******************************* // 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; } }
Last edited by ~s.o.s~; Dec 15th, 2007 at 1:02 pm. Reason: Added code tags, learn to use them.
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.
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.
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.
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.
Java Syntax (Toggle Plain Text)
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.
Last edited by Jishnu; Dec 15th, 2007 at 9:55 am.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Your reply was more specific. I just gave an example...
And yes,
This is a golden truth.
Thank you for the compliments
And yes,
•
•
•
•
You can either sort the numbers stored in each structure or sort the nth elements all the structure variables.
Thank you for the compliments
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
•
•
Join Date: Nov 2007
Posts: 15
Reputation:
Solved Threads: 0
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.
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.
If your problem is solved, mark this thread as solved by clicking on the link below the last post in this thread.
Java Syntax (Toggle Plain Text)
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.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
•
•
Join Date: Nov 2007
Posts: 15
Reputation:
Solved Threads: 0
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.
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
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
java Syntax (Toggle Plain Text)
dReg1 = Calculation(sOperator, dReg1, dReg2);
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
![]() |
Other Threads in the Java Forum
- Previous Thread: Error warning using show() from java.awt.Window
- Next Thread: i need help with GUI
Views: 1545 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider detection draw eclipse editor error errors event exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle os plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time transfer tree windows





