Consider the Collection interface in the java.util package, which is the ancestor of both List and Set. Write preconditions and postconditions for the operations below and modify the constraints you wrote in exercises 9-1 and 9–2, knowing that contracts are inherited. Make sure you comply with the Liskov Substitution Principle.
a. int size() returns the number of elements in the collection.
b. void add(Object e) adds an object to the collection.
c. void remove(Object e) removes an object from the collection.
d. boolean contains(Object e) returns true if the object is in the collection.
a. context Collection::size() post: result = elements->size()
b. context Collection::add(e) post: contains(e)
c. context Collection::remove(e) post:
(@pre.contains(e) implies size() = @pre.size() - 1) and
(not @pre.contains(e) implies size() = @pre.size())
d. context Collection::contains(e) post: elements->includes(e)
With contract inheritance, no constraints on remove, contains, and size of List and Set need to be added. The post
condition on add() on Set and List become:
context List::add(e) post: size() = @pre.size() + 1
context Set::add(e) post:
(@pre.contains(e) implies size() = @pre.size()) and
(not @pre.contains(e) implies size() = @pre.size() + 1)
You might also like to view...
Which statement below is false?
a. Structured programming produces programs that are easier to test. b. Structured programming requires four forms of control. c. Structured programming produces programs that are easier to modify d. Structured programming promotes simplicity.
If you place conditions in different Criteria rows in the design grid, all of the conditions must be met to select a record.
Answer the following statement true (T) or false (F)