SWE 619 In Class Exercise Number 6


This exercise is intended to get you to think about Liskov's argument about equals(). The following is "normal" Java:

Set<List<String>> s = new HashSet<List<String>>();     // AF(s) = ___
List<String> x = new ArrayList<String>();  // AF(x) = ___
List<String> y = new ArrayList<String>();  // AF(y) = ___
s.add(x);		 // AF(s) = ____
s.add(y);		 // AF(s) = ____
s.contains(y)            // true or false?
y.add("cat");	         // AF(y) = ____
                         // AF(s) = ____
s.contains(y);           // true or false?
s.add(y);		 // AF(s) = ____
y.remove("cat");	 // AF(s) = ____

s.remove(y);             // AF(s) = ____
s.contains(y);           // true, false, or something else?
Fill in the values. What is the role of preconditions here?

Now, let's turn to related subtypes.
  1. Define an implementation of Comparator that reverses the natural ordering.

    Does this implementation satisfy the Comparator contract?

    Can you generify this implementation?

  2. Repeat the exercise with a comparator<Integer> that uses absolute values.

  3. Repeat the exercise with a comparator<Integer> that orders evens before odds.