SWE/CS 332 In Class Exercise # 4
September 11, 2017


Name(s):

Goals: (A) (more) Identifying preconditions and postconditions in common Java contracts, and (B) Basic JUnit tests.

A: Consider the following excerpt from the JavaDoc specification from the Set interface:

Note: Great care must be exercised if mutable objects are used as set 
elements. The behavior of a set is not specified if the value of an object 
is changed in a manner that affects equals comparisons while the object 
is an element in the set. A special case of this prohibition is that it 
is not permissible for a set to contain itself as an element.
  1. In the language of the contract model, is this constraint a precondition or a postcondition? Explain.


  2. Consider the following example, which involves a set of stacks. Note: Stack objects are mutable.
      Set<Stack<String>> set = new HashSet<Stack<String>>(); // Line 1
      Stack<String> stack1   = new Stack<String>();          // Line 2
      Stack<String> stack2   = new Stack<String>();          // Line 3
      stack1.push("cat");                                    // Line 4
      set.add(stack1);                                       // Line 5
      set.add(stack2);                                       // Line 6
      stack2.push("cat");                                    // Line 7
    
    What is the state after each line of code?

    Which line(s) of code, if any, are in conflict with the JavaDoc specification?


  3. Write Java code that violates the last part of the JavaDoc specification.


  4. Constraints can (often) be converted back-and-forth between postconditions and preconditions. Would that work here?


B: Consider the min method from slide 15 of today's PPT.
  1. Generate tests, both inputs and expected outputs.


  2. How many tests do you need?


  3. What information are you using to generate your tests?


Relation to course outcomes:
Demonstrate knowledge of how to apply object-oriented specification and analysis techniques to Java.