SWE/CS 332 In Class Exercise Number 2

Names:

Consider the following contract and implementation:


public static <T> List<T> tail (List<T> list) {

   // Precondition:   ???
   // Postcondition:  ???

   List<T> result = new ArrayList<T> (list);
   result.remove(0);
   return result;
}
  1. Explain the generics. Explain the copy constructor (and its alternative).

  2. Is the new operator ok? Would it still be ok with an array?

  3. What does the implementation of tail do in each of the following cases? Mark each case as "happy-path" (or not).
    • list = null

    • list = []

    • list = [cat]

    • list = [cat, dog, mouse]

  4. Write preconditions that limit consideration to the "happy path".

  5. Write a postcondition for the "happy path". Note that the contract, thus far, is partial.

  6. Rewrite the contract to be total so that it matches the behavior of the implementation. Use Bloch's standard exceptions.

  7. The resulting contract is not appropriate. Why not? Rewrite the contract so that it is appropriate.

  8. Rewrite the implementation to match the revised contract.