isFixReg() ???

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2007
Posts: 15
Reputation: nljavaingineur is an unknown quantity at this point 
Solved Threads: 0
nljavaingineur nljavaingineur is offline Offline
Newbie Poster

isFixReg() ???

 
0
  #1
Dec 15th, 2007
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.
  1. //*******************************
  2. // Java script of Calculator
  3. // << Calc.java >>
  4. //===============================
  5. // Ver 0.0 96/01/19 T.Yamazaki
  6. //-------------------------------
  7. // email : [email]yamaza@st.rim.or.jp[/email]
  8. //*******************************
  9.  
  10. import java.awt.*;
  11. import java.lang.*;
  12. import java.applet.Applet;
  13.  
  14. //===================
  15. // Calculator Applet
  16. //===================
  17. public class Calc extends java.applet.Applet
  18. {
  19. //--------
  20. // member
  21. //--------
  22. TextField text;
  23. String sText1, sText2;
  24. double dReg1, dReg2, dMem;
  25. String sOperator;
  26. boolean isFixReg;
  27. //-------------
  28. // constructor
  29. //-------------
  30. public Calc()
  31. {
  32. Panel pFrame = new Panel();
  33. pFrame.setLayout(new FlowLayout());
  34.  
  35. text = new TextField("");
  36. text.setForeground(Color.yellow);
  37. text.setEditable(false);
  38.  
  39. Panel pCalc = new Panel();
  40. pCalc.setLayout(new BorderLayout(0, 10));
  41. pCalc.add("North", text);
  42.  
  43. pFrame.add("Center", pCalc);
  44.  
  45. Dimension dSize= pCalc.size();
  46. dSize.width = dSize.width + 20;
  47. dSize.height = dSize.height + 20;
  48. pFrame.resize(dSize);
  49.  
  50. Panel pKey = new Panel();
  51. pKey.setLayout(new GridLayout(5, 4, 5, 5));
  52. add("Center", pKey);
  53. pKey.add(new Button("C"));
  54. pKey.add(new Button("MR"));
  55. pKey.add(new Button("M-"));
  56. pKey.add(new Button("M+"));
  57. pKey.add(new Button("7"));
  58. pKey.add(new Button("8"));
  59. pKey.add(new Button("9"));
  60. pKey.add(new Button("/"));
  61. pKey.add(new Button("4"));
  62. pKey.add(new Button("5"));
  63. pKey.add(new Button("6"));
  64. pKey.add(new Button("*"));
  65. pKey.add(new Button("1"));
  66. pKey.add(new Button("2"));
  67. pKey.add(new Button("3"));
  68. pKey.add(new Button("-"));
  69. pKey.add(new Button("0"));
  70. pKey.add(new Button("."));
  71. pKey.add(new Button("="));
  72. pKey.add(new Button("+"));
  73.  
  74. pCalc.add("South", pKey);
  75.  
  76. setLayout(new BorderLayout(0, 0));
  77. add("North", pFrame);
  78. setBackground(Color.darkGray);
  79.  
  80. dReg1 = 0.0d;
  81. dReg2 = 0.0d;
  82. dMem = 0.0d;
  83. sOperator = "";
  84. text.setText("0");
  85. isFixReg = true; //What does this do?What is it for?It's used below.
  86. }
  87. //---------------
  88. // event handler
  89. //---------------
  90. public boolean action(Event evt, Object arg)
  91. {
  92. //
  93. // numeric key input
  94. //
  95. if ("C".equals(arg))
  96. {
  97. dReg1 = 0.0d;
  98. dReg2 = 0.0d;
  99. sOperator = "";
  100. text.setText("0");
  101. isFixReg = true;
  102. }
  103. else if (("0".equals(arg)) | ("1".equals(arg)) | ("2".equals(arg))
  104. | ("3".equals(arg)) | ("4".equals(arg)) | ("5".equals(arg))
  105. | ("6".equals(arg)) | ("7".equals(arg)) | ("8".equals(arg))
  106. | ("9".equals(arg)) | (".".equals(arg)))
  107. {
  108. if (isFixReg)
  109. sText2 = (String) arg;
  110. else
  111. sText2 = text.getText() + arg;
  112. text.setText(sText2);
  113. isFixReg = false;
  114. }
  115. //
  116. // operations
  117. //
  118. else if (("+".equals(arg)) | ("-".equals(arg))
  119. | ("*".equals(arg)) | ("/".equals(arg)) | ("=".equals(arg)))
  120. {
  121. sText1 = text.getText();
  122. dReg2 = (Double.valueOf(sText1)).doubleValue();
  123. dReg1 = Calculation(sOperator, dReg1, dReg2);
  124. Double dTemp = new Double(dReg1);
  125. sText2 = dTemp.toString();
  126. text.setText(sText2);
  127. sOperator = (String) arg;
  128. isFixReg = true;
  129. }
  130. //
  131. // memory read operation
  132. //
  133. else if ("MR".equals(arg))
  134. {
  135. Double dTemp = new Double(dMem);
  136. sText2 = dTemp.toString();
  137. text.setText(sText2);
  138. sOperator = "";
  139. isFixReg = true;
  140. }
  141. //
  142. // memory add operation
  143. //
  144. else if ("M+".equals(arg))
  145. {
  146. sText1 = text.getText();
  147. dReg2 = (Double.valueOf(sText1)).doubleValue();
  148. dReg1 = Calculation(sOperator, dReg1, dReg2);
  149. Double dTemp = new Double(dReg1);
  150. sText2 = dTemp.toString();
  151. text.setText(sText2);
  152. dMem = dMem + dReg1;
  153. sOperator = "";
  154. isFixReg = true;
  155. }
  156. //
  157. // memory sub operation
  158. //
  159. else if ("M-".equals(arg))
  160. {
  161. sText1 = text.getText();
  162. dReg2 = (Double.valueOf(sText1)).doubleValue();
  163. dReg1 = Calculation(sOperator, dReg1, dReg2);
  164. Double dTemp = new Double(dReg1);
  165. sText2 = dTemp.toString();
  166. text.setText(sText2);
  167. dMem = dMem - dReg1;
  168. sOperator = "";
  169. isFixReg = true;
  170. }
  171. return true;
  172. }
  173. //-------------
  174. // Calculation
  175. //-------------
  176. private double Calculation(String sOperator, double dReg1, double dReg2)
  177. {
  178. if ("+".equals(sOperator)) dReg1 = dReg1 + dReg2;
  179. else if ("-".equals(sOperator)) dReg1 = dReg1 - dReg2;
  180. else if ("*".equals(sOperator)) dReg1 = dReg1 * dReg2;
  181. else if ("/".equals(sOperator)) dReg1 = dReg1 / dReg2;
  182. else dReg1 = dReg2;
  183. return dReg1;
  184. }
  185. }
