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;
}
- Explain the generics. Explain the copy constructor (and its alternative).
- Is the
new
operator ok? Would it still be ok with an array?
- 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]
- Write preconditions that limit consideration to the "happy path".
- Write a postcondition for the "happy path". Note that the contract,
thus far, is partial.
- Rewrite the contract to be total so that it matches the behavior of the
implementation.
Use Bloch's standard exceptions.
- The resulting contract is not appropriate. Why not?
Rewrite the contract so that it is appropriate.
- Rewrite the implementation to match the revised contract.