Calcutron is a calculator utility similar to the calculator that comes with MS Windows:
Calcutron allows users to perform arithmetic on floating point numbers that can have as may as 32 digits. Users can store an intermediate result in a memory cell. The stored result can be erased or recalled.
The user enters a number one digit at a time and the number is displayed. The number may contain up to 32 digits, a decimal point, and a minus sign.
Here is how the user would enter -12.32:
user: enters 1
system: display = 10 * 0 + 1 = 1
system: displays 1
user: enters 2
system: display = 10 * 1 + 2 = 12
system: displays 12
user: enters .
system: decimal mode on
user: enters 3
system: display = 10 * 12 + 3 = 123
system: display = display/10 = 12.3
system: displays 12.3
user: enters 2
system: display = 100 * display + digit = 1232
system: display = display/100 = 12.32
system: displays display
user: enters -
system: display = -1 * display
User enters 0, -, or . at an inappropriate time.
User enters too many digits.
The user can enter any of the following operations:
+, -, /, *
Entering an operation combines the current result with the display using the previously entered operation, then overwrites this operation with the entered operation.
user: enters operation
system: result = result combined with display using last operation
system: displays result
system: clears display
system: last operation = entered operation
For example, here is how the user computes 2 + 4 * 5
user: enters 2
system: display = 2
user: enters +
system: result = 2
system: nextOp = +
system: displays 2
user: enters 4
system: display = 4
user: enters *
system: result = 4 + 2 = 6
system: nextOp = *
system: displays 6
user: enters 5
system: displays 5
user: enters =
system: result = 6 * 5 = 30
system: nextOp = ?
system: displays 30
We can display this same information using a sequence diagram:
User enters operator at an inappropriate time
User attempts to divide by 0
Overflow
Underflow
user: enters clear display (CE)
system: display = 0
system: displays 0
user: enters clear result (C)
system: result = 0
system: clears display
The user may store the displayed number in memory and recall it later.
Main Scenario
user: enters number
system: computes and displays number
user: stores number (M)
system: memory = number
user:
public class Calculator {
private double result = 0, display = 0,
memory = 0, shift = 10;
private boolean decimalMode = false;
char nextOp = '?';
public void enterDigit(int digit) {
display = shift * display + digit;
if (decimalMode) {
display = display/shift;
shift = 10 * shift;
}
System.out.println("display =
" + display);
}
public void enterDecimal() {
decimalMode = true; }
public void enterNegative() { display =
-1 * display; }
private void updateResult() {
System.out.println("operator =
" + nextOp);
switch(nextOp) {
case '+': result += display;
break;
case '*': result *= display;
break;
case '-': result -= display;
break;
case '/': result /= display;
break;
default: result = display;
}
display = 0;
shift = 10;
decimalMode = false;
System.out.println("result =
" + result);
}
public void enterOp(char theOp) {
updateResult();
nextOp = theOp;
}
public void clearDisplay() {
display = 0;
shift = 10;
decimalMode = false;
System.out.println("display =
" + display);
}
public void clearResult() {
result
= 0;
nextOp = '?';
clearDisplay();
}
}