60 likes | 215 Views
Specifications in JML. Example: Linear Search: Environment: var N : int; b : array [0.. N ) of int; i, x : int; Frame: i : [ N >0 x b [0.. N ), 0 i < N ( j | 0 j < i : x b [ j ]) x = b [ i ]]. In JML public static int[] b= {2,3,1,2,4, 3,4};
E N D
Specifications in JML • Example: Linear Search: • Environment: varN: int; b:array [0..N) of int; i, x: int; • Frame:i: [N>0 xb[0..N), 0 i <N (j| 0 j < i: xb[j]) x=b[i]] • In JML • public static int[] b= {2,3,1,2,4, 3,4}; • /*@ requires (\exists int j; 0<=j && j<b.length;x==b[j]); • @ ensures b[\result]==x; • @ ensures (\forall int j;0<=j && j<\result; b[j]!=x); • @*/ • public static int search1(int x){---} UCN T&B: JML-1
Specifications in JML public static int search1(int x){ int i= 0; boolean found= false; while(i<b.length && !found){ if(x==b[i]) found= true; else i++; } return i; } public static void main(String[] args){ System.out.println(search1(3)); } public static int[] b= {2,3,1,2,4, 3,4}; UCN T&B: JML-1
Specifications in JML public static int search1(int x){ int i= 0; boolean found= false; while(i<b.length && !found){ if(x==b[i]) found= true; else i++; } return i; } public static void main(String[] args){ System.out.println(search1(13)); } Precondition! UCN T&B: JML-1
Specifications in JML public static int search1(int x){ int i= 0; boolean found= false; while(i<b.length && !found){ if(x==b[i]) found= true; else i++; } return i+1; } public static void main(String[] args){ System.out.println(search1(3)); } Postcondition! UCN T&B: JML-1
Exercise • Implement and test a few of the examples or exercises in Java and JML. UCN T&B: JML-1