Discussion of Activity 4.8 - Designing by contract

Here is our sample solution for the Main class:

public class Main
{
   public static void main(String[] args)
   {
      FixedSizeContractQueue queue;

      // Test 1 of 2.
      System.out.println("\nTest 1: create a queue of capacity 4 and fill it up");
      queue = new FixedSizeContractQueue(4);
      for (int i=0; i<4; i++)
      {
         if (queue.isFull())
         {
            System.out.println("The queue is full - cannot add item");
         }
         else
         {
            queue.add(i*10);       // modify this to ensure contract holds
         }
      }
      System.out.println("Queue contents: " + queue);

      // Test 2 of 2.
      System.out.println("\nTest 2: try to add one item too many");
      if (queue.isFull())
      {
         System.out.println("The queue is full - cannot add item");
      }
      else
      {
         queue.add(40);             // modify this to ensure contract holds
      }
      System.out.println("Queue contents: " + queue);
   }
}

Notes

  1. Initially, the unchanged program should compile and run, but should terminate with a kind of RunTimeException.
  2. After the required modifications, it should run to completion.