Logout

4.1.11

Explain the need for pre-conditions when executing an algorithm.

 

Teaching Note:

 

Sample Question:

sdfsdfsf

JSR Notes:

Firstly note that this assessment statement is one that says "explain the need", rather than give specific examples - that is the next assessment statement. Another interesting thing to note is it doesn't ask the need for post-conditions. And frankly, there's often not such a need. Post conditions are simply a way to genarally communicate what a method will do, and often that's obvious just by the name of it.

Meantime,there is indeed a need for pre-conditions. Pre-conditions are conditions which must be met in order for the method to function properly. So, in terms of Java, they are the number, the type, and the range of the parameters taken in, along with any other things which would have had to be done first, like initialzing an array of template objects.

 

So, for example, a binarySearch() method requires two parameters, the item to be searched, and the array to search it from. But another pre-condition is that the array being sent is already sorted.

Or a mathematical method may be taking in a parameter which will be used as a divisor. A pre-condition for that parameter is that it is not 0.

Or a method may be intended to work with a set of data that first needs to be loaded from a file. And so the pre-condition is that indeed that file has been loaded into an internal array.

And so on. You can think of some of your own pre-conditions for methods in your IA project.

 

But getting back to "the need", well, there is a need to communicate all of the above examples, otherwise the programs in which those methods occurred would either not work as intended, or even freeze. That's why there's the need. Consistent use of them results in more reliable programs.