Purpose: practice specifying classes using JML and checking specifications using ESC/Java2
Copy the following two Java class declarations into your new project:
ints
In general terms, your task is to annotate the two classes with JML specifications using the Design-by-Contract style. You’ll add:
invariant clauses to specify facts that must always be true about the classes
requires and ensures clauses to specify method pre- and post-conditions
For this assignment, you do not need to specify assignable clauses, though you certainly may.
Amount class first. There are additional instructions in the comments at the beginning of that file. Note that the code has some subtle bugs in it. Part of your challenge is to determine whether your specification is wrong or the code is wrong. Your final solution should be concisely specified and correctly implemented.
Bag class. Like Amount, there are additional instructions in the comments. Unlike Amount, I give you less information about what invariants should hold for the class. Use the method names and the general idea of a multi-set to come up with an appropriate specification.
Some crucial hints:
x—might involve a null pointer, you can add a pre-condition that says x != null. Now the checker will be happy with the field access or method call, but will check that you never pass null as the value for parameter x.
x += 10, instead use x = x + 10.
Break up invariants and pre-conditions to get better warning messages. Instead of
//@ invariant A && B;
write
//@ invariant A;
//@ invariant B;
Amount.java and Bag.java files to the appropriate HW folder in your individual subversion repository for this course. Add and commit them. You do not need to turn in a pdf for this assignment.
This assignment is based on one by Erik Poll at Radboud University Nijmegen.