Lecture 7, Design by Contract & Interfaces

Reading

Bertrand Meyer. Applying "Design by Contract". In IEEE Computer, October 1992. p40-51. (pdf).

Slide handouts.

JP: Chapter 13.

Study Guide

An important principle when designing a system is the notion of "design by contract". The first example Meyer uses in his paper is that of a delivery service. A client can get a package, which weights less than 5kg, and is less than 2m in each dimension, delivered all over Paris for 100 franks within 4 hours. Thus, the delivery agent do not have to worry about packages too big or too heavy, as they are not covered by the contract. The delivery agent will carry all sorts of things around Paris, but invariantly they are all of a certain size and weight, and invariantly they are delivered within 4 hours. The reputation of the delivery agent depends very much on the capability to always honour this invariant behaviour.

The papers do not use Java as their programming language. The notion of design by contract is independent of programming language. The lecture and exercises will be based on Java as usual. 

I will also talk about the java notion of interfaces, so take a look at how interfaces work in JP chapter 13.

Exercises

Exercise 1 (Hand-In)

  1. Class String has a method "substring(int,int)". State the pre and post conditions for this method. 
  2. The String class also has a method startsWith(String). State the pre and post conditions for this method. Write your own version of the method. Remember to let it acknowledge the same pre and post conditions - and it must not be better than the existing one. Notice, one cannot extend String, so to test your method, it might be necessary to write a static method which takes two arguments, one the string to be tested, and the other the prefix to check for. However, please hand in a version which corresponds to what we might expect in class String.

Exercise 2

Exercise 3

Consider the following class:

public class Haddock {
    private String greet 
        = "You #!@ Miserable earthworms! Numbskulls! and Carpet-sellers!";
}

 

  1. Add a method to the class, which prints each word in greet on a line of its own. A word is a coherent sequence of letters. A letter is a character for which the java.lang.Character.isLetter method returns true. With the above initialization of greet, it should print "You" "Miserable" "earthworms" "Numbskulls" "and" "Carpet" "sellers", each on a line of its own (without ").
  2. Create an class GreetIterator for Haddock which implements the Iterator<String> interface. When iterated, it should return each word as defined above. 
  3. What is the class invariant for your GreetIterator?
  4. Argue why it is true after each legal call of next().
  5. Make Haddock implement the Iteratable<String> interface.
  6. If 5 is successfully implemented, the following piece of code::
            Haddock captain = new Haddock();
            for(String s: captain)
                System.out.println("Haddock says: " + s);
    Should print:
    Haddock says: You
    Haddock says: Miserable
    Haddock says: earthworms
    Haddock says: Numbskulls
    Haddock says: and
    Haddock says: Carpet
    Haddock says: sellers