Exercise: Applying "Contract Design Scheme (view this) " for handling Exceptions

Not collected

Recall the discussion of the STACK data structure where

two functions defined for a stack:

  • top() returns a reference to the topmost element on the stack the element itself is unaffected by the operation the element remains on the stack
  • size() returns the number of elements currently on the stack
Assume these implementation details about a stack:


       the stack is implemented using an array called elements                topIndex is the index of the topmost element on the stack The following is one possible implementation of the operation top: Object top() throws EmptyException { if (size() > 0) return elements[topIndex]; else throw(new EmptyException()); } Part 1: Identify the contract implicitly defined by this method That is: Specify its precondition and postcondition Part 2: Try to suggest another contract How is this new contract different from the first one?
A
© Lynne Grewe