Last edited by ~s.o.s~; Dec 15th, 2007 at 1:02 pm. Reason: Added code tags, learn to use them.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 811
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Practically a Posting Shark

Re: isFixReg() ???

 
2
  #2
Dec 15th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: isFixReg() ???

 
2
  #3
Dec 15th, 2007
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.

  1. if (isFixReg)
  2. sText2 = (String) arg;
  3. else
  4. sText2 = text.getText() + arg;
  5. text.setText(sText2);
  6. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 811
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Practically a Posting Shark

Re: isFixReg() ???

 
0
  #4
Dec 15th, 2007
Originally Posted by Jishnu View Post
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: isFixReg() ???

 
0
  #5
Dec 15th, 2007
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
"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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 15
Reputation: nljavaingineur is an unknown quantity at this point 
Solved Threads: 0
nljavaingineur nljavaingineur is offline Offline
Newbie Poster

Re: isFixReg() ???

 
0
  #6
Dec 15th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: isFixReg() ???

 
1
  #7
Dec 16th, 2007
sOperator gets its value using this statement.

  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 15
Reputation: nljavaingineur is an unknown quantity at this point 
Solved Threads: 0
nljavaingineur nljavaingineur is offline Offline
Newbie Poster

Re: isFixReg() ???

 
0
  #8
Dec 16th, 2007
Oh ok, thank you!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 15
Reputation: nljavaingineur is an unknown quantity at this point 
Solved Threads: 0
nljavaingineur nljavaingineur is offline Offline
Newbie Poster

Re: isFixReg() ???(Trying still to understand)

 
0
  #9
Dec 18th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 811
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Practically a Posting Shark

Re: isFixReg() ???

 
1
  #10
Dec 18th, 2007
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
  1. 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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 1545 | Replies: 13
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC