// Simple pre/post contract example in Java // CS/SWE 332 // Fall 2011 //MinArray.java public class MinArray { // pre: a is not null and a is not empty // post: return minimum value in a[] public static int min( int[] a ) { int min = a[0]; for (int i=1; i < a.length; i++ ) { if (min > a[i]) { min = a[i]; } } return min; } public static void main( String[] args ) { int[] data = { 0, 1, 2, 3, 4, 5, 9, 8, 7, 6 }; System.out.println( "The min is: " + min(data) ); int[] data1 = { 13 }; System.out.println( "The min is: " + min(data1) ); // Note that the following call violates the precondition // and results in an (inappropriate) exception. int[] data2 = {}; System.out.println( "The min is: " + min(data2) ); } }