Professional Documents
Culture Documents
AFM Exercises Sheet 9
AFM Exercises Sheet 9
Question 1:
a) Provide a JML postcondition for the removeDuplicates method stating that the
result array is at most as long as the input array a.
b) Provide a JML postcondition for the removeDuplicates method that states that
no value is duplicated in the result array.
c) Provide a JML postcondition for the removeDuplicates method, stating that any
value that appears in array a also appears in the result array.
Question 2:
State the meaning of the following JML method contract in English language.
public class A {
/*@ public normal_behavior
@ requires 0 < q && q <= p;
@ assignable \nothing;
@ ensures \result.length == q;
@ ensures (\forall int i; 0 <= i && i < q; 0 <= \result[i]);
@ ensures (\sum int i; 0 <= i && i < q; \result[i]) <= p;
@ ensures (\forall int i; 0 <= i && i < q;
@ (\sum int j; 0 <= j && j < q; \result[j]) <= \result[i] * q);
@*/
public int[] m(int p, int q) { ... }
}
1
Question 3:
A queue is an ordered group of elements. Queues have two ends. Elements are added at one
end and are removed from the other end. The element added first is also removed first (FIFO
principle). Enqueue is a function that adds a new element to the rear of the queue. Dequeue
is a function to remove the front element from the queue and the function returns the front
element.
Consider the following code fragment of a Queue:
public class Queue {
Object[] arr;
int size;
int first;
int next;
We consider an array a with n elements of type integer. Let j and k be two indices with
0 <= j < k < n.
In below subquestions 'a[j..k]' we mean a segment of the array a (but we cannot just
write 'a[j..k]' in JML).
Write JML expressions for the following: