0

This calculator performs two digit task successfully but what do i need to alter it i want it to evaluate 3 digits

public class Calculator {  
  
  private long currentInput;          //current input
  private long previousInput;         // previous input
  private long result;            // result of calculation
  private String lastOperator = "";  // keeps track of the last operator entered
  
  
  /* New digit entered as integer value i - moves currentInput 1 decimal place to the left and adds i in "one's column" */
  public void inDigit(long i) {
    currentInput = (currentInput * 10) + i;
  }
  
  
  /* Operator entered  + - or *   */
  public void inOperator(String op) {
    previousInput = currentInput;      // save the new input as previous to get ready for next input
    currentInput = 0;
    lastOperator = op;                 // remember which operator was entered
  } 
  
  
   /* Equals operation sets result to previousInput + - or * currentInput (depending on lastOperator) */
  public void inEquals() {
    if (lastOperator.equals("+")) {
      result = previousInput + currentInput;
    } else if (lastOperator.equals("-")) { 
      result = previousInput - currentInput;
    } else if (lastOperator.equals("*"))  {
      result = previousInput * currentInput;
    } 
    lastOperator = "";       // reset last operator to "nothing"
  }
  
  
  /* Clear operation */
  public void inClear() {
    currentInput = 0;
    previousInput = 0;
    result = 0;
    lastOperator = "";
  } 
  
  /* returns the current result */
  public String getResult() {  
    return Long.toString(result);  //converts int to String
  }
  
  /* returns the previous input value */
  public String getPreviousInput() {
    return Long.toString(previousInput);
  }
  /* returns the current input value */
  public String getCurrentInput() {
    return Long.toString(currentInput);
  }

}

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by sirlink99
0

make the previousInput equal to the result, and then reuse it when you do another calculation

previousInput = 3
currentInput = 2
Sign = *
previousInput = result = 3*2 = 6

next operation

previousInput = 6
currentInput = 1
sign = -
previousInput = result = 6-1 = 5

Edited by sirlink99: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.