Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A few suggestions.</p> <p>First, I would suggest when using a <code>boolean</code> as a conditional for an <code>if</code> statement, avoid comparison with <code>true</code> and <code>false</code> -- there are only two states for <code>boolean</code> anyway. Also, since there are only two states, rather than using <code>else if (false)</code>, an <code>else</code> will suffice:</p> <pre><code>if (condition == true) { // when condition is true } else if (condition == false) { // when condition is false } </code></pre> <p>can be rewritten as:</p> <pre><code>if (condition) { // when condition is true } else { // when condition is false } </code></pre> <p>Second, rather than comparing the string literals <code>"add"</code>, <code>"subtract"</code> and such, try to use constants (<code>final</code> variables), or <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html" rel="nofollow noreferrer"><code>enum</code></a>s. Doing a <code>String</code> comparison such as <code>(operator == "add")</code> is performing a check to see whether the string literal <code>"add"</code> and the <code>operator</code> variable are both <em>refering</em> to the same object, not whether the <em>values</em> are the same. So under certain circumstances, you may have the <code>operator</code> set to <code>"add"</code> but the comparison may not be <code>true</code> because the string literal is refering to a separate object. A simple workaround would be:</p> <pre><code>final String operatorAdd = "add"; // ... if (input.equals("+")) operator = operatorAdd; // ... if (operator == operatorAdd) // ... </code></pre> <p>Now, both the assignment of <code>operator</code> and the comparison of <code>operator</code> both are referecing the constant <code>operatorAdd</code>, so the comparison can use a <code>==</code> rather than a <code>equals()</code> method.</p> <p>Third, as this seems like the type of calculator which doesn't really require two operands (i.e. <code>operand1 + operand2</code>), but rather a single operand which is acting upon a stored value (i.e. <code>operand + currentValue</code>), it probably would be easier to have some variable that holds the current value, and another variable that holds the operator, and a method which will act according to the current operator and operand. (More or less an idea of an <a href="http://en.wikipedia.org/wiki/Accumulator_(computing)" rel="nofollow noreferrer">accumulator machine</a>, or 1-operand computer.)</p> <p>The basic method of operation will be:</p> <ol> <li>Set the <code>currentValue</code>.</li> <li>Set the operator.</li> <li>Set the <code>operand</code>.</li> <li>Perform the calculation.</li> <li>Set the <code>currentValue</code> to the result of the calculation.</li> <li>Set the operator to blank state.</li> </ol> <p>Each step should check that the previous step took place -- be sure that an operation is specified (<code>operator</code> is set to a valid operator), then the next value entered becomes the <code>operand</code>. A calculator is like a <a href="http://en.wikipedia.org/wiki/Finite_state_machine" rel="nofollow noreferrer">state machine</a>, where going from one step to another must be performed in a certain order, or else it will not proceed to the next step.</p> <p>So, the calculator may be like this (pseudocode!):</p> <pre><code>// Initialize calculator (Step 1) currentValue = 0; operand = 0; operator = operatorNone; loop { operand = getOperand(); // Step 2 operator = getOperator(); // Step 3 // Step 4 and 5 if (operator == operatorAdd) currentValue += operand; if (operator == operatorSubtract) currentValue -= operand; // ... // Step 6 operator = operatorNone; } </code></pre> <p>Although the above code uses a single loop and doesn't work like a event-based GUI model, but it should outline the steps that it takes to run a calculator.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload