Contract Class


The contract.java file is one that the user will not see, specify, or use. It will, however, allow the user to furthur test the ADT methods - specifically, the pre- and post-conditions. The contract class contains two methods that may be called from the user's Java class. Those methods are ensure() and require().

To use these methods, the user must import the contract class by adding the following line to the beginning of the Java class, before any code (not including comments):

import "contract";

Then, inside the definition of the ADT class (not in a method) must go the following line:

contract con = new contract();

Note that the contract doesn't have to be named "con", but it is here for demonstration purposes. Name it whatever you want.

Now, at the beginning of a method, the pre-condition can be tested by adding the line

con.require (req, err_message);

where req is a requirement, expressed as a boolean, that returns 'true' if the pre-conditions are satisfied, and err_message is a string that is printed if it's not true. Similarly, the line

con.ensure (req, err_message);

tests the post-condition.

Note that these changes to your code will only work when you are running Danish. When you are done, and the ADT is running perfectly, take these lines out (or comment them out) so the ADT will work on its own.

Note also that the use of the contract class is optional, and not necessary to run Danish. If it's giving you trouble, it's OK not to use it. You have my permission.


Goto Previous Topic Goto Next Topic
Created 04/27/98 by preston