User Tools

Site Tools


invariant:ex5

Intro Ex1 Ex2 Ex3 Ex4 Ex5

Invariants: Postconditions

Point Move Postcondition

Task: Pass tests.PointMovePostcondition.

A postcondition of the move operation of a Point is that the coordinates should change accordingly. If a call to move didn't actually move a point by the desired offset, then the point is in an illegal state and so an IllegalStateException should be thrown.

Tools: around advice. Note that because we're dealing with how the coordinates change during move, we need access to the coordinates both before and after the move, in one piece of advice.

When the access to the old coordinates and move arguments has been established, the check comes down to essentially the following code: (where dx and dy are the arguments given to the move operation)

if(p.getX() - oldx != dx || p.getY() - oldy != dy)
  throw new IllegalStateException();

With a correct implementation, you should pass the tests of suite tests.PointMovePostcondition.

Bounds Move Postcondition

Task: Pass tests.BoundsMovePostcondition.

FigureElement objects have a getBounds() method that returns a java.awt.Rectangle representing the bounds of the object. An important postcondition of the general move operation on a figure element is that the figure element's bounds rectangle should move by the same amount as the figure itself. Write an aspect to check for this postcondition – throw an IllegalStateException if it is violated.

The check can be less technical than in the previous case if you employ the Rectangle API:
  1. Employ the copy constructor to create a clone of the original bounds.
  2. Replay the move on the clone with the translate method.
  3. Compare the result to the actual new bounds using the equals method.

A correct implementation should pass the tests of suite tests.BoundsMovePostcondition.


When done, remove the aspect from the build path and continue with the Tracing track.

invariant/ex5.txt · Last modified: 2021/02/05 13:53 by 127.0.0.1