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)

Computer Science & Information Technology

You might also like to view...

Write a complete program with necessary #include directives to copy the C-string constant “Hello” into the string variable aString, declared below.

``` char aString[10]; ``` What will be an ideal response?

Computer Science & Information Technology

____________________ was developed by Pavel Krauz, inspired by Juggernaut, another session hijacking tool.?

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

The text suggests for each hour of a presentation, you allow ________ hours of planning time

A) two B) three C) four D) five

Computer Science & Information Technology

The Lync service can be accessed through your ________ account

A) Apple B) Google C) Office 365 D) Hotmail

Computer Science & Information Technology