Exercise 1: Applying "Contract Design Scheme" for handling

SOLUTION

 

Part 1: Identify the contract implicitly defined by this method That is: Specify its precondition and postcondition

In this example, no particular requirements are placed on the client. The client is allowed to call top in all situations, including when there is no top element to return.

Precondition:

true that is, there is no requirement, the precondition is always satisfied

Postcondition:

If the stack is not empty, then the topmost element is returned, else (that is, if the stack is empty), then an exception is thrown.

The stack is unchanged optional postcondition, otherwise implicit (specify changes only)

 

 

Part 2: Try to suggest another contract How is this new contract different from the first one?

Precondition:

The stack is not empty

Postcondition:

The topmost element of the stack is returned

The stack is unchanged optional postcondition, otherwise implicit (specify changes only)

© Lynne Grewe