SWE/CS 332 In Class Exercise #26

Names:

This is review. Work in groups.

Each person works the exercise individually.

Then the group compares solutions. If solutions differ, discuss with the goal of convergence.

  1. Write a total contract suitable for the remove() method in the java.util.List interface:
       public boolean remove(E e)
    

    Note: Ignore the fact that List is a subtype of Collection. In other words, write the contract "from scratch".

  2. Repeat the previous exercise for the java.util.Set interface
  3. Focusing just on the remove() method, determine whether there is a subtype relationship between List and Set. Be very specific about the steps in your analysis.
  4. Is List a subtype of Comparable? Why or why not? What about String?
  5. In-class exercise 20 examined Bloch's Chooser class. Suppose we allowed Chooser objects to be empty. What would this mean for the contract of the choose() method? Specifically, write two contracts for choose(), one that treats this case with a precondition, and the other that treats this case with a postcondition.

    What is the (possible) subtype relationship between these two Chooser classes?

  6. Suppose we want to add a method to the Chooser class that allows adding an additional choice beyond what is supplied to the constructor.
    public void addChoice(E choice) 
    
    Write a contract for this method and implement it.

    Write a client interaction that starts with three of four card suits (CLUBS, DIAMONDS, HEARTS), and later adds SPADES. Choose something from the 4 suited Chooser object.

    Suppose we decide Chooser objects should be immutable. What should we do with the addChoice() method? What happens to the client interaction with the suits?