| class DivModCalculator extends Observable{
private DivModPresenterObs viewObject;
IntPair inputMemory, outputMemory;
public DivModCalculator(DivModPresenterObs viewObject) {
this.viewObject = viewObject;
addObserver(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);
setChanged(); // now I am changed
notifyObservers(); // ... and therefore I notifiy my observers
}
public IntPair getResult(){
return outputMemory;
}
} // end DivModCalculator |