My latest assignment requires me to have this follow criteria
"All methods have explicit postcondition and those with parameters preconditions "
I have read through a few web pages trying to explain pre/post conditions, but can seem to get a grasp on them, could someone explain to me what they are, their use and how to write them?
Thanks
(the language i am learning is C# by the way)
Preconditions must be true before you enter the method, otherwise the contract is nullified. Postcoditions should be true after you exit the method. I'm sorry I don't know C# but if you know Java this selection sort example might help. Example:
public static void selSort(int[] a, int b) {
//Pre-condition: array a is not null and size of unsorted section is bigger than 1.
for(int unsortSz = b; unsortSz >1; unsortSz--) {
int max = 0;
for (int p = 1; p < unsortSz; p++){
if (a[p] > a[max]){
max = p;
}
}
//Post-condition: max is the position of largest element in unsorted part.
// now just swap the last element in unsorted part with max
temp = a[unsortSz-1];
a[unsortSz] = a[max];
a[max] = temp;
}
}