Question: NEED HELP ASAP! MATCHING QUESTION!! Use a choice atmost once. class Node { int item; Node next; Node(int item, Node next){ this.item = item; this.next
NEED HELP ASAP! MATCHING QUESTION!!


Use a choice atmost once. class Node { int item; Node next; Node(int item, Node next){ this.item = item; this.next = next; } } public Node reverse (Node x) { return reverse Aux(x, null); } public Node reverse Aux (Node x, Node alreadyReversed) { while (x != null) { //INVARIANT Node tmp = x.next; x.next = alreadyReversed; alreadyReversed = x; x = tmp; } return alreadyReversed; } int[] toArray(Node n,int size) { int[] a = new int[size]; int i=0; for (Node m=n; m!=null; m = m.next){ // INVARIANT a[i] = m.item; i++; } return a; } Node to Node(int[] a){ Node next = null; for (int k = a.length -1; k >=0; k--){ // INVARIANT next = new Node(a[k], next); return next; } 1. assert(x!=null) 2 assert(true) 3 (a!=null) && (a.length > 0) 4. assert(a!=null) 5. int[] a = {1,2,3,4}; Node n = toNode(a, 4); Node nr = reverse(n); Node nrr = reverse(nr); assert(Arrays.equals(a,toArray(nrr)); 6. int[] a = {1,2,3,4}; Node n = toNode(a, 4); Node nr = reverse(n); Node nrr = reverse(nr); assert(nrr.length == 4); 7. assert(reverse(null) == null); 8. Best Unit test for reverse: for non-null lists try reverse(null); assert(false); (catch Exception) precondition of toNode 9. try { reverse(null); invariant of toArray (catch Exception) { assert(false); } OD 10 DOO Unit test for reverse: to check it does not throw an exception for null lists 10. reverse of the list from x followed by the list from alreadyReversed Invariant of toNode 11. list from x followed by the list from alreadyReversed Precondition of reverse Aux 12. list from x followed by the reverse of the list from alreadyReversed Invariant for reverse Aux 13. array a from 0 to 1-1 contains the items of the list from n (inclusive) to m(exclusive) 14. array a from 0 to i contains the items of the list from n (inclusive) to m(exclusive) 15. next is the list of the elements in the array from k to a.length-1, both inclusive 16. next is the list of the elements in the array from k to a.length, both inclusive 17. next is the list of the elements in the array from k+1 to a.length-1, both inclusive 18. Node n = reverse(null); if (n==null) System.out.println("true"); else System.out.println("false")
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
