<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Lwillia</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Lwillia"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Lwillia"/>
	<updated>2026-05-18T05:24:47Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10350</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10350"/>
		<updated>2007-11-29T02:53:48Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 5 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions. [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling. [10]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
* The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
* The post conditions are used by the supplier to develop code.&lt;br /&gt;
* The developed code is then given back to the client with pre-conditions.&lt;br /&gt;
* These pre-conditions have to be honored in order for the post conditions to be honored&lt;br /&gt;
* This in turn becomes a contract between both sides&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function in the Python Progamming language. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. If any of the pre or post conditions are not upheld, an exception is thrown. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; section defines a precondition. This precondition checks to make sure that the parameter a is an instance of a list. It also checks to see if each element within the list can be compared to all of the other elements within the list. Both of these preconditions need to be met in order for the sort function to work properly.  &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;post:&amp;quot; section defines a postcondition. There are three conditions that are required to be met at the end of the function. First the length of the list must never change during the sorting of the list. Second, no elements should be lost or gained from sorting the list. Lastly the list has to actually be sorted. These three conditions assures the integrity of the list. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; and &amp;quot;post:&amp;quot; together make up the contract of the function.&lt;br /&gt;
&lt;br /&gt;
* The function is then sorted.&lt;br /&gt;
&lt;br /&gt;
* The code after &amp;quot;# enable contract checking&amp;quot; is used to test the function and verify that the contract is upheld. The function is simply imported and ran with an arbitrary list. &lt;br /&gt;
&lt;br /&gt;
* The example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _initparms&amp;quot; is the initialization for the class while &amp;quot;def addNumber(self, number)&amp;quot; adds the number to the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _invariant(self):&amp;quot; is an invariant that uses an assertion to verify that the length of the list doesn't exceed 100 and that there is at least one element in the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def addNumber_pre_condition(self, number):&amp;quot; defines a pre-condition for the addNumber function. The conditions that have to be met are a variable must contain an integer only and the number must be less than 100 and greater than zero. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def length_post_condition(self, result):&amp;quot; Is the post condition for the length function. This insures that after the length is found that the list is never bigger than &lt;br /&gt;
&lt;br /&gt;
* The pre-condition for addNumber and the post-condition for length together make up the contract of this class. The contract is enforced through the assertions and invariants.&lt;br /&gt;
&lt;br /&gt;
* The example below is from link 4.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
Below is a C example of two contracts for functions. Contracts in C begin with &amp;quot;/**&amp;quot; and end with &amp;quot;*/&amp;quot;. They are placed directly on top of the function.&lt;br /&gt;
&lt;br /&gt;
The contract below contains two preconditions and one post condition. The first pre-condition make sure that the index is greater than zero. The second makes sure that the index is less than the length of the array.  The post-condition simply states that a pointer to the index element within the array passed in is what is returned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
  pre: index &amp;gt;= 0&lt;br /&gt;
  pre: index &amp;lt; ary-&amp;gt;length&lt;br /&gt;
  post: return == ary-&amp;gt;ptr[index]&lt;br /&gt;
 */&lt;br /&gt;
void *DArray_get(DArray_T ary, long index)&lt;br /&gt;
{&lt;br /&gt;
  return ary-&amp;gt;ptr[index];&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The function below returns a copy of the array that is a parameter. The contract for this function contaisn a post conditions which states that the array returned must not be null. Also, the length of the copy must be equal to the length of the array copied. The last post-condition states that each element in the copied array equals each element in the original array. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
  post: return != NULL implies return-&amp;gt;length == ary-&amp;gt;length and&lt;br /&gt;
    forall (long i in 0...ary-&amp;gt;length | return-&amp;gt;ptr[i] == ary-&amp;gt;ptr[i])&lt;br /&gt;
 */&lt;br /&gt;
DArray_T DArray_copy(DArray_T ary)&lt;br /&gt;
{&lt;br /&gt;
  /* ... */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. The implementation contains functions to push and pop off on the stack. Also, the size of stack can be determined by the Size function. The remaining function returns how many more objects can be pushed onto the stack. Pre and post conditions are implemented as well as an invariant. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;preconditions()&amp;quot; defines a prec-onditon for Push and a pre-condition for Pop. The pre-condition for Push is to assure that the size of the stack is less than the maximum capacity of the stack before pushing another element on it. The pre-condition for Pop is that the stack contains at least one element to be popped off.  &lt;br /&gt;
* &amp;quot;postconditions()&amp;quot; defines one post-condition for push. This post condition simply assures that after placing something on the stack it is greater than zero.&lt;br /&gt;
* The invariant asurres that at all times the size of the stack is greater than zero and less than the maximum capacity.&lt;br /&gt;
* The contract consists of the preconditions and post-conditions in this example.&lt;br /&gt;
* The example is taken from link 7.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;br /&gt;
# http://www.onlamp.com/pub/a/onlamp/2004/10/28/design_by_contract_in_c.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10348</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10348"/>
		<updated>2007-11-29T02:49:21Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 4 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions. [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling. [10]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
* The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
* The post conditions are used by the supplier to develop code.&lt;br /&gt;
* The developed code is then given back to the client with pre-conditions.&lt;br /&gt;
* These pre-conditions have to be honored in order for the post conditions to be honored&lt;br /&gt;
* This in turn becomes a contract between both sides&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function in the Python Progamming language. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. If any of the pre or post conditions are not upheld, an exception is thrown. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; section defines a precondition. This precondition checks to make sure that the parameter a is an instance of a list. It also checks to see if each element within the list can be compared to all of the other elements within the list. Both of these preconditions need to be met in order for the sort function to work properly.  &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;post:&amp;quot; section defines a postcondition. There are three conditions that are required to be met at the end of the function. First the length of the list must never change during the sorting of the list. Second, no elements should be lost or gained from sorting the list. Lastly the list has to actually be sorted. These three conditions assures the integrity of the list. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; and &amp;quot;post:&amp;quot; together make up the contract of the function.&lt;br /&gt;
&lt;br /&gt;
* The function is then sorted.&lt;br /&gt;
&lt;br /&gt;
* The code after &amp;quot;# enable contract checking&amp;quot; is used to test the function and verify that the contract is upheld. The function is simply imported and ran with an arbitrary list. &lt;br /&gt;
&lt;br /&gt;
* The example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _initparms&amp;quot; is the initialization for the class while &amp;quot;def addNumber(self, number)&amp;quot; adds the number to the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _invariant(self):&amp;quot; is an invariant that uses an assertion to verify that the length of the list doesn't exceed 100 and that there is at least one element in the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def addNumber_pre_condition(self, number):&amp;quot; defines a pre-condition for the addNumber function. The conditions that have to be met are a variable must contain an integer only and the number must be less than 100 and greater than zero. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def length_post_condition(self, result):&amp;quot; Is the post condition for the length function. This insures that after the length is found that the list is never bigger than &lt;br /&gt;
&lt;br /&gt;
* The pre-condition for addNumber and the post-condition for length together make up the contract of this class. The contract is enforced through the assertions and invariants.&lt;br /&gt;
&lt;br /&gt;
* The example below is from link 4.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
Below is a C example of two contracts for functions. Contracts in C begin with &amp;quot;/**&amp;quot; and end with &amp;quot;*/&amp;quot;. They are placed directly on top of the function.&lt;br /&gt;
&lt;br /&gt;
The contract below contains two preconditions and one post condition. The first pre-condition make sure that the index is greater than zero. The second makes sure that the index is less than the length of the array.  The post-condition simply states that a pointer to the index element within the array passed in is what is returned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
  pre: index &amp;gt;= 0&lt;br /&gt;
  pre: index &amp;lt; ary-&amp;gt;length&lt;br /&gt;
  post: return == ary-&amp;gt;ptr[index]&lt;br /&gt;
 */&lt;br /&gt;
void *DArray_get(DArray_T ary, long index)&lt;br /&gt;
{&lt;br /&gt;
  return ary-&amp;gt;ptr[index];&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The function below returns a copy of the array that is a parameter. The contract for this function contaisn a post conditions which states that the array returned must not be null. Also, the length of the copy must be equal to the length of the array copied. The last post-condition states that each element in the copied array equals each element in the original array. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
  post: return != NULL implies return-&amp;gt;length == ary-&amp;gt;length and&lt;br /&gt;
    forall (long i in 0...ary-&amp;gt;length | return-&amp;gt;ptr[i] == ary-&amp;gt;ptr[i])&lt;br /&gt;
 */&lt;br /&gt;
DArray_T DArray_copy(DArray_T ary)&lt;br /&gt;
{&lt;br /&gt;
  /* ... */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. The implementation contains functions to push and pop off on the stack. Also, the size of stack can be determined by the Size function. The remaining function returns how many more objects can be pushed onto the stack. Pre and post conditions are implemented as well as an invariant. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;preconditions()&amp;quot; defines a prec-onditon for Push and a pre-condition for Pop. The pre-condition for Push is to assure that the size of the stack is less than the maximum capacity of the stack before pushing another element on it. The pre-condition for Pop is that the stack contains at least one element to be popped off.  &lt;br /&gt;
* &amp;quot;postconditions()&amp;quot; defines one post-condition for push. This post condition simply assures that after placing something on the stack it is greater than zero.&lt;br /&gt;
* The invariant asurres that at all times the size of the stack is greater than zero and less than the maximum capacity.&lt;br /&gt;
* The contract consists of the preconditions and post-conditions in this example.&lt;br /&gt;
* The example is taken from link 7.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;br /&gt;
# http://www.onlamp.com/pub/a/onlamp/2004/10/28/design_by_contract_in_c.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10347</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10347"/>
		<updated>2007-11-29T02:49:04Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 4 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions. [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling. [10]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
* The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
* The post conditions are used by the supplier to develop code.&lt;br /&gt;
* The developed code is then given back to the client with pre-conditions.&lt;br /&gt;
* These pre-conditions have to be honored in order for the post conditions to be honored&lt;br /&gt;
* This in turn becomes a contract between both sides&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function in the Python Progamming language. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. If any of the pre or post conditions are not upheld, an exception is thrown. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; section defines a precondition. This precondition checks to make sure that the parameter a is an instance of a list. It also checks to see if each element within the list can be compared to all of the other elements within the list. Both of these preconditions need to be met in order for the sort function to work properly.  &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;post:&amp;quot; section defines a postcondition. There are three conditions that are required to be met at the end of the function. First the length of the list must never change during the sorting of the list. Second, no elements should be lost or gained from sorting the list. Lastly the list has to actually be sorted. These three conditions assures the integrity of the list. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; and &amp;quot;post:&amp;quot; together make up the contract of the function.&lt;br /&gt;
&lt;br /&gt;
* The function is then sorted.&lt;br /&gt;
&lt;br /&gt;
* The code after &amp;quot;# enable contract checking&amp;quot; is used to test the function and verify that the contract is upheld. The function is simply imported and ran with an arbitrary list. &lt;br /&gt;
&lt;br /&gt;
* The example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _initparms&amp;quot; is the initialization for the class while &amp;quot;def addNumber(self, number)&amp;quot; adds the number to the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _invariant(self):&amp;quot; is an invariant that uses an assertion to verify that the length of the list doesn't exceed 100 and that there is at least one element in the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def addNumber_pre_condition(self, number):&amp;quot; defines a pre-condition for the addNumber function. The conditions that have to be met are a variable must contain an integer only and the number must be less than 100 and greater than zero. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def length_post_condition(self, result):&amp;quot; Is the post condition for the length function. This insures that after the length is found that the list is never bigger than &lt;br /&gt;
&lt;br /&gt;
* The pre-condition for addNumber and the post-condition for length together make up the contract of this class. The contract is enforced through the assertions and invariants.&lt;br /&gt;
&lt;br /&gt;
* The example below is from link 4.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
Below is a C example of two contracts for functions. Contracts in C begin with &amp;quot;/**&amp;quot; and end with &amp;quot;*/&amp;quot;. They are placed directly on top of the function.&lt;br /&gt;
&lt;br /&gt;
The contract below contains two preconditions and one post condition. The first pre-condition make sure that the index is greater than zero. The second makes sure that the index is less than the length of the array.  The post-condition simply states that a pointer to the index element within the array passed in is what is returned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
  pre: index &amp;gt;= 0&lt;br /&gt;
  pre: index &amp;lt; ary-&amp;gt;length&lt;br /&gt;
  post: return == ary-&amp;gt;ptr[index]&lt;br /&gt;
 */&lt;br /&gt;
void *DArray_get(DArray_T ary, long index)&lt;br /&gt;
{&lt;br /&gt;
  return ary-&amp;gt;ptr[index];&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The function below returns a copy of the array that is a parameter. The contract for this function contaisn a post conditions which states that the array returned must not be null. Also, the length of the copy must be equal to the length of the array copied. The last post-condition states that each element in the copied array equals each element in the original array. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
  post: return != NULL implies return-&amp;gt;length == ary-&amp;gt;length and&lt;br /&gt;
    forall (long i in 0...ary-&amp;gt;length | return-&amp;gt;ptr[i] == ary-&amp;gt;ptr[i])&lt;br /&gt;
 */&lt;br /&gt;
DArray_T DArray_copy(DArray_T ary)&lt;br /&gt;
{&lt;br /&gt;
  /* ... */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. The implementation contains functions to push and pop off on the stack. Also, the size of stack can be determined by the Size function. The remaining function returns how many more objects can be pushed onto the stack. Pre and post conditions are implemented as well as an invariant. &lt;br /&gt;
&lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;preconditions()&amp;quot; defines a prec-onditon for Push and a pre-condition for Pop. The pre-condition for Push is to assure that the size of the stack is less than the maximum capacity of the stack before pushing another element on it. The pre-condition for Pop is that the stack contains at least one element to be popped off.  &lt;br /&gt;
* &amp;quot;postconditions()&amp;quot; defines one post-condition for push. This post condition simply assures that after placing something on the stack it is greater than zero.&lt;br /&gt;
* The invariant asurres that at all times the size of the stack is greater than zero and less than the maximum capacity.&lt;br /&gt;
* The contract consists of the preconditions and post-conditions in this example.&lt;br /&gt;
* The example is taken from link 7.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;br /&gt;
# http://www.onlamp.com/pub/a/onlamp/2004/10/28/design_by_contract_in_c.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10346</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10346"/>
		<updated>2007-11-29T02:41:05Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 4 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions. [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling. [10]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
* The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
* The post conditions are used by the supplier to develop code.&lt;br /&gt;
* The developed code is then given back to the client with pre-conditions.&lt;br /&gt;
* These pre-conditions have to be honored in order for the post conditions to be honored&lt;br /&gt;
* This in turn becomes a contract between both sides&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function in the Python Progamming language. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. If any of the pre or post conditions are not upheld, an exception is thrown. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; section defines a precondition. This precondition checks to make sure that the parameter a is an instance of a list. It also checks to see if each element within the list can be compared to all of the other elements within the list. Both of these preconditions need to be met in order for the sort function to work properly.  &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;post:&amp;quot; section defines a postcondition. There are three conditions that are required to be met at the end of the function. First the length of the list must never change during the sorting of the list. Second, no elements should be lost or gained from sorting the list. Lastly the list has to actually be sorted. These three conditions assures the integrity of the list. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; and &amp;quot;post:&amp;quot; together make up the contract of the function.&lt;br /&gt;
&lt;br /&gt;
* The function is then sorted.&lt;br /&gt;
&lt;br /&gt;
* The code after &amp;quot;# enable contract checking&amp;quot; is used to test the function and verify that the contract is upheld. The function is simply imported and ran with an arbitrary list. &lt;br /&gt;
&lt;br /&gt;
* The example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _initparms&amp;quot; is the initialization for the class while &amp;quot;def addNumber(self, number)&amp;quot; adds the number to the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _invariant(self):&amp;quot; is an invariant that uses an assertion to verify that the length of the list doesn't exceed 100 and that there is at least one element in the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def addNumber_pre_condition(self, number):&amp;quot; defines a pre-condition for the addNumber function. The conditions that have to be met are a variable must contain an integer only and the number must be less than 100 and greater than zero. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def length_post_condition(self, result):&amp;quot; Is the post condition for the length function. This insures that after the length is found that the list is never bigger than &lt;br /&gt;
&lt;br /&gt;
* The pre-condition for addNumber and the post-condition for length together make up the contract of this class. The contract is enforced through the assertions and invariants.&lt;br /&gt;
&lt;br /&gt;
* The example below is from link 4.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
Below is a C example of two contracts for functions. Contracts in C begin with &amp;quot;/**&amp;quot; and end with &amp;quot;*/&amp;quot;. They are placed directly on top of the function.&lt;br /&gt;
&lt;br /&gt;
The contract below contains two preconditions and one post condition. The first pre-condition make sure that the index is greater than zero. The second makes sure that the index is less than the length of the array.  The post-condition simply states that a pointer to the index element within the array passed in is what is returned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
  pre: index &amp;gt;= 0&lt;br /&gt;
  pre: index &amp;lt; ary-&amp;gt;length&lt;br /&gt;
  post: return == ary-&amp;gt;ptr[index]&lt;br /&gt;
 */&lt;br /&gt;
void *DArray_get(DArray_T ary, long index)&lt;br /&gt;
{&lt;br /&gt;
  return ary-&amp;gt;ptr[index];&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The function below returns a copy of the array that is a parameter. The contract for this function contaisn a post conditions which states that the array returned must not be null. Also, the length of the copy must be equal to the length of the array copied. The last post-condition states that each element in the copied array equals each element in the original array. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
  post: return != NULL implies return-&amp;gt;length == ary-&amp;gt;length and&lt;br /&gt;
    forall (long i in 0...ary-&amp;gt;length | return-&amp;gt;ptr[i] == ary-&amp;gt;ptr[i])&lt;br /&gt;
 */&lt;br /&gt;
DArray_T DArray_copy(DArray_T ary)&lt;br /&gt;
{&lt;br /&gt;
  /* ... */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. The implementation contains functions to push and pop off on the stack. Also, the size of stack can be determined by the Size function. The remaining function returns how many more objects can be pushed onto the stack. Pre and post conditions are implemented as well as an invariant. &lt;br /&gt;
&lt;br /&gt;
*&lt;br /&gt;
*&lt;br /&gt;
* The example is taken from link 7.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;br /&gt;
# http://www.onlamp.com/pub/a/onlamp/2004/10/28/design_by_contract_in_c.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10345</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10345"/>
		<updated>2007-11-29T02:38:45Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 4 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions. [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling. [10]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
* The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
* The post conditions are used by the supplier to develop code.&lt;br /&gt;
* The developed code is then given back to the client with pre-conditions.&lt;br /&gt;
* These pre-conditions have to be honored in order for the post conditions to be honored&lt;br /&gt;
* This in turn becomes a contract between both sides&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function in the Python Progamming language. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. If any of the pre or post conditions are not upheld, an exception is thrown. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; section defines a precondition. This precondition checks to make sure that the parameter a is an instance of a list. It also checks to see if each element within the list can be compared to all of the other elements within the list. Both of these preconditions need to be met in order for the sort function to work properly.  &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;post:&amp;quot; section defines a postcondition. There are three conditions that are required to be met at the end of the function. First the length of the list must never change during the sorting of the list. Second, no elements should be lost or gained from sorting the list. Lastly the list has to actually be sorted. These three conditions assures the integrity of the list. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; and &amp;quot;post:&amp;quot; together make up the contract of the function.&lt;br /&gt;
&lt;br /&gt;
* The function is then sorted.&lt;br /&gt;
&lt;br /&gt;
* The code after &amp;quot;# enable contract checking&amp;quot; is used to test the function and verify that the contract is upheld. The function is simply imported and ran with an arbitrary list. &lt;br /&gt;
&lt;br /&gt;
* The example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _initparms&amp;quot; is the initialization for the class while &amp;quot;def addNumber(self, number)&amp;quot; adds the number to the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _invariant(self):&amp;quot; is an invariant that uses an assertion to verify that the length of the list doesn't exceed 100 and that there is at least one element in the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def addNumber_pre_condition(self, number):&amp;quot; defines a pre-condition for the addNumber function. The conditions that have to be met are a variable must contain an integer only and the number must be less than 100 and greater than zero. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def length_post_condition(self, result):&amp;quot; Is the post condition for the length function. This insures that after the length is found that the list is never bigger than &lt;br /&gt;
&lt;br /&gt;
* The pre-condition for addNumber and the post-condition for length together make up the contract of this class. The contract is enforced through the assertions and invariants.&lt;br /&gt;
&lt;br /&gt;
* The example below is from link 4.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
Below is a C example of two contracts for functions. Contracts in C begin with &amp;quot;/**&amp;quot; and end with &amp;quot;*/&amp;quot;. They are placed directly on top of the function.&lt;br /&gt;
&lt;br /&gt;
The contract below contains two preconditions and one post condition. The first pre-condition make sure that the index is greater than zero. The second makes sure that the index is less than the length of the array.  The post-condition simply states that a pointer to the index element within the array passed in is what is returned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
  pre: index &amp;gt;= 0&lt;br /&gt;
  pre: index &amp;lt; ary-&amp;gt;length&lt;br /&gt;
  post: return == ary-&amp;gt;ptr[index]&lt;br /&gt;
 */&lt;br /&gt;
void *DArray_get(DArray_T ary, long index)&lt;br /&gt;
{&lt;br /&gt;
  return ary-&amp;gt;ptr[index];&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The function below returns a copy of the array that is a parameter. The contract for this function contaisn a post conditions which states that the array returned must not be null. Also, the length of the copy must be equal to the length of the array copied. The last post-condition states that each element in the copied array equals each element in the original array. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
  post: return != NULL implies return-&amp;gt;length == ary-&amp;gt;length and&lt;br /&gt;
    forall (long i in 0...ary-&amp;gt;length | return-&amp;gt;ptr[i] == ary-&amp;gt;ptr[i])&lt;br /&gt;
 */&lt;br /&gt;
DArray_T DArray_copy(DArray_T ary)&lt;br /&gt;
{&lt;br /&gt;
  /* ... */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. The implementation contains functions to push and pop off on the stack. Also, the size of stack can be determined by the Size function.  Pre and post conditions are implemented as well as an invariant. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* The example is taken from link 7.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;br /&gt;
# http://www.onlamp.com/pub/a/onlamp/2004/10/28/design_by_contract_in_c.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10344</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10344"/>
		<updated>2007-11-29T02:36:25Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 3 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions. [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling. [10]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
* The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
* The post conditions are used by the supplier to develop code.&lt;br /&gt;
* The developed code is then given back to the client with pre-conditions.&lt;br /&gt;
* These pre-conditions have to be honored in order for the post conditions to be honored&lt;br /&gt;
* This in turn becomes a contract between both sides&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function in the Python Progamming language. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. If any of the pre or post conditions are not upheld, an exception is thrown. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; section defines a precondition. This precondition checks to make sure that the parameter a is an instance of a list. It also checks to see if each element within the list can be compared to all of the other elements within the list. Both of these preconditions need to be met in order for the sort function to work properly.  &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;post:&amp;quot; section defines a postcondition. There are three conditions that are required to be met at the end of the function. First the length of the list must never change during the sorting of the list. Second, no elements should be lost or gained from sorting the list. Lastly the list has to actually be sorted. These three conditions assures the integrity of the list. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; and &amp;quot;post:&amp;quot; together make up the contract of the function.&lt;br /&gt;
&lt;br /&gt;
* The function is then sorted.&lt;br /&gt;
&lt;br /&gt;
* The code after &amp;quot;# enable contract checking&amp;quot; is used to test the function and verify that the contract is upheld. The function is simply imported and ran with an arbitrary list. &lt;br /&gt;
&lt;br /&gt;
* The example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _initparms&amp;quot; is the initialization for the class while &amp;quot;def addNumber(self, number)&amp;quot; adds the number to the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _invariant(self):&amp;quot; is an invariant that uses an assertion to verify that the length of the list doesn't exceed 100 and that there is at least one element in the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def addNumber_pre_condition(self, number):&amp;quot; defines a pre-condition for the addNumber function. The conditions that have to be met are a variable must contain an integer only and the number must be less than 100 and greater than zero. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def length_post_condition(self, result):&amp;quot; Is the post condition for the length function. This insures that after the length is found that the list is never bigger than &lt;br /&gt;
&lt;br /&gt;
* The pre-condition for addNumber and the post-condition for length together make up the contract of this class. The contract is enforced through the assertions and invariants.&lt;br /&gt;
&lt;br /&gt;
* The example below is from link 4.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
Below is a C example of two contracts for functions. Contracts in C begin with &amp;quot;/**&amp;quot; and end with &amp;quot;*/&amp;quot;. They are placed directly on top of the function.&lt;br /&gt;
&lt;br /&gt;
The contract below contains two preconditions and one post condition. The first pre-condition make sure that the index is greater than zero. The second makes sure that the index is less than the length of the array.  The post-condition simply states that a pointer to the index element within the array passed in is what is returned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
  pre: index &amp;gt;= 0&lt;br /&gt;
  pre: index &amp;lt; ary-&amp;gt;length&lt;br /&gt;
  post: return == ary-&amp;gt;ptr[index]&lt;br /&gt;
 */&lt;br /&gt;
void *DArray_get(DArray_T ary, long index)&lt;br /&gt;
{&lt;br /&gt;
  return ary-&amp;gt;ptr[index];&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The function below returns a copy of the array that is a parameter. The contract for this function contaisn a post conditions which states that the array returned must not be null. Also, the length of the copy must be equal to the length of the array copied. The last post-condition states that each element in the copied array equals each element in the original array. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
  post: return != NULL implies return-&amp;gt;length == ary-&amp;gt;length and&lt;br /&gt;
    forall (long i in 0...ary-&amp;gt;length | return-&amp;gt;ptr[i] == ary-&amp;gt;ptr[i])&lt;br /&gt;
 */&lt;br /&gt;
DArray_T DArray_copy(DArray_T ary)&lt;br /&gt;
{&lt;br /&gt;
  /* ... */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;br /&gt;
# http://www.onlamp.com/pub/a/onlamp/2004/10/28/design_by_contract_in_c.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10340</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10340"/>
		<updated>2007-11-29T02:27:31Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 3 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions. [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling. [10]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
* The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
* The post conditions are used by the supplier to develop code.&lt;br /&gt;
* The developed code is then given back to the client with pre-conditions.&lt;br /&gt;
* These pre-conditions have to be honored in order for the post conditions to be honored&lt;br /&gt;
* This in turn becomes a contract between both sides&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function in the Python Progamming language. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. If any of the pre or post conditions are not upheld, an exception is thrown. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; section defines a precondition. This precondition checks to make sure that the parameter a is an instance of a list. It also checks to see if each element within the list can be compared to all of the other elements within the list. Both of these preconditions need to be met in order for the sort function to work properly.  &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;post:&amp;quot; section defines a postcondition. There are three conditions that are required to be met at the end of the function. First the length of the list must never change during the sorting of the list. Second, no elements should be lost or gained from sorting the list. Lastly the list has to actually be sorted. These three conditions assures the integrity of the list. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; and &amp;quot;post:&amp;quot; together make up the contract of the function.&lt;br /&gt;
&lt;br /&gt;
* The function is then sorted.&lt;br /&gt;
&lt;br /&gt;
* The code after &amp;quot;# enable contract checking&amp;quot; is used to test the function and verify that the contract is upheld. The function is simply imported and ran with an arbitrary list. &lt;br /&gt;
&lt;br /&gt;
* The example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _initparms&amp;quot; is the initialization for the class while &amp;quot;def addNumber(self, number)&amp;quot; adds the number to the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _invariant(self):&amp;quot; is an invariant that uses an assertion to verify that the length of the list doesn't exceed 100 and that there is at least one element in the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def addNumber_pre_condition(self, number):&amp;quot; defines a pre-condition for the addNumber function. The conditions that have to be met are a variable must contain an integer only and the number must be less than 100 and greater than zero. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def length_post_condition(self, result):&amp;quot; Is the post condition for the length function. This insures that after the length is found that the list is never bigger than &lt;br /&gt;
&lt;br /&gt;
* The pre-condition for addNumber and the post-condition for length together make up the contract of this class. The contract is enforced through the assertions and invariants.&lt;br /&gt;
&lt;br /&gt;
* The example below is from link 4.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is a C example of two contracts for functions. Contracts  in c begin with &amp;quot;/**&amp;quot; and end with &amp;quot;*/&amp;quot;. They are placed directly on top of the function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
  pre: index &amp;gt;= 0&lt;br /&gt;
  pre: index &amp;lt; ary-&amp;gt;length&lt;br /&gt;
  post: return == ary-&amp;gt;ptr[index]&lt;br /&gt;
 */&lt;br /&gt;
void *DArray_get(DArray_T ary, long index)&lt;br /&gt;
{&lt;br /&gt;
  return ary-&amp;gt;ptr[index];&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
  post: return != NULL implies return-&amp;gt;length == ary-&amp;gt;length and&lt;br /&gt;
    forall (long i in 0...ary-&amp;gt;length | return-&amp;gt;ptr[i] == ary-&amp;gt;ptr[i])&lt;br /&gt;
 */&lt;br /&gt;
DArray_T DArray_copy(DArray_T ary)&lt;br /&gt;
{&lt;br /&gt;
  /* ... */&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;br /&gt;
# http://www.onlamp.com/pub/a/onlamp/2004/10/28/design_by_contract_in_c.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10339</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10339"/>
		<updated>2007-11-29T02:25:37Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions. [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling. [10]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
* The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
* The post conditions are used by the supplier to develop code.&lt;br /&gt;
* The developed code is then given back to the client with pre-conditions.&lt;br /&gt;
* These pre-conditions have to be honored in order for the post conditions to be honored&lt;br /&gt;
* This in turn becomes a contract between both sides&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function in the Python Progamming language. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. If any of the pre or post conditions are not upheld, an exception is thrown. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; section defines a precondition. This precondition checks to make sure that the parameter a is an instance of a list. It also checks to see if each element within the list can be compared to all of the other elements within the list. Both of these preconditions need to be met in order for the sort function to work properly.  &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;post:&amp;quot; section defines a postcondition. There are three conditions that are required to be met at the end of the function. First the length of the list must never change during the sorting of the list. Second, no elements should be lost or gained from sorting the list. Lastly the list has to actually be sorted. These three conditions assures the integrity of the list. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; and &amp;quot;post:&amp;quot; together make up the contract of the function.&lt;br /&gt;
&lt;br /&gt;
* The function is then sorted.&lt;br /&gt;
&lt;br /&gt;
* The code after &amp;quot;# enable contract checking&amp;quot; is used to test the function and verify that the contract is upheld. The function is simply imported and ran with an arbitrary list. &lt;br /&gt;
&lt;br /&gt;
* The example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _initparms&amp;quot; is the initialization for the class while &amp;quot;def addNumber(self, number)&amp;quot; adds the number to the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _invariant(self):&amp;quot; is an invariant that uses an assertion to verify that the length of the list doesn't exceed 100 and that there is at least one element in the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def addNumber_pre_condition(self, number):&amp;quot; defines a pre-condition for the addNumber function. The conditions that have to be met are a variable must contain an integer only and the number must be less than 100 and greater than zero. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def length_post_condition(self, result):&amp;quot; Is the post condition for the length function. This insures that after the length is found that the list is never bigger than &lt;br /&gt;
&lt;br /&gt;
* The pre-condition for addNumber and the post-condition for length together make up the contract of this class. The contract is enforced through the assertions and invariants.&lt;br /&gt;
&lt;br /&gt;
* The example below is from link 4.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;br /&gt;
# http://www.onlamp.com/pub/a/onlamp/2004/10/28/design_by_contract_in_c.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10336</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10336"/>
		<updated>2007-11-29T02:22:38Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions. [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling. [10]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
* The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
* The post conditions are used by the supplier to develop code.&lt;br /&gt;
* The developed code is then given back to the client with pre-conditions.&lt;br /&gt;
* These pre-conditions have to be honored in order for the post conditions to be honored&lt;br /&gt;
* This in turn becomes a contract between both sides&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function in the Python Progamming language. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. If any of the pre or post conditions are not upheld, an exception is thrown. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; section defines a precondition. This precondition checks to make sure that the parameter a is an instance of a list. It also checks to see if each element within the list can be compared to all of the other elements within the list. Both of these preconditions need to be met in order for the sort function to work properly.  &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;post:&amp;quot; section defines a postcondition. There are three conditions that are required to be met at the end of the function. First the length of the list must never change during the sorting of the list. Second, no elements should be lost or gained from sorting the list. Lastly the list has to actually be sorted. These three conditions assures the integrity of the list. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; and &amp;quot;post:&amp;quot; together make up the contract of the function.&lt;br /&gt;
&lt;br /&gt;
* The function is then sorted.&lt;br /&gt;
&lt;br /&gt;
* The code after &amp;quot;# enable contract checking&amp;quot; is used to test the function and verify that the contract is upheld. The function is simply imported and ran with an arbitrary list. &lt;br /&gt;
&lt;br /&gt;
* The example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _initparms&amp;quot; is the initialization for the class while &amp;quot;def addNumber(self, number)&amp;quot; adds the number to the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _invariant(self):&amp;quot; is an invariant that uses an assertion to verify that the length of the list doesn't exceed 100 and that there is at least one element in the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def addNumber_pre_condition(self, number):&amp;quot; defines a pre-condition for the addNumber function. The conditions that have to be met are a variable must contain an integer only and the number must be less than 100 and greater than zero. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def length_post_condition(self, result):&amp;quot; Is the post condition for the length function. This insures that after the length is found that the list is never bigger than &lt;br /&gt;
&lt;br /&gt;
* The pre-condition for addNumber and the post-condition for length together make up the contract of this class. The contract is enforced through the assertions and invariants.&lt;br /&gt;
&lt;br /&gt;
* The example below is from link 4.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10335</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10335"/>
		<updated>2007-11-29T02:21:29Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions. [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling. [10]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
* The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
* The post conditions are used by the supplier to develop code.&lt;br /&gt;
* The developed code is then given back to the client with pre-conditions.&lt;br /&gt;
* These pre-conditions have to be honored in order for the post conditions to be honored&lt;br /&gt;
* This in turn becomes a contract between both sides&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function in the Python Progamming language. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. If any of the pre or post conditions are not upheld, an exception is thrown. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; section defines a precondition. This precondition checks to make sure that the parameter a is an instance of a list. It also checks to see if each element within the list can be compared to all of the other elements within the list. Both of these preconditions need to be met in order for the sort function to work properly.  &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;post:&amp;quot; section defines a postcondition. There are three conditions that are required to be met at the end of the function. First the length of the list must never change during the sorting of the list. Second, no elements should be lost or gained from sorting the list. Lastly the list has to actually be sorted. These three conditions assures the integrity of the list. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; and &amp;quot;post:&amp;quot; together make up the contract of the function.&lt;br /&gt;
&lt;br /&gt;
* The function is then sorted.&lt;br /&gt;
&lt;br /&gt;
* The code after &amp;quot;# enable contract checking&amp;quot; is used to test the function and verify that the contract is upheld. The function is simply imported and ran with an arbitrary list. &lt;br /&gt;
&lt;br /&gt;
* The example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _initparms&amp;quot; is the initialization for the class while &amp;quot;def addNumber(self, number)&amp;quot; adds the number to the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _invariant(self):&amp;quot; is an invariant that uses an assertion to verify that the length of the list doesn't exceed 100 and that there is at least one element in the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def addNumber_pre_condition(self, number):&amp;quot; defines a pre-condition for the addNumber function. The conditions that have to be met are a variable must contain an integer only and the number must be less than 100 and greater than zero. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def length_post_condition(self, result):&amp;quot; Is the post condition for the length function. This insures that after the length is found that the list is never bigger than &lt;br /&gt;
&lt;br /&gt;
* The example below is from link 4.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10333</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10333"/>
		<updated>2007-11-29T02:18:55Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions. [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling. [10]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
* The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
* The post conditions are used by the supplier to develop code.&lt;br /&gt;
* The developed code is then given back to the client with pre-conditions.&lt;br /&gt;
* These pre-conditions have to be honored in order for the post conditions to be honored&lt;br /&gt;
* This in turn becomes a contract between both sides&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function in the Python Progamming language. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. If any of the pre or post conditions are not upheld, an exception is thrown. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; section defines a precondition. This precondition checks to make sure that the parameter a is an instance of a list. It also checks to see if each element within the list can be compared to all of the other elements within the list. Both of these preconditions need to be met in order for the sort function to work properly.  &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;post:&amp;quot; section defines a postcondition. There are three conditions that are required to be met at the end of the function. First the length of the list must never change during the sorting of the list. Second, no elements should be lost or gained from sorting the list. Lastly the list has to actually be sorted. These three conditions assures the integrity of the list. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; and &amp;quot;post:&amp;quot; together make up the contract of the function.&lt;br /&gt;
&lt;br /&gt;
* The function is then sorted.&lt;br /&gt;
&lt;br /&gt;
* The code after &amp;quot;# enable contract checking&amp;quot; is used to test the function and verify that the contract is upheld. The function is simply imported and ran with an arbitrary list. &lt;br /&gt;
&lt;br /&gt;
* The example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _initparms&amp;quot; is the initialization for the class while &amp;quot;def addNumber(self, number)&amp;quot; adds the number to the list.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def _invariant(self):&amp;quot; is an invariant that uses an assertion to verify that every number in the list is under 100 at any given time.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def addNumber_pre_condition(self, number):&amp;quot; defines a pre-condition for the addNumber function. The conditions that have to be met are a variable must contain an integer only and the number must be less than 100 and greater than zero. &lt;br /&gt;
&lt;br /&gt;
* &amp;quot;def length_post_condition(self, result):&amp;quot;&lt;br /&gt;
&lt;br /&gt;
* The example below is from link 4.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10325</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10325"/>
		<updated>2007-11-29T01:59:29Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions. [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling. [10]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
* The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
* The post conditions are used by the supplier to develop code.&lt;br /&gt;
* The developed code is then given back to the client with pre-conditions.&lt;br /&gt;
* These pre-conditions have to be honored in order for the post conditions to be honored&lt;br /&gt;
* This in turn becomes a contract between both sides&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function in the Python Progamming language. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. If any of the pre or post conditions are not upheld, an exception is thrown. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; section defines a precondition. This precondition checks to make sure that the parameter a is an instance of a list. It also checks to see if each element within the list can be compared to all of the other elements within the list. Both of these preconditions need to be met in order for the sort function to work properly.  &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;post:&amp;quot; section defines a postcondition. There are three conditions that are required to be met at the end of the function. First the length of the list must never change during the sorting of the list. Second, no elements should be lost or gained from sorting the list. Lastly the list has to actually be sorted. These three conditions assures the integrity of the list. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; and &amp;quot;post:&amp;quot; together make up the contract of the function.&lt;br /&gt;
&lt;br /&gt;
* The function is then sorted.&lt;br /&gt;
&lt;br /&gt;
* The code after &amp;quot;# enable contract checking&amp;quot; is used to test the function and verify that the contract is upheld. The function is simply imported and ran with an arbitrary list. &lt;br /&gt;
&lt;br /&gt;
* The example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows how invariants and assetions can be used designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10324</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10324"/>
		<updated>2007-11-29T01:56:05Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions. [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling. [10]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
* The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
* The post conditions are used by the supplier to develop code.&lt;br /&gt;
* The developed code is then given back to the client with pre-conditions.&lt;br /&gt;
* These pre-conditions have to be honored in order for the post conditions to be honored&lt;br /&gt;
* This in turn becomes a contract between both sides&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function in the Python Progamming language. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. If any of the pre or post conditions are not upheld, an exception is thrown. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; section defines a precondition. This precondition checks to make sure that the parameter a is an instance of a list. It also checks to see if each element within the list can be compared to all of the other elements within the list. Both of these preconditions need to be met in order for the sort function to work properly.  &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;post:&amp;quot; section defines a postcondition. There are three conditions that are required to be met at the end of the function. First the length of the list must never change during the sorting of the list. Second, no elements should be lost or gained from sorting the list. Lastly the list has to actually be sorted. These three conditions assures the integrity of the list. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; and &amp;quot;post:&amp;quot; together make up the contract of the function.&lt;br /&gt;
&lt;br /&gt;
* The function is then sorted.&lt;br /&gt;
&lt;br /&gt;
* The code after &amp;quot;# enable contract checking&amp;quot; is used to test the function and verify that the contract is upheld. The function is simply imported and ran with an arbitrary list. &lt;br /&gt;
&lt;br /&gt;
* The example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10323</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10323"/>
		<updated>2007-11-29T01:55:57Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions. [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling. [10]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
* The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
* The post conditions are used by the supplier to develop code.&lt;br /&gt;
* The developed code is then given back to the client with pre-conditions.&lt;br /&gt;
* These pre-conditions have to be honored in order for the post conditions to be honored&lt;br /&gt;
* This in turn becomes a contract between both sides&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function in the Python Progamming language. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. If any of the pre or post conditions are not upheld, an exception is thrown. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; section defines a precondition. This precondition checks to make sure that the parameter a is an instance of a list. It also checks to see if each element within the list can be compared to all of the other elements within the list. Both of these preconditions need to be met in order for the sort function to work properly.  &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;post:&amp;quot; section defines a postcondition. There are three conditions that are required to be met at the end of the function. First the length of the list must never change during the sorting of the list. Second, no elements should be lost or gained from sorting the list. Lastly the list has to actually be sorted. These three conditions assures the integrity of the list. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; and &amp;quot;post:&amp;quot; together make up the contract of the function.&lt;br /&gt;
&lt;br /&gt;
* The function is then sorted.&lt;br /&gt;
&lt;br /&gt;
* The code after &amp;quot;# enable contract checking&amp;quot; is used to test the function and verify that the contract is upheld. The function is simply imported and ran with an arbitrary list. &lt;br /&gt;
&lt;br /&gt;
* The example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10322</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10322"/>
		<updated>2007-11-29T01:55:34Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions. [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling. [10]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
* The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
* The post conditions are used by the supplier to develop code.&lt;br /&gt;
* The developed code is then given back to the client with pre-conditions.&lt;br /&gt;
* These pre-conditions have to be honored in order for the post conditions to be honored&lt;br /&gt;
* This in turn becomes a contract between both sides&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function in the Python Progamming language. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. If any of the pre or post conditions are not upheld, an exception is thrown. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; section defines a precondition. This precondition checks to make sure that the parameter a is an instance of a list. It also checks to see if each element within the list can be compared to all of the other elements within the list. Both of these preconditions need to be met in order for the sort function to work properly.  &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;post:&amp;quot; section defines a postcondition. There are three conditions that are required to be met at the end of the function. First the length of the list must never change during the sorting of the list. Second, no elements should be lost or gained from sorting the list. Lastly the list has to actually be sorted. These three conditions assures the integrity of the list. &lt;br /&gt;
&lt;br /&gt;
* The &amp;quot;pre:&amp;quot; and &amp;quot;post:&amp;quot; together make up the contract of the function.&lt;br /&gt;
&lt;br /&gt;
* The function is then sorted.&lt;br /&gt;
&lt;br /&gt;
* The code after &amp;quot;# enable contract checking&amp;quot; is used to test the function and verify that the contract is upheld. The function is simply imported and ran with an arbitrary list. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* The example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10321</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10321"/>
		<updated>2007-11-29T01:55:08Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions. [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling. [10]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
* The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
* The post conditions are used by the supplier to develop code.&lt;br /&gt;
* The developed code is then given back to the client with pre-conditions.&lt;br /&gt;
* These pre-conditions have to be honored in order for the post conditions to be honored&lt;br /&gt;
* This in turn becomes a contract between both sides&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function in the Python Progamming language. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. If any of the pre or post conditions are not upheld, an exception is thrown. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;pre:&amp;quot; section defines a precondition. This precondition checks to make sure that the parameter a is an instance of a list. It also checks to see if each element within the list can be compared to all of the other elements within the list. Both of these preconditions need to be met in order for the sort function to work properly.  &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;post:&amp;quot; section defines a postcondition. There are three conditions that are required to be met at the end of the function. First the length of the list must never change during the sorting of the list. Second, no elements should be lost or gained from sorting the list. Lastly the list has to actually be sorted. These three conditions assures the integrity of the list. &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;pre:&amp;quot; and &amp;quot;post:&amp;quot; together make up the contract of the function.&lt;br /&gt;
&lt;br /&gt;
The function is then sorted.&lt;br /&gt;
&lt;br /&gt;
The code after &amp;quot;# enable contract checking&amp;quot; is used to test the function and verify that the contract is upheld. The function is simply imported and ran with an arbitrary list. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10195</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10195"/>
		<updated>2007-11-28T22:38:17Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Implementation and Reasons For */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions. [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling. [10]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
* The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
* The post conditions are used by the supplier to develop code.&lt;br /&gt;
* The developed code is then given back to the client with pre-conditions.&lt;br /&gt;
* These pre-conditions have to be honored in order for the post conditions to be honored&lt;br /&gt;
* This in turn becomes a contract between both sides&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10175</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10175"/>
		<updated>2007-11-28T22:24:44Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling.&lt;br /&gt;
*  &lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
*The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
*The post conditions are used&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;br /&gt;
# http://www.muc.de/~hoelzl/tools/dbc/dbc.lisp&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10172</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10172"/>
		<updated>2007-11-28T22:22:57Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling.&lt;br /&gt;
*  &lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
*The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
*The post conditions are used&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;br /&gt;
# http://archive.eiffel.com/doc/manuals/technology/contract/&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10171</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10171"/>
		<updated>2007-11-28T22:22:17Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Implementation and Reasons For */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract. [9]&lt;br /&gt;
* Able to use assertions to uphold the contract. [9]&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions [9]&lt;br /&gt;
* Handles abmormal cases which goes great with exception handling.&lt;br /&gt;
*  &lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
*The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
*The post conditions are used&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10167</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10167"/>
		<updated>2007-11-28T22:18:06Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Why Use Programming By Contract? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server.&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract.&lt;br /&gt;
* Able to use assertions to uphold the contract.&lt;br /&gt;
* Tests for pre-conditions as well as post-conditions&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
*The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
*The post conditions are used&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10143</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10143"/>
		<updated>2007-11-28T20:37:18Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Why Use Programming By Contract? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server.&lt;br /&gt;
* Works very well with Inheritance. Any inherited methods need to conform to the contract.&lt;br /&gt;
* Able to use assertions to uphold the contract.&lt;br /&gt;
* Test for pre-conditions as well as post-conditions&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
*The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
*The post conditions are used&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10132</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10132"/>
		<updated>2007-11-28T20:29:07Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Implementation and Reasons For */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
*The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
*The post conditions are used&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10131</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10131"/>
		<updated>2007-11-28T20:28:47Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
*The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
*The post conditions are used&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10130</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10130"/>
		<updated>2007-11-28T20:26:26Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* How it works */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
*The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
*The post conditions are used&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Previous information came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10129</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10129"/>
		<updated>2007-11-28T20:25:44Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Programming By Contract */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. [9]&lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
*The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
*The post conditions are used&lt;br /&gt;
&lt;br /&gt;
''' Following came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10127</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10127"/>
		<updated>2007-11-28T20:22:58Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. &lt;br /&gt;
&lt;br /&gt;
==Implementation and Reasons For==&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
*The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
*The post conditions are used&lt;br /&gt;
&lt;br /&gt;
''' Following came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10126</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10126"/>
		<updated>2007-11-28T20:21:31Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. &lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
*The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
*The post conditions are used&lt;br /&gt;
&lt;br /&gt;
''' Following came from reference [9]. '''&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10125</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10125"/>
		<updated>2007-11-28T20:21:04Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
'''&lt;br /&gt;
Programming by contract (PBC)''' is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1] Programming by contract uses assertions to enforce the contract between requirements and code. PBC puts the responsibilities on the client to specify the conditions of the functions and other code to meet while and the supplier to honor these requirements that form a contract. &lt;br /&gt;
&lt;br /&gt;
===How it works===&lt;br /&gt;
&lt;br /&gt;
*The client has the responsibility of specifying the post conditions for the supplier. &lt;br /&gt;
*The post conditions are used&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
Following came from reference [9].&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
* PBC helps to define and show the responsibilities of the client and the server. [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
* [9]&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10104</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10104"/>
		<updated>2007-11-28T20:02:59Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
&lt;br /&gt;
Programming by contract (PBC) is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://onestepback.org/index.cgi/Tech/Programming/DbcAndTesting.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10102</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10102"/>
		<updated>2007-11-28T20:00:06Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
&lt;br /&gt;
Programming by contract (PBC) is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Why Use Programming By Contract?===&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10043</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10043"/>
		<updated>2007-11-28T17:51:52Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 5 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
&lt;br /&gt;
Programming by contract (PBC) is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10042</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10042"/>
		<updated>2007-11-28T17:51:36Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 4 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
&lt;br /&gt;
Programming by contract (PBC) is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10041</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10041"/>
		<updated>2007-11-28T17:51:14Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 3 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
&lt;br /&gt;
Programming by contract (PBC) is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10040</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10040"/>
		<updated>2007-11-28T17:50:48Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
&lt;br /&gt;
Programming by contract (PBC) is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10039</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=10039"/>
		<updated>2007-11-28T17:50:26Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
&lt;br /&gt;
Programming by contract (PBC) is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot; &lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=9155</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=9155"/>
		<updated>2007-11-19T03:09:19Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
&lt;br /&gt;
Programming by contract (PBC) is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. The example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. The example below is from link 4.&lt;br /&gt;
&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot; &lt;br /&gt;
&lt;br /&gt;
==Example 3==&lt;br /&gt;
&lt;br /&gt;
This is a C++ example of a bar class. The class takes a value but assures that the value is greater than zero. This example is from link 5.&lt;br /&gt;
&lt;br /&gt;
public class Bar&lt;br /&gt;
{&lt;br /&gt;
  public void m1( int value )&lt;br /&gt;
  {&lt;br /&gt;
    assert 0 &amp;lt;= value : &amp;quot;Value must be non-negative: value= &amp;quot; + value;&lt;br /&gt;
    System.out.println( &amp;quot;OK&amp;quot; );&lt;br /&gt;
  }&lt;br /&gt;
  public static void main( String[] args )&lt;br /&gt;
  {&lt;br /&gt;
    Bar bar = new Bar();&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1(  1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( 1 );&lt;br /&gt;
    System.out.print( &amp;quot;bar.m1( -1 ): &amp;quot; );&lt;br /&gt;
    bar.m1( -1 );&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Example 4==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java and is an implementation of a stack. Pre and post conditions are implemented as well as an invariant. The example is taken from link 7.&lt;br /&gt;
&lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
   public Stack()&lt;br /&gt;
   {  &lt;br /&gt;
      objects = new Object[MAX];&lt;br /&gt;
      top = 0;&lt;br /&gt;
   }&lt;br /&gt;
   public void Push(Object o)&lt;br /&gt;
   {  &lt;br /&gt;
      objects[top] = o;&lt;br /&gt;
      top++;&lt;br /&gt;
   }&lt;br /&gt;
   public Object Pop()&lt;br /&gt;
   {  &lt;br /&gt;
      top--;&lt;br /&gt;
      return  objects[top];&lt;br /&gt;
   }&lt;br /&gt;
   public int Size()&lt;br /&gt;
   {  &lt;br /&gt;
      return  top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private int Remaining()&lt;br /&gt;
   {  &lt;br /&gt;
      assert (false);&lt;br /&gt;
      return  MAX - top;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   private Object[] objects;&lt;br /&gt;
   private int top;&lt;br /&gt;
   private static final int MAX = 10;&lt;br /&gt;
   &lt;br /&gt;
   preconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;lt; MAX;&lt;br /&gt;
      Pop:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   postconditions()&lt;br /&gt;
   {  &lt;br /&gt;
      Push:&lt;br /&gt;
      top &amp;gt; 0;&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   invariants()&lt;br /&gt;
   {  &lt;br /&gt;
      top &amp;gt;= 0;&lt;br /&gt;
      top &amp;lt;= MAX;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Example 5==&lt;br /&gt;
&lt;br /&gt;
This example is written in Java. Below is two functions that use assertions to check for null and in range. The example is taken from link 8.&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that a specified object is not null.&lt;br /&gt;
 * @param object - the object that should not be null.&lt;br /&gt;
 * @param name - the name of the object to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean notNull(Object object, String name) {&lt;br /&gt;
	assert object != null: name + &amp;quot; must not be null&amp;quot;;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * This method asserts that an int value is within a specified range.&lt;br /&gt;
 * @param value - the value that must be within the specified range.&lt;br /&gt;
 * @param min - the minimum value specified of the specified range.&lt;br /&gt;
 * @param max - the maximum value of the specified range.&lt;br /&gt;
 * @param name - the name of the value to be used for error reporting.&lt;br /&gt;
 */&lt;br /&gt;
public final static boolean inRange(int value, int min, int max, String name) {&lt;br /&gt;
	assert value &amp;gt;= min &amp;amp;&amp;amp; value &amp;lt;= max: name + &amp;quot; must be in the range [&amp;quot; + min + &amp;quot; to &amp;quot; + max + &amp;quot;]. Value is &amp;quot; + value;&lt;br /&gt;
	return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html?page=2&lt;br /&gt;
# http://www.codeproject.com/csharp/designbycontract.asp&lt;br /&gt;
# http://mozart-dev.sourceforge.net/pi-assert.html&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=9150</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=9150"/>
		<updated>2007-11-19T02:47:45Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
&lt;br /&gt;
Programming by contract (PBC) is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example 1==&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. Example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&lt;br /&gt;
This example is an API example that adds a number to a list. The number has to be between 0 and 100. This example shows different ways of designing by contract. There are three functions and each one defines ever a invariant, pre-condition, or a post-condition. Notice that the assert keyword is used for upholding the contract of each condition. Example below is from link 4&lt;br /&gt;
&lt;br /&gt;
class SampleTool(StandardTool):&lt;br /&gt;
&lt;br /&gt;
      def _initparms(self):&lt;br /&gt;
          self.lst = []&lt;br /&gt;
&lt;br /&gt;
      def _invariant(self):&lt;br /&gt;
          assert len(self.lst) &amp;lt; 100&lt;br /&gt;
&lt;br /&gt;
      def addNumber(self, number):&lt;br /&gt;
          self.lst.append(number)&lt;br /&gt;
&lt;br /&gt;
      def addNumber_pre_condition(self, number):&lt;br /&gt;
          assert type(number) == type(1), &amp;quot;Type must be integer&amp;quot;&lt;br /&gt;
          assert (number &amp;lt; 100) and (number &amp;gt; 0), &amp;quot;Number must be less than 100, but greater than 0&amp;quot;&lt;br /&gt;
&lt;br /&gt;
      def length(self):&lt;br /&gt;
          return len(self.lst)&lt;br /&gt;
&lt;br /&gt;
      def length_post_condition(self, result):&lt;br /&gt;
          assert (result &amp;lt; 100) and (result &amp;gt;= 0), &amp;quot;Result must be less than 100, but greater or equal 0&amp;quot; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;br /&gt;
# http://pyds.muensterland.org/wiki/programmingbycontractforwebservices.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=9145</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=9145"/>
		<updated>2007-11-19T02:41:30Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
&lt;br /&gt;
Programming by contract (PBC) is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications. These contracts are enforced by assertions.[1]&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
This example shows design by contract using a sort function. It defines pre and post conditions for the function and also tests the function to make sure that these conditions are upheld. Example below is from link 3.&lt;br /&gt;
&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = [1, 1, 1, 1, 1, 2, 2, 1]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    [1, 1, 1, 1, 1, 1, 2, 2]&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a = 'the quick brown fox jumped over the lazy dog'.split()&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; sort(a)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; a&lt;br /&gt;
    ['brown', 'dog', 'fox', 'jumped', 'lazy', 'over', 'quick', 'the', 'the']&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    a.sort()&lt;br /&gt;
&lt;br /&gt;
# enable contract checking&lt;br /&gt;
import contract&lt;br /&gt;
contract.checkmod(__name__)&lt;br /&gt;
&lt;br /&gt;
def _test():&lt;br /&gt;
    import doctest, sort&lt;br /&gt;
    return doctest.testmod(sort)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    _test()&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;br /&gt;
# http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html?page=1&lt;br /&gt;
# http://www.wayforward.net/pycontract/&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=9136</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_c4&amp;diff=9136"/>
		<updated>2007-11-19T02:22:50Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
In class, we had some difficulty coming up with good examples of programming by contract. Find some concise ones that illustrate the principle well, and are accessible to a general audience of programmers.&lt;br /&gt;
&lt;br /&gt;
===Programming By Contract===&lt;br /&gt;
&lt;br /&gt;
Programming by contract (PBC) is also called design by contract. It is a form of progamming that deals with pre conditions, post conditions, and invariants. These are used in a client/supplier model. Pre conditions, post conditons, and invariants are specified first. After that, the code is written based on the specified conditions and invariants. It can be seen as a contract because the code needs to abide by the specifications.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# http://www.dugaldmorrow.com/index.php?option=com_content&amp;amp;task=view&amp;amp;id=19&amp;amp;Itemid=38&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=8006</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 10 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=8006"/>
		<updated>2007-10-29T15:39:22Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
&lt;br /&gt;
Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
Inheritance is the process of one class having access to another class's attributes. Inheritance occurs when a class, also called the superclass, is created with generalized methods and attributes, and then another class is created as a subclass to the first class. The subclass provides provides detailed methods and attributes and automatically has access to methods and data members of the superclass. These subclasses can also override the generalized methods of the super class. Sometimes subclasses inherit from more than one super class; the occurrance of this is naturally called multiple inheritance.[1]&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
Delegation is the process of assigning the implementation of a method, within a class, to another method. Generally, the implementation responsibility is forwarded to the parent class. If the parent class can not handle the implementation responsibility, it forwards it to its parent and so on and so on. &lt;br /&gt;
This forwarding allows a class to dynamically change by changing the parent class relations. Delegation is also called dynamic inheritance.[2]&lt;br /&gt;
&lt;br /&gt;
==Inheritance vs Delegation==&lt;br /&gt;
&lt;br /&gt;
Inheritance and delegation are used in different situations in object oriented programming. Although they can be used simultaneously, its rarely done due to the difficulty in it. Most of the time people use them interchangeably but are doing so incorrectly. &lt;br /&gt;
&lt;br /&gt;
===When to use Inheritance===&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used when two objects are of the same type but one of the objects needs to handle somethings differently. The second object would inherit from the first and simply rewrite the code needed in the second object.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used in is-a relationships between objects. [3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used to sub categorize objects.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance should be restricted to objects of the same type.[3]&lt;br /&gt;
&lt;br /&gt;
* When the base class is to general but the functionality is still needed.&lt;br /&gt;
&lt;br /&gt;
=== When to use Delegation===&lt;br /&gt;
&lt;br /&gt;
* Delegation is used when two objects aren't of the same type but one has methods and attributes that the other wants to use internally.[3]&lt;br /&gt;
&lt;br /&gt;
* Delegation is used in has-a relationships where the type of object that is included may change during runtime. &lt;br /&gt;
&lt;br /&gt;
* Delegation is used when methods implementation needs to be handled by some other class within the parent tree. The class that will handle the method isn't known right away. [6]&lt;br /&gt;
&lt;br /&gt;
* When code execution within objects needs to be determined dynamically. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
==Inheritance is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Inheritance is better in this example because any specific account type will require the basic attributes of an account. An interest bearing account also &amp;quot;is a&amp;quot; account. Delegation would not be great in this case because allowing for methods to be determined dynamically opens up opportunities for hacker and calculation issues. Example below is from link [7] and link [8].&lt;br /&gt;
&lt;br /&gt;
public class Account { &lt;br /&gt;
   protected double balance; // Constructor to initialise balance &lt;br /&gt;
      &lt;br /&gt;
   public Account( double amount ) { &lt;br /&gt;
      balance = amount; &lt;br /&gt;
   } &lt;br /&gt;
   // Overloaded constructor for empty balance &lt;br /&gt;
   public Account() { &lt;br /&gt;
      balance = 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public void deposit( double amount ) { &lt;br /&gt;
      balance += amount; &lt;br /&gt;
   } &lt;br /&gt;
   public double withdraw( double amount ) { &lt;br /&gt;
      // See if amount can be withdrawn &lt;br /&gt;
      if (balance &amp;gt;= amount) { &lt;br /&gt;
         balance -= amount; return amount; &lt;br /&gt;
      } else &lt;br /&gt;
      // Withdrawal not allowed return 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public double getbalance() { &lt;br /&gt;
      return balance; &lt;br /&gt;
   } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class InterestBearingAccount extends Account{&lt;br /&gt;
   private static double default_interest = 7.95;&lt;br /&gt;
   private double interest_rate;&lt;br /&gt;
&lt;br /&gt;
   // Overloaded constructor accepting balance and an interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount, double interest){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor accepting balance with a default interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount ){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor with empty balance and a default interest rate&lt;br /&gt;
   public InterestBearingAccount(){&lt;br /&gt;
       balance = 0.0;&lt;br /&gt;
       interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
	&lt;br /&gt;
   public void add_monthly_interest(){&lt;br /&gt;
      // Add interest to our account&lt;br /&gt;
      balance = balance + 		&lt;br /&gt;
     (balance * interest_rate / 100) / 12;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
Here is another example where inheritance is better than delegation. The generalized shape interface is used as a basis for all of the subclasses.&lt;br /&gt;
Notice that each subclass is designed to personal specifications for the object. Here there would be no need for any of the code to dynamically change. Example below is from link [9].&lt;br /&gt;
&lt;br /&gt;
public interface Shape{   &lt;br /&gt;
    public double getArea();&lt;br /&gt;
    public double getPerimeter();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Rectangle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int width;&lt;br /&gt;
    private int height;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(){&lt;br /&gt;
        this(1,1);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int width, int height){&lt;br /&gt;
        this.width = width;&lt;br /&gt;
        this.height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getWidth(){&lt;br /&gt;
        return this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getHeight(){&lt;br /&gt;
        return this.height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * this.height + 2 * this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return width * height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;width: &amp;quot; + this.width + &amp;quot;, height: &amp;quot; + this.height;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Square extends Rectangle{   &lt;br /&gt;
    public Square(int lengthOfSide){&lt;br /&gt;
        super(lengthOfSide, lengthOfSide);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getSide(){&lt;br /&gt;
        return getWidth();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;Length of Side: &amp;quot; + getSide();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Circle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int radius;&lt;br /&gt;
&lt;br /&gt;
    public Circle(int radius){&lt;br /&gt;
        this.radius = radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return Math.PI * this.radius * this.radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * Math.PI * this.radius;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Delegation is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Delegation is better in this example because class Student doesnt know if it wants to use class Classroom or class Cafeteria's implementation of the f and g methods. During the running of the code class Student can change which class implementation of the functions it points to by calling toClassroom or toCafeteria. Students go between classrooms, libraries, and the cafeteria pretty often throughout a day, delegation allows for an easy change betewen these locations. Inheritance isnt able to handle this dynamically. Any implementation of the code would have to be either in the current class or its parent. It couldnt be in a sibling class. Example below is from link [11]. I have adapted it so that it is more clear to understand. SchoolLocation = I, A = Classroom, B = Cafeteria, C = Student;&lt;br /&gt;
&lt;br /&gt;
interface SchoolLocation {&lt;br /&gt;
     void f();&lt;br /&gt;
     void g();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Classroom implements School {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;A: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;A: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cafeteria implements School {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;B: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;B: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Sudent implements School {&lt;br /&gt;
     // delegation&lt;br /&gt;
     SchoolLocation location = new Classroom();&lt;br /&gt;
 &lt;br /&gt;
     public void f() { location.f(); }&lt;br /&gt;
     public void g() { location.g(); }&lt;br /&gt;
 &lt;br /&gt;
     // normal attributes&lt;br /&gt;
     void toClassroom() { location = new Classroom(); }&lt;br /&gt;
     void toCafeteria() { location = new Cafeteria(); }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
Below is the link to a great example of how to use delegation for a real world scenario. It isn't code but is in the form of diagrams. &lt;br /&gt;
This link uses a school registration system as an example. The school registration system consists of students and faculty. This is then broken down further into graduate and undergraduate students. Graduate students can be phd or master students. Some graduate students can also teach classes and be faculty. This brings up a situation where a graduate student needs to inherit from two places. Many know that multiple inheritance isn't a good dway to handle this. The example uses an delegation and interfaces to handle this issue. &lt;br /&gt;
&lt;br /&gt;
http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Exampleofdelegation.html&lt;br /&gt;
&lt;br /&gt;
http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Addingcomplexity.html&lt;br /&gt;
&lt;br /&gt;
http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Crossingboundaries.html&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
# http://searchsmb.techtarget.com/sDefinition/0,,sid44_gci212351,00.html&lt;br /&gt;
# http://www.ccs.neu.edu/research/demeter/papers/context-journal/node3.html&lt;br /&gt;
# http://www.tek-tips.com/viewthread.cfm?qid=372254&lt;br /&gt;
# http://www.python.org/ftp/python/doc/delegation.ps&lt;br /&gt;
# http://www.softwarefederation.com/csci4448/courseNotes/05_ObjectOrientedDesign.pdf&lt;br /&gt;
# http://www.javaworld.com/javaworld/javaqa/2001-09/01-qa-0914-delegate.html&lt;br /&gt;
# http://www.javacoffeebreak.com/java102/Account.java&lt;br /&gt;
# http://www.javacoffeebreak.com/java104/java104.html&lt;br /&gt;
# http://www.cs.utexas.edu/~scottm/cs305j/codingSamples.htm&lt;br /&gt;
# http://www.tanguay.info/web/codeExample.php?id=492&lt;br /&gt;
# http://en.wikipedia.org/wiki/Delegation_pattern&lt;br /&gt;
# http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Exampleofdelegation.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=8005</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 10 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=8005"/>
		<updated>2007-10-29T15:26:07Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
&lt;br /&gt;
Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
Inheritance is the process of one class having access to another class's attributes. Inheritance occurs when a class, also called the superclass, is created with generalized methods and attributes, and then another class is created as a subclass to the first class. The subclass provides provides detailed methods and attributes and automatically has access to methods and data members of the superclass. These subclasses can also override the generalized methods of the super class. Sometimes subclasses inherit from more than one super class; the occurrance of this is naturally called multiple inheritance.[1]&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
Delegation is the process of assigning the implementation of a method, within a class, to another method. Generally, the implementation responsibility is forwarded to the parent class. If the parent class can not handle the implementation responsibility, it forwards it to its parent and so on and so on. &lt;br /&gt;
This forwarding allows a class to dynamically change by changing the parent class relations. Delegation is also called dynamic inheritance.[2]&lt;br /&gt;
&lt;br /&gt;
==Inheritance vs Delegation==&lt;br /&gt;
&lt;br /&gt;
Inheritance and delegation are used in different situations in object oriented programming. Although they can be used simultaneously, its rarely done due to the difficulty in it. Most of the time people use them interchangeably but are doing so incorrectly. &lt;br /&gt;
&lt;br /&gt;
===When to use Inheritance===&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used when two objects are of the same type but one of the objects needs to handle somethings differently. The second object would inherit from the first and simply rewrite the code needed in the second object.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used in is-a relationships between objects. [3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used to sub categorize objects.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance should be restricted to objects of the same type.[3]&lt;br /&gt;
&lt;br /&gt;
* When the base class is to general but the functionality is still needed.&lt;br /&gt;
&lt;br /&gt;
=== When to use Delegation===&lt;br /&gt;
&lt;br /&gt;
* Delegation is used when two objects aren't of the same type but one has methods and attributes that the other wants to use internally.[3]&lt;br /&gt;
&lt;br /&gt;
* Delegation is used in has-a relationships where the type of object that is included may change during runtime. &lt;br /&gt;
&lt;br /&gt;
* Delegation is used when methods implementation needs to be handled by some other class within the parent tree. The class that will handle the method isn't known right away. [6]&lt;br /&gt;
&lt;br /&gt;
* When code execution within objects needs to be determined dynamically. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
==Inheritance is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Inheritance is better in this example because any specific account type will require the basic attributes of an account. An interest bearing account also &amp;quot;is a&amp;quot; account. Delegation would not be great in this case because allowing for methods to be determined dynamically opens up opportunities for hacker and calculation issues. Example below is from link [7] and link [8].&lt;br /&gt;
&lt;br /&gt;
public class Account { &lt;br /&gt;
   protected double balance; // Constructor to initialise balance &lt;br /&gt;
      &lt;br /&gt;
   public Account( double amount ) { &lt;br /&gt;
      balance = amount; &lt;br /&gt;
   } &lt;br /&gt;
   // Overloaded constructor for empty balance &lt;br /&gt;
   public Account() { &lt;br /&gt;
      balance = 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public void deposit( double amount ) { &lt;br /&gt;
      balance += amount; &lt;br /&gt;
   } &lt;br /&gt;
   public double withdraw( double amount ) { &lt;br /&gt;
      // See if amount can be withdrawn &lt;br /&gt;
      if (balance &amp;gt;= amount) { &lt;br /&gt;
         balance -= amount; return amount; &lt;br /&gt;
      } else &lt;br /&gt;
      // Withdrawal not allowed return 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public double getbalance() { &lt;br /&gt;
      return balance; &lt;br /&gt;
   } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class InterestBearingAccount extends Account{&lt;br /&gt;
   private static double default_interest = 7.95;&lt;br /&gt;
   private double interest_rate;&lt;br /&gt;
&lt;br /&gt;
   // Overloaded constructor accepting balance and an interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount, double interest){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor accepting balance with a default interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount ){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor with empty balance and a default interest rate&lt;br /&gt;
   public InterestBearingAccount(){&lt;br /&gt;
       balance = 0.0;&lt;br /&gt;
       interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
	&lt;br /&gt;
   public void add_monthly_interest(){&lt;br /&gt;
      // Add interest to our account&lt;br /&gt;
      balance = balance + 		&lt;br /&gt;
     (balance * interest_rate / 100) / 12;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
Here is another example where inheritance is better than delegation. The generalized shape interface is used as a basis for all of the subclasses.&lt;br /&gt;
Notice that each subclass is designed to personal specifications for the object. Here there would be no need for any of the code to dynamically change. Example below is from link [9].&lt;br /&gt;
&lt;br /&gt;
public interface Shape{   &lt;br /&gt;
    public double getArea();&lt;br /&gt;
    public double getPerimeter();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Rectangle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int width;&lt;br /&gt;
    private int height;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(){&lt;br /&gt;
        this(1,1);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int width, int height){&lt;br /&gt;
        this.width = width;&lt;br /&gt;
        this.height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getWidth(){&lt;br /&gt;
        return this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getHeight(){&lt;br /&gt;
        return this.height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * this.height + 2 * this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return width * height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;width: &amp;quot; + this.width + &amp;quot;, height: &amp;quot; + this.height;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Square extends Rectangle{   &lt;br /&gt;
    public Square(int lengthOfSide){&lt;br /&gt;
        super(lengthOfSide, lengthOfSide);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getSide(){&lt;br /&gt;
        return getWidth();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;Length of Side: &amp;quot; + getSide();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Circle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int radius;&lt;br /&gt;
&lt;br /&gt;
    public Circle(int radius){&lt;br /&gt;
        this.radius = radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return Math.PI * this.radius * this.radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * Math.PI * this.radius;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Delegation is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Delegation is better in this example because class Student doesnt know if it wants to use class Classroom or class Cafeteria's implementation of the f and g methods. During the running of the code class Student can change which class implementation of the functions it points to by calling toClassroom or toCafeteria. Students go between classrooms, libraries, and the cafeteria pretty often throughout a day, delegation allows for an easy change betewen these locations. Inheritance isnt able to handle this dynamically. Any implementation of the code would have to be either in the current class or its parent. It couldnt be in a sibling class. Example below is from link [11]. I have adapted it so that it is more clear to understand. SchoolLocation = I, A = Classroom, B = Cafeteria, C = Student;&lt;br /&gt;
&lt;br /&gt;
interface SchoolLocation {&lt;br /&gt;
     void f();&lt;br /&gt;
     void g();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Classroom implements School {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;A: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;A: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Cafeteria implements School {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;B: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;B: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class Sudent implements School {&lt;br /&gt;
     // delegation&lt;br /&gt;
     SchoolLocation location = new Classroom();&lt;br /&gt;
 &lt;br /&gt;
     public void f() { location.f(); }&lt;br /&gt;
     public void g() { location.g(); }&lt;br /&gt;
 &lt;br /&gt;
     // normal attributes&lt;br /&gt;
     void toClassroom() { location = new Classroom(); }&lt;br /&gt;
     void toCafeteria() { location = new Cafeteria(); }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
Below is the link to a great example of how to use delegation for a real world scenario. It isnt code but is in the form of diagrams. &lt;br /&gt;
&lt;br /&gt;
http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Exampleofdelegation.html&lt;br /&gt;
&lt;br /&gt;
http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Addingcomplexity.html&lt;br /&gt;
&lt;br /&gt;
http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Crossingboundaries.html&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
# http://searchsmb.techtarget.com/sDefinition/0,,sid44_gci212351,00.html&lt;br /&gt;
# http://www.ccs.neu.edu/research/demeter/papers/context-journal/node3.html&lt;br /&gt;
# http://www.tek-tips.com/viewthread.cfm?qid=372254&lt;br /&gt;
# http://www.python.org/ftp/python/doc/delegation.ps&lt;br /&gt;
# http://www.softwarefederation.com/csci4448/courseNotes/05_ObjectOrientedDesign.pdf&lt;br /&gt;
# http://www.javaworld.com/javaworld/javaqa/2001-09/01-qa-0914-delegate.html&lt;br /&gt;
# http://www.javacoffeebreak.com/java102/Account.java&lt;br /&gt;
# http://www.javacoffeebreak.com/java104/java104.html&lt;br /&gt;
# http://www.cs.utexas.edu/~scottm/cs305j/codingSamples.htm&lt;br /&gt;
# http://www.tanguay.info/web/codeExample.php?id=492&lt;br /&gt;
# http://en.wikipedia.org/wiki/Delegation_pattern&lt;br /&gt;
# http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Exampleofdelegation.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=6790</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 10 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=6790"/>
		<updated>2007-10-23T03:30:01Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
&lt;br /&gt;
Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
Inheritance is the process of one class having access to another class's attributes. Inheritance occurs when a class, also called the superclass, is created with generalized methods and attributes, and then another class is created as a subclass to the first class. The subclass provides provides detailed methods and attributes and automatically has access to methods and data members of the superclass. These subclasses can also override the generalized methods of the super class. Sometimes subclasses inherit from more than one super class; the occurrance of this is naturally called multiple inheritance.[1]&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
Delegation is the process of assigning the implementation of a method, within a class, to another method. Generally, the implementation responsibility is forwarded to the parent class. If the parent class can not handle the implementation responsibility, it forwards it to its parent and so on and so on. &lt;br /&gt;
This forwarding allows a class to dynamically change by changing the parent class relations. Delegation is also called dynamic inheritance.[2]&lt;br /&gt;
&lt;br /&gt;
==Inheritance vs Delegation==&lt;br /&gt;
&lt;br /&gt;
Inheritance and delegation are used in different situations in object oriented programming. Although they can be used simultaneously, its rarely done due to the difficulty in it. Most of the time people use them interchangeably but are doing so incorrectly. &lt;br /&gt;
&lt;br /&gt;
===When to use Inheritance===&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used when two objects are of the same type but one of the objects needs to handle somethings differently. The second object would inherit from the first and simply rewrite the code needed in the second object.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used in is-a relationships between objects. [3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used to sub categorize objects.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance should be restricted to objects of the same type.[3]&lt;br /&gt;
&lt;br /&gt;
* When the base class is to general but the functionality is still needed.&lt;br /&gt;
&lt;br /&gt;
=== When to use Delegation===&lt;br /&gt;
&lt;br /&gt;
* Delegation is used when two objects aren't of the same type but one has methods and attributes that the other wants to use internally.[3]&lt;br /&gt;
&lt;br /&gt;
* Delegation is used in has-a relationships where the type of object that is included may change during runtime. &lt;br /&gt;
&lt;br /&gt;
* Delegation is used when methods implementation needs to be handled by some other class within the parent tree. The class that will handle the method isn't known right away. [6]&lt;br /&gt;
&lt;br /&gt;
* When code execution within objects needs to be determined dynamically. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
==Inheritance is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Inheritance is better in this example because any specific account type will require the basic attributes of an account. An interest bearing account also &amp;quot;is a&amp;quot; account. Delegation would not be great in this case because allowing for methods to be determined dynamically opens up opportunities for hacker and calculation issues. Example below is from link [7] and link [8].&lt;br /&gt;
&lt;br /&gt;
public class Account { &lt;br /&gt;
   protected double balance; // Constructor to initialise balance &lt;br /&gt;
      &lt;br /&gt;
   public Account( double amount ) { &lt;br /&gt;
      balance = amount; &lt;br /&gt;
   } &lt;br /&gt;
   // Overloaded constructor for empty balance &lt;br /&gt;
   public Account() { &lt;br /&gt;
      balance = 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public void deposit( double amount ) { &lt;br /&gt;
      balance += amount; &lt;br /&gt;
   } &lt;br /&gt;
   public double withdraw( double amount ) { &lt;br /&gt;
      // See if amount can be withdrawn &lt;br /&gt;
      if (balance &amp;gt;= amount) { &lt;br /&gt;
         balance -= amount; return amount; &lt;br /&gt;
      } else &lt;br /&gt;
      // Withdrawal not allowed return 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public double getbalance() { &lt;br /&gt;
      return balance; &lt;br /&gt;
   } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class InterestBearingAccount extends Account{&lt;br /&gt;
   private static double default_interest = 7.95;&lt;br /&gt;
   private double interest_rate;&lt;br /&gt;
&lt;br /&gt;
   // Overloaded constructor accepting balance and an interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount, double interest){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor accepting balance with a default interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount ){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor with empty balance and a default interest rate&lt;br /&gt;
   public InterestBearingAccount(){&lt;br /&gt;
       balance = 0.0;&lt;br /&gt;
       interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
	&lt;br /&gt;
   public void add_monthly_interest(){&lt;br /&gt;
      // Add interest to our account&lt;br /&gt;
      balance = balance + 		&lt;br /&gt;
     (balance * interest_rate / 100) / 12;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
Here is another example where inheritance is better than delegation. The generalized shape interface is used as a basis for all of the subclasses.&lt;br /&gt;
Notice that each subclass is designed to personal specifications for the object. Here there would be no need for any of the code to dynamically change. Example below is from link [9].&lt;br /&gt;
&lt;br /&gt;
public interface Shape{   &lt;br /&gt;
    public double getArea();&lt;br /&gt;
    public double getPerimeter();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Rectangle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int width;&lt;br /&gt;
    private int height;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(){&lt;br /&gt;
        this(1,1);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int width, int height){&lt;br /&gt;
        this.width = width;&lt;br /&gt;
        this.height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getWidth(){&lt;br /&gt;
        return this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getHeight(){&lt;br /&gt;
        return this.height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * this.height + 2 * this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return width * height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;width: &amp;quot; + this.width + &amp;quot;, height: &amp;quot; + this.height;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Square extends Rectangle{   &lt;br /&gt;
    public Square(int lengthOfSide){&lt;br /&gt;
        super(lengthOfSide, lengthOfSide);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getSide(){&lt;br /&gt;
        return getWidth();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;Length of Side: &amp;quot; + getSide();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Circle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int radius;&lt;br /&gt;
&lt;br /&gt;
    public Circle(int radius){&lt;br /&gt;
        this.radius = radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return Math.PI * this.radius * this.radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * Math.PI * this.radius;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Delegation is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Delegation is better in this example because class c doesnt know if it wants to use class A or class B's implementation of the f and g methods. During the running of the code class c can change which class implementation of the functions it points to by calling toA or toB. Inheritance isnt able to handle this dynamically. Any implementation of the code would have to be either in the current class or its parent. It couldnt be in a sibling class. Example below is from link [11].&lt;br /&gt;
&lt;br /&gt;
interface I {&lt;br /&gt;
     void f();&lt;br /&gt;
     void g();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class A implements I {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;A: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;A: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class B implements I {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;B: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;B: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class C implements I {&lt;br /&gt;
     // delegation&lt;br /&gt;
     I i = new A();&lt;br /&gt;
 &lt;br /&gt;
     public void f() { i.f(); }&lt;br /&gt;
     public void g() { i.g(); }&lt;br /&gt;
 &lt;br /&gt;
     // normal attributes&lt;br /&gt;
     void toA() { i = new A(); }&lt;br /&gt;
     void toB() { i = new B(); }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
Below is the link to a great example of how to use delegation for a real world scenario. It isnt code but is in the form of diagrams. &lt;br /&gt;
&lt;br /&gt;
http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Exampleofdelegation.html&lt;br /&gt;
&lt;br /&gt;
http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Addingcomplexity.html&lt;br /&gt;
&lt;br /&gt;
http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Crossingboundaries.html&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
# http://searchsmb.techtarget.com/sDefinition/0,,sid44_gci212351,00.html&lt;br /&gt;
# http://www.ccs.neu.edu/research/demeter/papers/context-journal/node3.html&lt;br /&gt;
# http://www.tek-tips.com/viewthread.cfm?qid=372254&lt;br /&gt;
# http://www.python.org/ftp/python/doc/delegation.ps&lt;br /&gt;
# http://www.softwarefederation.com/csci4448/courseNotes/05_ObjectOrientedDesign.pdf&lt;br /&gt;
# http://www.javaworld.com/javaworld/javaqa/2001-09/01-qa-0914-delegate.html&lt;br /&gt;
# http://www.javacoffeebreak.com/java102/Account.java&lt;br /&gt;
# http://www.javacoffeebreak.com/java104/java104.html&lt;br /&gt;
# http://www.cs.utexas.edu/~scottm/cs305j/codingSamples.htm&lt;br /&gt;
# http://www.tanguay.info/web/codeExample.php?id=492&lt;br /&gt;
# http://en.wikipedia.org/wiki/Delegation_pattern&lt;br /&gt;
# http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Exampleofdelegation.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=6789</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 10 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=6789"/>
		<updated>2007-10-23T03:29:47Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
&lt;br /&gt;
Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
Inheritance is the process of one class having access to another class's attributes. Inheritance occurs when a class, also called the superclass, is created with generalized methods and attributes, and then another class is created as a subclass to the first class. The subclass provides provides detailed methods and attributes and automatically has access to methods and data members of the superclass. These subclasses can also override the generalized methods of the super class. Sometimes subclasses inherit from more than one super class; the occurrance of this is naturally called multiple inheritance.[1]&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
Delegation is the process of assigning the implementation of a method, within a class, to another method. Generally, the implementation responsibility is forwarded to the parent class. If the parent class can not handle the implementation responsibility, it forwards it to its parent and so on and so on. &lt;br /&gt;
This forwarding allows a class to dynamically change by changing the parent class relations. Delegation is also called dynamic inheritance.[2]&lt;br /&gt;
&lt;br /&gt;
==Inheritance vs Delegation==&lt;br /&gt;
&lt;br /&gt;
Inheritance and delegation are used in different situations in object oriented programming. Although they can be used simultaneously, its rarely done due to the difficulty in it. Most of the time people use them interchangeably but are doing so incorrectly. &lt;br /&gt;
&lt;br /&gt;
===When to use Inheritance===&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used when two objects are of the same type but one of the objects needs to handle somethings differently. The second object would inherit from the first and simply rewrite the code needed in the second object.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used in is-a relationships between objects. [3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used to sub categorize objects.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance should be restricted to objects of the same type.[3]&lt;br /&gt;
&lt;br /&gt;
* When the base class is to general but the functionality is still needed.&lt;br /&gt;
&lt;br /&gt;
=== When to use Delegation===&lt;br /&gt;
&lt;br /&gt;
* Delegation is used when two objects aren't of the same type but one has methods and attributes that the other wants to use internally.[3]&lt;br /&gt;
&lt;br /&gt;
* Delegation is used in has-a relationships where the type of object that is included may change during runtime. &lt;br /&gt;
&lt;br /&gt;
* Delegation is used when methods implementation needs to be handled by some other class within the parent tree. The class that will handle the method isn't known right away. [6]&lt;br /&gt;
&lt;br /&gt;
* When code execution within objects needs to be determined dynamically. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
==Inheritance is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Inheritance is better in this example because any specific account type will require the basic attributes of an account. An interest bearing account also &amp;quot;is a&amp;quot; account. Delegation would not be great in this case because allowing for methods to be determined dynamically opens up opportunities for hacker and calculation issues. Example below is from link [7] and link [8].&lt;br /&gt;
&lt;br /&gt;
public class Account { &lt;br /&gt;
   protected double balance; // Constructor to initialise balance &lt;br /&gt;
      &lt;br /&gt;
   public Account( double amount ) { &lt;br /&gt;
      balance = amount; &lt;br /&gt;
   } &lt;br /&gt;
   // Overloaded constructor for empty balance &lt;br /&gt;
   public Account() { &lt;br /&gt;
      balance = 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public void deposit( double amount ) { &lt;br /&gt;
      balance += amount; &lt;br /&gt;
   } &lt;br /&gt;
   public double withdraw( double amount ) { &lt;br /&gt;
      // See if amount can be withdrawn &lt;br /&gt;
      if (balance &amp;gt;= amount) { &lt;br /&gt;
         balance -= amount; return amount; &lt;br /&gt;
      } else &lt;br /&gt;
      // Withdrawal not allowed return 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public double getbalance() { &lt;br /&gt;
      return balance; &lt;br /&gt;
   } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class InterestBearingAccount extends Account{&lt;br /&gt;
   private static double default_interest = 7.95;&lt;br /&gt;
   private double interest_rate;&lt;br /&gt;
&lt;br /&gt;
   // Overloaded constructor accepting balance and an interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount, double interest){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor accepting balance with a default interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount ){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor with empty balance and a default interest rate&lt;br /&gt;
   public InterestBearingAccount(){&lt;br /&gt;
       balance = 0.0;&lt;br /&gt;
       interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
	&lt;br /&gt;
   public void add_monthly_interest(){&lt;br /&gt;
      // Add interest to our account&lt;br /&gt;
      balance = balance + 		&lt;br /&gt;
     (balance * interest_rate / 100) / 12;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
Here is another example where inheritance is better than delegation. The generalized shape interface is used as a basis for all of the subclasses.&lt;br /&gt;
Notice that each subclass is designed to personal specifications for the object. Here there would be no need for any of the code to dynamically change. Example below is from link [9].&lt;br /&gt;
&lt;br /&gt;
public interface Shape{   &lt;br /&gt;
    public double getArea();&lt;br /&gt;
    public double getPerimeter();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Rectangle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int width;&lt;br /&gt;
    private int height;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(){&lt;br /&gt;
        this(1,1);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int width, int height){&lt;br /&gt;
        this.width = width;&lt;br /&gt;
        this.height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getWidth(){&lt;br /&gt;
        return this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getHeight(){&lt;br /&gt;
        return this.height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * this.height + 2 * this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return width * height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;width: &amp;quot; + this.width + &amp;quot;, height: &amp;quot; + this.height;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Square extends Rectangle{   &lt;br /&gt;
    public Square(int lengthOfSide){&lt;br /&gt;
        super(lengthOfSide, lengthOfSide);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getSide(){&lt;br /&gt;
        return getWidth();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;Length of Side: &amp;quot; + getSide();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Circle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int radius;&lt;br /&gt;
&lt;br /&gt;
    public Circle(int radius){&lt;br /&gt;
        this.radius = radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return Math.PI * this.radius * this.radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * Math.PI * this.radius;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Delegation is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Delegation is better in this example because class c doesnt know if it wants to use class A or class B's implementation of the f and g methods. During the running of the code class c can change which class implementation of the functions it points to by calling toA or toB. Inheritance isnt able to handle this dynamically. Any implementation of the code would have to be either in the current class or its parent. It couldnt be in a sibling class. Example below is from link [11].&lt;br /&gt;
&lt;br /&gt;
interface I {&lt;br /&gt;
     void f();&lt;br /&gt;
     void g();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class A implements I {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;A: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;A: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class B implements I {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;B: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;B: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class C implements I {&lt;br /&gt;
     // delegation&lt;br /&gt;
     I i = new A();&lt;br /&gt;
 &lt;br /&gt;
     public void f() { i.f(); }&lt;br /&gt;
     public void g() { i.g(); }&lt;br /&gt;
 &lt;br /&gt;
     // normal attributes&lt;br /&gt;
     void toA() { i = new A(); }&lt;br /&gt;
     void toB() { i = new B(); }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
Below is the link to a great example of how to use delegation for a real world scenario. It isnt code but is in the form of diagrams. &lt;br /&gt;
&lt;br /&gt;
http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Exampleofdelegation.html&lt;br /&gt;
&lt;br /&gt;
http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Addingcomplexity.html&lt;br /&gt;
http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Crossingboundaries.html&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
# http://searchsmb.techtarget.com/sDefinition/0,,sid44_gci212351,00.html&lt;br /&gt;
# http://www.ccs.neu.edu/research/demeter/papers/context-journal/node3.html&lt;br /&gt;
# http://www.tek-tips.com/viewthread.cfm?qid=372254&lt;br /&gt;
# http://www.python.org/ftp/python/doc/delegation.ps&lt;br /&gt;
# http://www.softwarefederation.com/csci4448/courseNotes/05_ObjectOrientedDesign.pdf&lt;br /&gt;
# http://www.javaworld.com/javaworld/javaqa/2001-09/01-qa-0914-delegate.html&lt;br /&gt;
# http://www.javacoffeebreak.com/java102/Account.java&lt;br /&gt;
# http://www.javacoffeebreak.com/java104/java104.html&lt;br /&gt;
# http://www.cs.utexas.edu/~scottm/cs305j/codingSamples.htm&lt;br /&gt;
# http://www.tanguay.info/web/codeExample.php?id=492&lt;br /&gt;
# http://en.wikipedia.org/wiki/Delegation_pattern&lt;br /&gt;
# http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Exampleofdelegation.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=6788</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 10 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=6788"/>
		<updated>2007-10-23T03:29:29Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
&lt;br /&gt;
Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
Inheritance is the process of one class having access to another class's attributes. Inheritance occurs when a class, also called the superclass, is created with generalized methods and attributes, and then another class is created as a subclass to the first class. The subclass provides provides detailed methods and attributes and automatically has access to methods and data members of the superclass. These subclasses can also override the generalized methods of the super class. Sometimes subclasses inherit from more than one super class; the occurrance of this is naturally called multiple inheritance.[1]&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
Delegation is the process of assigning the implementation of a method, within a class, to another method. Generally, the implementation responsibility is forwarded to the parent class. If the parent class can not handle the implementation responsibility, it forwards it to its parent and so on and so on. &lt;br /&gt;
This forwarding allows a class to dynamically change by changing the parent class relations. Delegation is also called dynamic inheritance.[2]&lt;br /&gt;
&lt;br /&gt;
==Inheritance vs Delegation==&lt;br /&gt;
&lt;br /&gt;
Inheritance and delegation are used in different situations in object oriented programming. Although they can be used simultaneously, its rarely done due to the difficulty in it. Most of the time people use them interchangeably but are doing so incorrectly. &lt;br /&gt;
&lt;br /&gt;
===When to use Inheritance===&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used when two objects are of the same type but one of the objects needs to handle somethings differently. The second object would inherit from the first and simply rewrite the code needed in the second object.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used in is-a relationships between objects. [3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used to sub categorize objects.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance should be restricted to objects of the same type.[3]&lt;br /&gt;
&lt;br /&gt;
* When the base class is to general but the functionality is still needed.&lt;br /&gt;
&lt;br /&gt;
=== When to use Delegation===&lt;br /&gt;
&lt;br /&gt;
* Delegation is used when two objects aren't of the same type but one has methods and attributes that the other wants to use internally.[3]&lt;br /&gt;
&lt;br /&gt;
* Delegation is used in has-a relationships where the type of object that is included may change during runtime. &lt;br /&gt;
&lt;br /&gt;
* Delegation is used when methods implementation needs to be handled by some other class within the parent tree. The class that will handle the method isn't known right away. [6]&lt;br /&gt;
&lt;br /&gt;
* When code execution within objects needs to be determined dynamically. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
==Inheritance is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Inheritance is better in this example because any specific account type will require the basic attributes of an account. An interest bearing account also &amp;quot;is a&amp;quot; account. Delegation would not be great in this case because allowing for methods to be determined dynamically opens up opportunities for hacker and calculation issues. Example below is from link [7] and link [8].&lt;br /&gt;
&lt;br /&gt;
public class Account { &lt;br /&gt;
   protected double balance; // Constructor to initialise balance &lt;br /&gt;
      &lt;br /&gt;
   public Account( double amount ) { &lt;br /&gt;
      balance = amount; &lt;br /&gt;
   } &lt;br /&gt;
   // Overloaded constructor for empty balance &lt;br /&gt;
   public Account() { &lt;br /&gt;
      balance = 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public void deposit( double amount ) { &lt;br /&gt;
      balance += amount; &lt;br /&gt;
   } &lt;br /&gt;
   public double withdraw( double amount ) { &lt;br /&gt;
      // See if amount can be withdrawn &lt;br /&gt;
      if (balance &amp;gt;= amount) { &lt;br /&gt;
         balance -= amount; return amount; &lt;br /&gt;
      } else &lt;br /&gt;
      // Withdrawal not allowed return 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public double getbalance() { &lt;br /&gt;
      return balance; &lt;br /&gt;
   } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class InterestBearingAccount extends Account{&lt;br /&gt;
   private static double default_interest = 7.95;&lt;br /&gt;
   private double interest_rate;&lt;br /&gt;
&lt;br /&gt;
   // Overloaded constructor accepting balance and an interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount, double interest){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor accepting balance with a default interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount ){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor with empty balance and a default interest rate&lt;br /&gt;
   public InterestBearingAccount(){&lt;br /&gt;
       balance = 0.0;&lt;br /&gt;
       interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
	&lt;br /&gt;
   public void add_monthly_interest(){&lt;br /&gt;
      // Add interest to our account&lt;br /&gt;
      balance = balance + 		&lt;br /&gt;
     (balance * interest_rate / 100) / 12;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
Here is another example where inheritance is better than delegation. The generalized shape interface is used as a basis for all of the subclasses.&lt;br /&gt;
Notice that each subclass is designed to personal specifications for the object. Here there would be no need for any of the code to dynamically change. Example below is from link [9].&lt;br /&gt;
&lt;br /&gt;
public interface Shape{   &lt;br /&gt;
    public double getArea();&lt;br /&gt;
    public double getPerimeter();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Rectangle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int width;&lt;br /&gt;
    private int height;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(){&lt;br /&gt;
        this(1,1);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int width, int height){&lt;br /&gt;
        this.width = width;&lt;br /&gt;
        this.height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getWidth(){&lt;br /&gt;
        return this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getHeight(){&lt;br /&gt;
        return this.height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * this.height + 2 * this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return width * height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;width: &amp;quot; + this.width + &amp;quot;, height: &amp;quot; + this.height;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Square extends Rectangle{   &lt;br /&gt;
    public Square(int lengthOfSide){&lt;br /&gt;
        super(lengthOfSide, lengthOfSide);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getSide(){&lt;br /&gt;
        return getWidth();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;Length of Side: &amp;quot; + getSide();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Circle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int radius;&lt;br /&gt;
&lt;br /&gt;
    public Circle(int radius){&lt;br /&gt;
        this.radius = radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return Math.PI * this.radius * this.radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * Math.PI * this.radius;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Delegation is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Delegation is better in this example because class c doesnt know if it wants to use class A or class B's implementation of the f and g methods. During the running of the code class c can change which class implementation of the functions it points to by calling toA or toB. Inheritance isnt able to handle this dynamically. Any implementation of the code would have to be either in the current class or its parent. It couldnt be in a sibling class. Example below is from link [11].&lt;br /&gt;
&lt;br /&gt;
interface I {&lt;br /&gt;
     void f();&lt;br /&gt;
     void g();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class A implements I {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;A: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;A: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class B implements I {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;B: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;B: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class C implements I {&lt;br /&gt;
     // delegation&lt;br /&gt;
     I i = new A();&lt;br /&gt;
 &lt;br /&gt;
     public void f() { i.f(); }&lt;br /&gt;
     public void g() { i.g(); }&lt;br /&gt;
 &lt;br /&gt;
     // normal attributes&lt;br /&gt;
     void toA() { i = new A(); }&lt;br /&gt;
     void toB() { i = new B(); }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
Below is the link to a great example of how to use delegation for a real world scenario. It isnt code but is in the form of diagrams. &lt;br /&gt;
&lt;br /&gt;
http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Exampleofdelegation.html&lt;br /&gt;
http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Addingcomplexity.html&lt;br /&gt;
http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Crossingboundaries.html&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
# http://searchsmb.techtarget.com/sDefinition/0,,sid44_gci212351,00.html&lt;br /&gt;
# http://www.ccs.neu.edu/research/demeter/papers/context-journal/node3.html&lt;br /&gt;
# http://www.tek-tips.com/viewthread.cfm?qid=372254&lt;br /&gt;
# http://www.python.org/ftp/python/doc/delegation.ps&lt;br /&gt;
# http://www.softwarefederation.com/csci4448/courseNotes/05_ObjectOrientedDesign.pdf&lt;br /&gt;
# http://www.javaworld.com/javaworld/javaqa/2001-09/01-qa-0914-delegate.html&lt;br /&gt;
# http://www.javacoffeebreak.com/java102/Account.java&lt;br /&gt;
# http://www.javacoffeebreak.com/java104/java104.html&lt;br /&gt;
# http://www.cs.utexas.edu/~scottm/cs305j/codingSamples.htm&lt;br /&gt;
# http://www.tanguay.info/web/codeExample.php?id=492&lt;br /&gt;
# http://en.wikipedia.org/wiki/Delegation_pattern&lt;br /&gt;
# http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Exampleofdelegation.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=6785</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 10 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=6785"/>
		<updated>2007-10-23T03:26:33Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
&lt;br /&gt;
Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
Inheritance is the process of one class having access to another class's attributes. Inheritance occurs when a class, also called the superclass, is created with generalized methods and attributes, and then another class is created as a subclass to the first class. The subclass provides provides detailed methods and attributes and automatically has access to methods and data members of the superclass. These subclasses can also override the generalized methods of the super class. Sometimes subclasses inherit from more than one super class; the occurrance of this is naturally called multiple inheritance.[1]&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
Delegation is the process of assigning the implementation of a method, within a class, to another method. Generally, the implementation responsibility is forwarded to the parent class. If the parent class can not handle the implementation responsibility, it forwards it to its parent and so on and so on. &lt;br /&gt;
This forwarding allows a class to dynamically change by changing the parent class relations. Delegation is also called dynamic inheritance.[2]&lt;br /&gt;
&lt;br /&gt;
==Inheritance vs Delegation==&lt;br /&gt;
&lt;br /&gt;
Inheritance and delegation are used in different situations in object oriented programming. Although they can be used simultaneously, its rarely done due to the difficulty in it. Most of the time people use them interchangeably but are doing so incorrectly. &lt;br /&gt;
&lt;br /&gt;
===When to use Inheritance===&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used when two objects are of the same type but one of the objects needs to handle somethings differently. The second object would inherit from the first and simply rewrite the code needed in the second object.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used in is-a relationships between objects. [3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used to sub categorize objects.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance should be restricted to objects of the same type.[3]&lt;br /&gt;
&lt;br /&gt;
* When the base class is to general but the functionality is still needed.&lt;br /&gt;
&lt;br /&gt;
=== When to use Delegation===&lt;br /&gt;
&lt;br /&gt;
* Delegation is used when two objects aren't of the same type but one has methods and attributes that the other wants to use internally.[3]&lt;br /&gt;
&lt;br /&gt;
* Delegation is used in has-a relationships where the type of object that is included may change during runtime. &lt;br /&gt;
&lt;br /&gt;
* Delegation is used when methods implementation needs to be handled by some other class within the parent tree. The class that will handle the method isn't known right away. [6]&lt;br /&gt;
&lt;br /&gt;
* When code execution within objects needs to be determined dynamically. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
==Inheritance is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Inheritance is better in this example because any specific account type will require the basic attributes of an account. An interest bearing account also &amp;quot;is a&amp;quot; account. Delegation would not be great in this case because allowing for methods to be determined dynamically opens up opportunities for hacker and calculation issues. Example below is from link [7] and link [8].&lt;br /&gt;
&lt;br /&gt;
public class Account { &lt;br /&gt;
   protected double balance; // Constructor to initialise balance &lt;br /&gt;
      &lt;br /&gt;
   public Account( double amount ) { &lt;br /&gt;
      balance = amount; &lt;br /&gt;
   } &lt;br /&gt;
   // Overloaded constructor for empty balance &lt;br /&gt;
   public Account() { &lt;br /&gt;
      balance = 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public void deposit( double amount ) { &lt;br /&gt;
      balance += amount; &lt;br /&gt;
   } &lt;br /&gt;
   public double withdraw( double amount ) { &lt;br /&gt;
      // See if amount can be withdrawn &lt;br /&gt;
      if (balance &amp;gt;= amount) { &lt;br /&gt;
         balance -= amount; return amount; &lt;br /&gt;
      } else &lt;br /&gt;
      // Withdrawal not allowed return 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public double getbalance() { &lt;br /&gt;
      return balance; &lt;br /&gt;
   } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class InterestBearingAccount extends Account{&lt;br /&gt;
   private static double default_interest = 7.95;&lt;br /&gt;
   private double interest_rate;&lt;br /&gt;
&lt;br /&gt;
   // Overloaded constructor accepting balance and an interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount, double interest){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor accepting balance with a default interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount ){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor with empty balance and a default interest rate&lt;br /&gt;
   public InterestBearingAccount(){&lt;br /&gt;
       balance = 0.0;&lt;br /&gt;
       interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
	&lt;br /&gt;
   public void add_monthly_interest(){&lt;br /&gt;
      // Add interest to our account&lt;br /&gt;
      balance = balance + 		&lt;br /&gt;
     (balance * interest_rate / 100) / 12;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
Here is another example where inheritance is better than delegation. The generalized shape interface is used as a basis for all of the subclasses.&lt;br /&gt;
Notice that each subclass is designed to personal specifications for the object. Here there would be no need for any of the code to dynamically change. Example below is from link [9].&lt;br /&gt;
&lt;br /&gt;
public interface Shape{   &lt;br /&gt;
    public double getArea();&lt;br /&gt;
    public double getPerimeter();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Rectangle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int width;&lt;br /&gt;
    private int height;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(){&lt;br /&gt;
        this(1,1);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int width, int height){&lt;br /&gt;
        this.width = width;&lt;br /&gt;
        this.height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getWidth(){&lt;br /&gt;
        return this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getHeight(){&lt;br /&gt;
        return this.height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * this.height + 2 * this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return width * height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;width: &amp;quot; + this.width + &amp;quot;, height: &amp;quot; + this.height;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Square extends Rectangle{   &lt;br /&gt;
    public Square(int lengthOfSide){&lt;br /&gt;
        super(lengthOfSide, lengthOfSide);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getSide(){&lt;br /&gt;
        return getWidth();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;Length of Side: &amp;quot; + getSide();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Circle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int radius;&lt;br /&gt;
&lt;br /&gt;
    public Circle(int radius){&lt;br /&gt;
        this.radius = radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return Math.PI * this.radius * this.radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * Math.PI * this.radius;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Delegation is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Delegation is better in this example because class c doesnt know if it wants to use class A or class B's implementation of the f and g methods. During the running of the code class c can change which class implementation of the functions it points to by calling toA or toB. Inheritance isnt able to handle this dynamically. Any implementation of the code would have to be either in the current class or its parent. It couldnt be in a sibling class. Example below is from link [11].&lt;br /&gt;
&lt;br /&gt;
interface I {&lt;br /&gt;
     void f();&lt;br /&gt;
     void g();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class A implements I {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;A: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;A: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class B implements I {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;B: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;B: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class C implements I {&lt;br /&gt;
     // delegation&lt;br /&gt;
     I i = new A();&lt;br /&gt;
 &lt;br /&gt;
     public void f() { i.f(); }&lt;br /&gt;
     public void g() { i.g(); }&lt;br /&gt;
 &lt;br /&gt;
     // normal attributes&lt;br /&gt;
     void toA() { i = new A(); }&lt;br /&gt;
     void toB() { i = new B(); }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
# http://searchsmb.techtarget.com/sDefinition/0,,sid44_gci212351,00.html&lt;br /&gt;
# http://www.ccs.neu.edu/research/demeter/papers/context-journal/node3.html&lt;br /&gt;
# http://www.tek-tips.com/viewthread.cfm?qid=372254&lt;br /&gt;
# http://www.python.org/ftp/python/doc/delegation.ps&lt;br /&gt;
# http://www.softwarefederation.com/csci4448/courseNotes/05_ObjectOrientedDesign.pdf&lt;br /&gt;
# http://www.javaworld.com/javaworld/javaqa/2001-09/01-qa-0914-delegate.html&lt;br /&gt;
# http://www.javacoffeebreak.com/java102/Account.java&lt;br /&gt;
# http://www.javacoffeebreak.com/java104/java104.html&lt;br /&gt;
# http://www.cs.utexas.edu/~scottm/cs305j/codingSamples.htm&lt;br /&gt;
# http://www.tanguay.info/web/codeExample.php?id=492&lt;br /&gt;
# http://en.wikipedia.org/wiki/Delegation_pattern&lt;br /&gt;
# http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class7/Exampleofdelegation.html&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=6781</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 10 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=6781"/>
		<updated>2007-10-23T03:19:02Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
&lt;br /&gt;
Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
Inheritance is the process of one class having access to another class's attributes. Inheritance occurs when a class, also called the superclass, is created with generalized methods and attributes, and then another class is created as a subclass to the first class. The subclass provides provides detailed methods and attributes and automatically has access to methods and data members of the superclass. These subclasses can also override the generalized methods of the super class. Sometimes subclasses inherit from more than one super class; the occurrance of this is naturally called multiple inheritance.[1]&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
Delegation is the process of assigning the implementation of a method, within a class, to another method. Generally, the implementation responsibility is forwarded to the parent class. If the parent class can not handle the implementation responsibility, it forwards it to its parent and so on and so on. &lt;br /&gt;
This forwarding allows a class to dynamically change by changing the parent class relations. Delegation is also called dynamic inheritance.[2]&lt;br /&gt;
&lt;br /&gt;
==Inheritance vs Delegation==&lt;br /&gt;
&lt;br /&gt;
Inheritance and delegation are used in different situations in object oriented programming. Although they can be used simultaneously, its rarely done due to the difficulty in it. Most of the time people use them interchangeably but are doing so incorrectly. &lt;br /&gt;
&lt;br /&gt;
===When to use Inheritance===&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used when two objects are of the same type but one of the objects needs to handle somethings differently. The second object would inherit from the first and simply rewrite the code needed in the second object.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used in is-a relationships between objects. [3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used to sub categorize objects.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance should be restricted to objects of the same type.[3]&lt;br /&gt;
&lt;br /&gt;
* When the base class is to general but the functionality is still needed.&lt;br /&gt;
&lt;br /&gt;
=== When to use Delegation===&lt;br /&gt;
&lt;br /&gt;
* Delegation is used when two objects aren't of the same type but one has methods and attributes that the other wants to use internally.[3]&lt;br /&gt;
&lt;br /&gt;
* Delegation is used in has-a relationships where the type of object that is included may change during runtime. &lt;br /&gt;
&lt;br /&gt;
* Delegation is used when methods implementation needs to be handled by some other class within the parent tree. The class that will handle the method isn't known right away. [6]&lt;br /&gt;
&lt;br /&gt;
* When code execution within objects needs to be determined dynamically. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
==Inheritance is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Inheritance is better in this example because any specific account type will require the basic attributes of an account. An interest bearing account also &amp;quot;is a&amp;quot; account. Delegation would not be great in this case because allowing for methods to be determined dynamically opens up opportunities for hacker and calculation issues. Example below is from link [7] and link [8].&lt;br /&gt;
&lt;br /&gt;
public class Account { &lt;br /&gt;
   protected double balance; // Constructor to initialise balance &lt;br /&gt;
      &lt;br /&gt;
   public Account( double amount ) { &lt;br /&gt;
      balance = amount; &lt;br /&gt;
   } &lt;br /&gt;
   // Overloaded constructor for empty balance &lt;br /&gt;
   public Account() { &lt;br /&gt;
      balance = 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public void deposit( double amount ) { &lt;br /&gt;
      balance += amount; &lt;br /&gt;
   } &lt;br /&gt;
   public double withdraw( double amount ) { &lt;br /&gt;
      // See if amount can be withdrawn &lt;br /&gt;
      if (balance &amp;gt;= amount) { &lt;br /&gt;
         balance -= amount; return amount; &lt;br /&gt;
      } else &lt;br /&gt;
      // Withdrawal not allowed return 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public double getbalance() { &lt;br /&gt;
      return balance; &lt;br /&gt;
   } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class InterestBearingAccount extends Account{&lt;br /&gt;
   private static double default_interest = 7.95;&lt;br /&gt;
   private double interest_rate;&lt;br /&gt;
&lt;br /&gt;
   // Overloaded constructor accepting balance and an interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount, double interest){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor accepting balance with a default interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount ){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor with empty balance and a default interest rate&lt;br /&gt;
   public InterestBearingAccount(){&lt;br /&gt;
       balance = 0.0;&lt;br /&gt;
       interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
	&lt;br /&gt;
   public void add_monthly_interest(){&lt;br /&gt;
      // Add interest to our account&lt;br /&gt;
      balance = balance + 		&lt;br /&gt;
     (balance * interest_rate / 100) / 12;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
Here is another example where inheritance is better than delegation. The generalized shape interface is used as a basis for all of the subclasses.&lt;br /&gt;
Notice that each subclass is designed to personal specifications for the object. Here there would be no need for any of the code to dynamically change. Example below is from link [9].&lt;br /&gt;
&lt;br /&gt;
public interface Shape{   &lt;br /&gt;
    public double getArea();&lt;br /&gt;
    public double getPerimeter();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Rectangle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int width;&lt;br /&gt;
    private int height;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(){&lt;br /&gt;
        this(1,1);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int width, int height){&lt;br /&gt;
        this.width = width;&lt;br /&gt;
        this.height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getWidth(){&lt;br /&gt;
        return this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getHeight(){&lt;br /&gt;
        return this.height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * this.height + 2 * this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return width * height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;width: &amp;quot; + this.width + &amp;quot;, height: &amp;quot; + this.height;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Square extends Rectangle{   &lt;br /&gt;
    public Square(int lengthOfSide){&lt;br /&gt;
        super(lengthOfSide, lengthOfSide);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getSide(){&lt;br /&gt;
        return getWidth();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;Length of Side: &amp;quot; + getSide();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Circle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int radius;&lt;br /&gt;
&lt;br /&gt;
    public Circle(int radius){&lt;br /&gt;
        this.radius = radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return Math.PI * this.radius * this.radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * Math.PI * this.radius;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Delegation is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Delegation is better in this example because class c doesnt know if it wants to use class A or class B's implementation of the f and g methods. During the running of the code class c can change which class implementation of the functions it points to by calling toA or toB. Inheritance isnt able to handle this dynamically. Any implementation of the code would have to be either in the current class or its parent. It couldnt be in a sibling class. Example below is from link [11].&lt;br /&gt;
&lt;br /&gt;
interface I {&lt;br /&gt;
     void f();&lt;br /&gt;
     void g();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class A implements I {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;A: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;A: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class B implements I {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;B: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;B: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class C implements I {&lt;br /&gt;
     // delegation&lt;br /&gt;
     I i = new A();&lt;br /&gt;
 &lt;br /&gt;
     public void f() { i.f(); }&lt;br /&gt;
     public void g() { i.g(); }&lt;br /&gt;
 &lt;br /&gt;
     // normal attributes&lt;br /&gt;
     void toA() { i = new A(); }&lt;br /&gt;
     void toB() { i = new B(); }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
# http://searchsmb.techtarget.com/sDefinition/0,,sid44_gci212351,00.html&lt;br /&gt;
# http://www.ccs.neu.edu/research/demeter/papers/context-journal/node3.html&lt;br /&gt;
# http://www.tek-tips.com/viewthread.cfm?qid=372254&lt;br /&gt;
# http://www.python.org/ftp/python/doc/delegation.ps&lt;br /&gt;
# http://www.softwarefederation.com/csci4448/courseNotes/05_ObjectOrientedDesign.pdf&lt;br /&gt;
# http://www.javaworld.com/javaworld/javaqa/2001-09/01-qa-0914-delegate.html&lt;br /&gt;
# http://www.javacoffeebreak.com/java102/Account.java&lt;br /&gt;
# http://www.javacoffeebreak.com/java104/java104.html&lt;br /&gt;
# http://www.cs.utexas.edu/~scottm/cs305j/codingSamples.htm&lt;br /&gt;
# http://www.tanguay.info/web/codeExample.php?id=492&lt;br /&gt;
# http://en.wikipedia.org/wiki/Delegation_pattern&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=6779</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 10 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=6779"/>
		<updated>2007-10-23T03:18:43Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
&lt;br /&gt;
Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
Inheritance is the process of one class having access to another class's attributes. Inheritance occurs when a class, also called the superclass, is created with generalized methods and attributes, and then another class is created as a subclass to the first class. The subclass provides provides detailed methods and attributes and automatically has access to methods and data members of the superclass. These subclasses can also override the generalized methods of the super class. Sometimes subclasses inherit from more than one super class; the occurrance of this is naturally called multiple inheritance.[1]&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
Delegation is the process of assigning the implementation of a method, within a class, to another method. Generally, the implementation responsibility is forwarded to the parent class. If the parent class can not handle the implementation responsibility, it forwards it to its parent and so on and so on. &lt;br /&gt;
This forwarding allows a class to dynamically change by changing the parent class relations. Delegation is also called dynamic inheritance.[2]&lt;br /&gt;
&lt;br /&gt;
==Inheritance vs Delegation==&lt;br /&gt;
&lt;br /&gt;
Inheritance and delegation are used in different situations in object oriented programming. Although they can be used simultaneously, its rarely done due to the difficulty in it. Most of the time people use them interchangeably but are doing so incorrectly. &lt;br /&gt;
&lt;br /&gt;
===When to use Inheritance===&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used when two objects are of the same type but one of the objects needs to handle somethings differently. The second object would inherit from the first and simply rewrite the code needed in the second object.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used in is-a relationships between objects. [3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used to sub categorize objects.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance should be restricted to objects of the same type.[3]&lt;br /&gt;
&lt;br /&gt;
* When the base class is to general but the functionality is still needed.&lt;br /&gt;
&lt;br /&gt;
=== When to use Delegation===&lt;br /&gt;
&lt;br /&gt;
* Delegation is used when two objects aren't of the same type but one has methods and attributes that the other wants to use internally.[3]&lt;br /&gt;
&lt;br /&gt;
* Delegation is used in has-a relationships where the type of object that is included may change during runtime. &lt;br /&gt;
&lt;br /&gt;
* Delegation is used when methods implementation needs to be handled by some other class within the parent tree. The class that will handle the method isn't known right away. [6]&lt;br /&gt;
&lt;br /&gt;
* When code execution within objects needs to be determined dynamically. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
==Inheritance is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Inheritance is better in this example because any specific account type will require the basic attributes of an account. An interest bearing account also &amp;quot;is a&amp;quot; account. Delegation would not be great in this case because allowing for methods to be determined dynamically opens up opportunities for hacker and calculation issues. Example below is from link [7] and link [8].&lt;br /&gt;
&lt;br /&gt;
public class Account { &lt;br /&gt;
   protected double balance; // Constructor to initialise balance &lt;br /&gt;
      &lt;br /&gt;
   public Account( double amount ) { &lt;br /&gt;
      balance = amount; &lt;br /&gt;
   } &lt;br /&gt;
   // Overloaded constructor for empty balance &lt;br /&gt;
   public Account() { &lt;br /&gt;
      balance = 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public void deposit( double amount ) { &lt;br /&gt;
      balance += amount; &lt;br /&gt;
   } &lt;br /&gt;
   public double withdraw( double amount ) { &lt;br /&gt;
      // See if amount can be withdrawn &lt;br /&gt;
      if (balance &amp;gt;= amount) { &lt;br /&gt;
         balance -= amount; return amount; &lt;br /&gt;
      } else &lt;br /&gt;
      // Withdrawal not allowed return 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public double getbalance() { &lt;br /&gt;
      return balance; &lt;br /&gt;
   } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class InterestBearingAccount extends Account{&lt;br /&gt;
   private static double default_interest = 7.95;&lt;br /&gt;
   private double interest_rate;&lt;br /&gt;
&lt;br /&gt;
   // Overloaded constructor accepting balance and an interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount, double interest){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor accepting balance with a default interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount ){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor with empty balance and a default interest rate&lt;br /&gt;
   public InterestBearingAccount(){&lt;br /&gt;
       balance = 0.0;&lt;br /&gt;
       interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
	&lt;br /&gt;
   public void add_monthly_interest(){&lt;br /&gt;
      // Add interest to our account&lt;br /&gt;
      balance = balance + 		&lt;br /&gt;
     (balance * interest_rate / 100) / 12;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
Here is another example where inheritance is better than delegation. The generalized shape interface is used as a basis for all of the subclasses.&lt;br /&gt;
Notice that each subclass is designed to personal specifications for the object. Here there would be no need for any of the code to dynamically change. Example below is from link [9].&lt;br /&gt;
&lt;br /&gt;
public interface Shape{   &lt;br /&gt;
    public double getArea();&lt;br /&gt;
    public double getPerimeter();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Rectangle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int width;&lt;br /&gt;
    private int height;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(){&lt;br /&gt;
        this(1,1);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int width, int height){&lt;br /&gt;
        this.width = width;&lt;br /&gt;
        this.height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getWidth(){&lt;br /&gt;
        return this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getHeight(){&lt;br /&gt;
        return this.height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * this.height + 2 * this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return width * height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;width: &amp;quot; + this.width + &amp;quot;, height: &amp;quot; + this.height;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Square extends Rectangle{   &lt;br /&gt;
    public Square(int lengthOfSide){&lt;br /&gt;
        super(lengthOfSide, lengthOfSide);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getSide(){&lt;br /&gt;
        return getWidth();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;Length of Side: &amp;quot; + getSide();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Circle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int radius;&lt;br /&gt;
&lt;br /&gt;
    public Circle(int radius){&lt;br /&gt;
        this.radius = radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return Math.PI * this.radius * this.radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * Math.PI * this.radius;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Delegation is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Delegation is better in this example because class c doesnt know if it wants to use class A or class B's implementation of the f and g methods. During the running of the code class c can change which class implementation of the functions it points to by calling toA or toB. Inheritance isnt able to handle this dynamically. Any implementation of the code would have to be either in the current class or its parent. It couldnt be in a sibling class. &lt;br /&gt;
&lt;br /&gt;
interface I {&lt;br /&gt;
     void f();&lt;br /&gt;
     void g();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class A implements I {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;A: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;A: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class B implements I {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;B: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;B: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class C implements I {&lt;br /&gt;
     // delegation&lt;br /&gt;
     I i = new A();&lt;br /&gt;
 &lt;br /&gt;
     public void f() { i.f(); }&lt;br /&gt;
     public void g() { i.g(); }&lt;br /&gt;
 &lt;br /&gt;
     // normal attributes&lt;br /&gt;
     void toA() { i = new A(); }&lt;br /&gt;
     void toB() { i = new B(); }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
# http://searchsmb.techtarget.com/sDefinition/0,,sid44_gci212351,00.html&lt;br /&gt;
# http://www.ccs.neu.edu/research/demeter/papers/context-journal/node3.html&lt;br /&gt;
# http://www.tek-tips.com/viewthread.cfm?qid=372254&lt;br /&gt;
# http://www.python.org/ftp/python/doc/delegation.ps&lt;br /&gt;
# http://www.softwarefederation.com/csci4448/courseNotes/05_ObjectOrientedDesign.pdf&lt;br /&gt;
# http://www.javaworld.com/javaworld/javaqa/2001-09/01-qa-0914-delegate.html&lt;br /&gt;
# http://www.javacoffeebreak.com/java102/Account.java&lt;br /&gt;
# http://www.javacoffeebreak.com/java104/java104.html&lt;br /&gt;
# http://www.cs.utexas.edu/~scottm/cs305j/codingSamples.htm&lt;br /&gt;
# http://www.tanguay.info/web/codeExample.php?id=492&lt;br /&gt;
# http://en.wikipedia.org/wiki/Delegation_pattern&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=6778</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 10 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=6778"/>
		<updated>2007-10-23T03:18:17Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
&lt;br /&gt;
Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
Inheritance is the process of one class having access to another class's attributes. Inheritance occurs when a class, also called the superclass, is created with generalized methods and attributes, and then another class is created as a subclass to the first class. The subclass provides provides detailed methods and attributes and automatically has access to methods and data members of the superclass. These subclasses can also override the generalized methods of the super class. Sometimes subclasses inherit from more than one super class; the occurrance of this is naturally called multiple inheritance.[1]&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
Delegation is the process of assigning the implementation of a method, within a class, to another method. Generally, the implementation responsibility is forwarded to the parent class. If the parent class can not handle the implementation responsibility, it forwards it to its parent and so on and so on. &lt;br /&gt;
This forwarding allows a class to dynamically change by changing the parent class relations. Delegation is also called dynamic inheritance.[2]&lt;br /&gt;
&lt;br /&gt;
==Inheritance vs Delegation==&lt;br /&gt;
&lt;br /&gt;
Inheritance and delegation are used in different situations in object oriented programming. Although they can be used simultaneously, its rarely done due to the difficulty in it. Most of the time people use them interchangeably but are doing so incorrectly. &lt;br /&gt;
&lt;br /&gt;
===When to use Inheritance===&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used when two objects are of the same type but one of the objects needs to handle somethings differently. The second object would inherit from the first and simply rewrite the code needed in the second object.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used in is-a relationships between objects. [3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used to sub categorize objects.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance should be restricted to objects of the same type.[3]&lt;br /&gt;
&lt;br /&gt;
* When the base class is to general but the functionality is still needed.&lt;br /&gt;
&lt;br /&gt;
=== When to use Delegation===&lt;br /&gt;
&lt;br /&gt;
* Delegation is used when two objects aren't of the same type but one has methods and attributes that the other wants to use internally.[3]&lt;br /&gt;
&lt;br /&gt;
* Delegation is used in has-a relationships where the type of object that is included may change during runtime. &lt;br /&gt;
&lt;br /&gt;
* Delegation is used when methods implementation needs to be handled by some other class within the parent tree. The class that will handle the method isn't known right away. [6]&lt;br /&gt;
&lt;br /&gt;
* When code execution within objects needs to be determined dynamically. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
==Inheritance is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Inheritance is better in this example because any specific account type will require the basic attributes of an account. An interest bearing account also &amp;quot;is a&amp;quot; account. Delegation would not be great in this case because allowing for methods to be determined dynamically opens up opportunities for hacker and calculation issues. Example below is from link [7] and link [8].&lt;br /&gt;
&lt;br /&gt;
public class Account { &lt;br /&gt;
   protected double balance; // Constructor to initialise balance &lt;br /&gt;
      &lt;br /&gt;
   public Account( double amount ) { &lt;br /&gt;
      balance = amount; &lt;br /&gt;
   } &lt;br /&gt;
   // Overloaded constructor for empty balance &lt;br /&gt;
   public Account() { &lt;br /&gt;
      balance = 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public void deposit( double amount ) { &lt;br /&gt;
      balance += amount; &lt;br /&gt;
   } &lt;br /&gt;
   public double withdraw( double amount ) { &lt;br /&gt;
      // See if amount can be withdrawn &lt;br /&gt;
      if (balance &amp;gt;= amount) { &lt;br /&gt;
         balance -= amount; return amount; &lt;br /&gt;
      } else &lt;br /&gt;
      // Withdrawal not allowed return 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public double getbalance() { &lt;br /&gt;
      return balance; &lt;br /&gt;
   } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class InterestBearingAccount extends Account{&lt;br /&gt;
   private static double default_interest = 7.95;&lt;br /&gt;
   private double interest_rate;&lt;br /&gt;
&lt;br /&gt;
   // Overloaded constructor accepting balance and an interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount, double interest){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor accepting balance with a default interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount ){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor with empty balance and a default interest rate&lt;br /&gt;
   public InterestBearingAccount(){&lt;br /&gt;
       balance = 0.0;&lt;br /&gt;
       interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
	&lt;br /&gt;
   public void add_monthly_interest(){&lt;br /&gt;
      // Add interest to our account&lt;br /&gt;
      balance = balance + 		&lt;br /&gt;
     (balance * interest_rate / 100) / 12;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
Here is another example where inheritance is better than delegation. The generalized shape interface is used as a basis for all of the subclasses.&lt;br /&gt;
Notice that each subclass is designed to personal specifications for the object. Here there would be no need for any of the code to dynamically change. Example below is from link [9].&lt;br /&gt;
&lt;br /&gt;
public interface Shape{   &lt;br /&gt;
    public double getArea();&lt;br /&gt;
    public double getPerimeter();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Rectangle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int width;&lt;br /&gt;
    private int height;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(){&lt;br /&gt;
        this(1,1);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int width, int height){&lt;br /&gt;
        this.width = width;&lt;br /&gt;
        this.height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getWidth(){&lt;br /&gt;
        return this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getHeight(){&lt;br /&gt;
        return this.height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * this.height + 2 * this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return width * height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;width: &amp;quot; + this.width + &amp;quot;, height: &amp;quot; + this.height;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Square extends Rectangle{   &lt;br /&gt;
    public Square(int lengthOfSide){&lt;br /&gt;
        super(lengthOfSide, lengthOfSide);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getSide(){&lt;br /&gt;
        return getWidth();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;Length of Side: &amp;quot; + getSide();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Circle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int radius;&lt;br /&gt;
&lt;br /&gt;
    public Circle(int radius){&lt;br /&gt;
        this.radius = radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return Math.PI * this.radius * this.radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * Math.PI * this.radius;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Delegation is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Delegation is better in this example because class c doesnt know if it wants to use class A or class B's implementation of the f and g methods. During the running of the code class c can change which class implementation of the functions it points to by calling toA or toB. Inheritance isnt able to handle this dynamically. Any implementation of the code would have to be either in the current class or its parent. It couldnt be in a sibling class. &lt;br /&gt;
&lt;br /&gt;
interface I {&lt;br /&gt;
     void f();&lt;br /&gt;
     void g();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class A implements I {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;A: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;A: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class B implements I {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;B: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;B: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class C implements I {&lt;br /&gt;
     // delegation&lt;br /&gt;
     I i = new A();&lt;br /&gt;
 &lt;br /&gt;
     public void f() { i.f(); }&lt;br /&gt;
     public void g() { i.g(); }&lt;br /&gt;
 &lt;br /&gt;
     // normal attributes&lt;br /&gt;
     void toA() { i = new A(); }&lt;br /&gt;
     void toB() { i = new B(); }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
# http://searchsmb.techtarget.com/sDefinition/0,,sid44_gci212351,00.html&lt;br /&gt;
# http://www.ccs.neu.edu/research/demeter/papers/context-journal/node3.html&lt;br /&gt;
# http://www.tek-tips.com/viewthread.cfm?qid=372254&lt;br /&gt;
# http://www.python.org/ftp/python/doc/delegation.ps&lt;br /&gt;
# http://www.softwarefederation.com/csci4448/courseNotes/05_ObjectOrientedDesign.pdf&lt;br /&gt;
# http://www.javaworld.com/javaworld/javaqa/2001-09/01-qa-0914-delegate.html&lt;br /&gt;
# http://www.javacoffeebreak.com/java102/Account.java&lt;br /&gt;
# http://www.javacoffeebreak.com/java104/java104.html&lt;br /&gt;
# http://www.cs.utexas.edu/~scottm/cs305j/codingSamples.htm&lt;br /&gt;
# http://www.tanguay.info/web/codeExample.php?id=492&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=6775</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 10 c4</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_c4&amp;diff=6775"/>
		<updated>2007-10-23T03:12:35Z</updated>

		<summary type="html">&lt;p&gt;Lwillia: /* Example 1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
&lt;br /&gt;
Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
===Inheritance===&lt;br /&gt;
&lt;br /&gt;
Inheritance is the process of one class having access to another class's attributes. Inheritance occurs when a class, also called the superclass, is created with generalized methods and attributes, and then another class is created as a subclass to the first class. The subclass provides provides detailed methods and attributes and automatically has access to methods and data members of the superclass. These subclasses can also override the generalized methods of the super class. Sometimes subclasses inherit from more than one super class; the occurrance of this is naturally called multiple inheritance.[1]&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
&lt;br /&gt;
Delegation is the process of assigning the implementation of a method, within a class, to another method. Generally, the implementation responsibility is forwarded to the parent class. If the parent class can not handle the implementation responsibility, it forwards it to its parent and so on and so on. &lt;br /&gt;
This forwarding allows a class to dynamically change by changing the parent class relations. Delegation is also called dynamic inheritance.[2]&lt;br /&gt;
&lt;br /&gt;
==Inheritance vs Delegation==&lt;br /&gt;
&lt;br /&gt;
Inheritance and delegation are used in different situations in object oriented programming. Although they can be used simultaneously, its rarely done due to the difficulty in it. Most of the time people use them interchangeably but are doing so incorrectly. &lt;br /&gt;
&lt;br /&gt;
===When to use Inheritance===&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used when two objects are of the same type but one of the objects needs to handle somethings differently. The second object would inherit from the first and simply rewrite the code needed in the second object.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used in is-a relationships between objects. [3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance is used to sub categorize objects.[3]&lt;br /&gt;
&lt;br /&gt;
* Inheritance should be restricted to objects of the same type.[3]&lt;br /&gt;
&lt;br /&gt;
* When the base class is to general but the functionality is still needed.&lt;br /&gt;
&lt;br /&gt;
=== When to use Delegation===&lt;br /&gt;
&lt;br /&gt;
* Delegation is used when two objects aren't of the same type but one has methods and attributes that the other wants to use internally.[3]&lt;br /&gt;
&lt;br /&gt;
* Delegation is used in has-a relationships where the type of object that is included may change during runtime. &lt;br /&gt;
&lt;br /&gt;
* Delegation is used when methods implementation needs to be handled by some other class within the parent tree. The class that will handle the method isn't known right away. [6]&lt;br /&gt;
&lt;br /&gt;
* When code execution within objects needs to be determined dynamically. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.&lt;br /&gt;
&lt;br /&gt;
==Inheritance is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
Inheritance is better in this example because any specific account type will require the basic attributes of an account. An interest bearing account also &amp;quot;is a&amp;quot; account. Delegation would not be great in this case because allowing for methods to be determined dynamically opens up opportunities for hacker and calculation issues. Example below is from link [7] and link [8].&lt;br /&gt;
&lt;br /&gt;
public class Account { &lt;br /&gt;
   protected double balance; // Constructor to initialise balance &lt;br /&gt;
      &lt;br /&gt;
   public Account( double amount ) { &lt;br /&gt;
      balance = amount; &lt;br /&gt;
   } &lt;br /&gt;
   // Overloaded constructor for empty balance &lt;br /&gt;
   public Account() { &lt;br /&gt;
      balance = 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public void deposit( double amount ) { &lt;br /&gt;
      balance += amount; &lt;br /&gt;
   } &lt;br /&gt;
   public double withdraw( double amount ) { &lt;br /&gt;
      // See if amount can be withdrawn &lt;br /&gt;
      if (balance &amp;gt;= amount) { &lt;br /&gt;
         balance -= amount; return amount; &lt;br /&gt;
      } else &lt;br /&gt;
      // Withdrawal not allowed return 0.0; &lt;br /&gt;
   } &lt;br /&gt;
   public double getbalance() { &lt;br /&gt;
      return balance; &lt;br /&gt;
   } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class InterestBearingAccount extends Account{&lt;br /&gt;
   private static double default_interest = 7.95;&lt;br /&gt;
   private double interest_rate;&lt;br /&gt;
&lt;br /&gt;
   // Overloaded constructor accepting balance and an interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount, double interest){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor accepting balance with a default interest rate&lt;br /&gt;
   public InterestBearingAccount( double amount ){&lt;br /&gt;
      balance = amount;&lt;br /&gt;
      interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
   // Overloaded constructor with empty balance and a default interest rate&lt;br /&gt;
   public InterestBearingAccount(){&lt;br /&gt;
       balance = 0.0;&lt;br /&gt;
       interest_rate = default_interest;&lt;br /&gt;
   }&lt;br /&gt;
	&lt;br /&gt;
   public void add_monthly_interest(){&lt;br /&gt;
      // Add interest to our account&lt;br /&gt;
      balance = balance + 		&lt;br /&gt;
     (balance * interest_rate / 100) / 12;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
Here is another example where inheritance is better than delegation. The generalized shape interface is used as a basis for all of the subclasses.&lt;br /&gt;
Notice that each subclass is designed to personal specifications for the object. Here there would be no need for any of the code to dynamically change. Example below is from link [9].&lt;br /&gt;
&lt;br /&gt;
public interface Shape{   &lt;br /&gt;
    public double getArea();&lt;br /&gt;
    public double getPerimeter();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Rectangle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int width;&lt;br /&gt;
    private int height;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(){&lt;br /&gt;
        this(1,1);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int width, int height){&lt;br /&gt;
        this.width = width;&lt;br /&gt;
        this.height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getWidth(){&lt;br /&gt;
        return this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getHeight(){&lt;br /&gt;
        return this.height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * this.height + 2 * this.width;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return width * height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;width: &amp;quot; + this.width + &amp;quot;, height: &amp;quot; + this.height;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Square extends Rectangle{   &lt;br /&gt;
    public Square(int lengthOfSide){&lt;br /&gt;
        super(lengthOfSide, lengthOfSide);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getSide(){&lt;br /&gt;
        return getWidth();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String toString(){&lt;br /&gt;
        return &amp;quot;Length of Side: &amp;quot; + getSide();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Circle implements Shape&lt;br /&gt;
{   &lt;br /&gt;
    private int radius;&lt;br /&gt;
&lt;br /&gt;
    public Circle(int radius){&lt;br /&gt;
        this.radius = radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getArea(){&lt;br /&gt;
        return Math.PI * this.radius * this.radius;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public double getPerimeter(){&lt;br /&gt;
        return 2 * Math.PI * this.radius;&lt;br /&gt;
    }    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
==Delegation is Better==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
interface I {&lt;br /&gt;
     void f();&lt;br /&gt;
     void g();&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class A implements I {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;A: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;A: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class B implements I {&lt;br /&gt;
     public void f() { System.out.println(&amp;quot;B: doing f()&amp;quot;); }&lt;br /&gt;
     public void g() { System.out.println(&amp;quot;B: doing g()&amp;quot;); }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 class C implements I {&lt;br /&gt;
     // delegation&lt;br /&gt;
     I i = new A();&lt;br /&gt;
 &lt;br /&gt;
     public void f() { i.f(); }&lt;br /&gt;
     public void g() { i.g(); }&lt;br /&gt;
 &lt;br /&gt;
     // normal attributes&lt;br /&gt;
     void toA() { i = new A(); }&lt;br /&gt;
     void toB() { i = new B(); }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
# http://searchsmb.techtarget.com/sDefinition/0,,sid44_gci212351,00.html&lt;br /&gt;
# http://www.ccs.neu.edu/research/demeter/papers/context-journal/node3.html&lt;br /&gt;
# http://www.tek-tips.com/viewthread.cfm?qid=372254&lt;br /&gt;
# http://www.python.org/ftp/python/doc/delegation.ps&lt;br /&gt;
# http://www.softwarefederation.com/csci4448/courseNotes/05_ObjectOrientedDesign.pdf&lt;br /&gt;
# http://www.javaworld.com/javaworld/javaqa/2001-09/01-qa-0914-delegate.html&lt;br /&gt;
# http://www.javacoffeebreak.com/java102/Account.java&lt;br /&gt;
# http://www.javacoffeebreak.com/java104/java104.html&lt;br /&gt;
# http://www.cs.utexas.edu/~scottm/cs305j/codingSamples.htm&lt;br /&gt;
# http://www.tanguay.info/web/codeExample.php?id=492&lt;/div&gt;</summary>
		<author><name>Lwillia</name></author>
	</entry>
</feed>