In: Computer Science
use a match almost once.
consider rhe following instance variable and method.
private int[]arr;
puplic int i(intx){
\\PRECOND
for (intk =arr.length-1;k>=0;k--){
\\LOOPINVAR
is(arr[k]<x)returnk;
}
\\POSTCOND
RETURN-1;
}
Let:int m=f(n)
1.assert(a!=null);
2.assert(a.length>0);
3.assert(true)
4.assert(false);
5. All values in positions 0 through m are less
than n.
6. All values in positions m+1 through
arr.lenght-1 are greater than or equal to n.
7.All the values in positions m+1 through
arr.lenght-1 are less then n.
8.The smallest value is at position m.
9.The largest value that is smaller that n is at
position m.
10.All values in positions k+1 through
arr.length-1 are greater than or equal to n.
11.All values in positions k though arr.length-1
are less than n.
12.all values in positions 0 through k are less
than n
13.int[] arr={3,9,3,5,8}
assertTrue(f(4)==2);
14.int[] arr={1,9,3,5,8}
assertTrue(f(4)==0);
The best Preconditions is ____
junit test for thr method that fails____
Best postondition____
junit test for the method that passes_____
Loop invariant_____
The best Preconditions is ____
Answer:
1.assert(a!=null);
Explanation: Array must be checked if it is null or not. Otherwise
NullPointerException will be thrown on next line.
junit test for thr method that fails____
Answer:
14.int[] arr={1,9,3,5,8}
assertTrue(f(4)==0);
Explanation: f(4) will return 2 as loop starts from last element
and breaks when comparing number 3 which is at position 2 in
array.
So this test case will fail. As here f(4) will return 2. And
assertion is made on comparison with 0.
Best postondition____
Answer:
10.All values in positions k+1 through arr.length-1 are greater
than or equal to n.
Description: It will come to the this point when all the values are
greater than or equal to n.
junit test for the method that passes_____
Answer:
13.int[] arr={3,9,3,5,8}
assertTrue(f(4)==2);
Explanation: f(4) will return 2 as loop starts from last element
and breaks when comparing number 3 which is at position 2 in
array.
So this test case will pass. As here f(4) will return 2. And
assertion is made on comparison with 2.
Loop invariant_____
Answer:
6. All values in positions m+1 through arr.lenght-1 are greater
than or equal to n.
Explanation: Since m is the index of array at which point the value
is less than n,
so all values from m+1 till arr.length-1 are greater than or equal
to n.