| class DivModCalculator {
private DivModPresenter viewObject;
IntPair inputMemory, outputMemory;
public DivModCalculator(DivModPresenter viewObject) {
this.viewObject = viewObject;
inputMemory = null; outputMemory = null;
}
private IntPair divMod(int x, int y){
// require x > 0 && y > 0 ;
int rest = x;
int quotient = 0;
while (rest >= y) {
// invariant rest + quotient * y = x;
rest = rest - y;
quotient = quotient + 1;
}
return (new IntPair(quotient,rest));
// ensure rest + quotient * y = x && rest < y && rest >= 0 ;
} // end div
public void doCalculate(int input1, int input2){
inputMemory = new IntPair(input1, input2);
if (input1 > 0 && input2 > 0)
outputMemory = divMod(input1, input2);
else
outputMemory = new IntPair(-1, -1);
// update the view - in an easy but primitive way:
viewObject.showResult(outputMemory);
}
} // end DivModCalculator |