CS307 Midterm 2 Spring 2007 Suggested Solutions and Criteria 1. Answer as written or -1. Ignore differences in capitilization. On Big O, it is okay if missing O(). A. 6 B. maepnnpeam C. 14 D. O(N^3) E. O(N) F. O(N^3) G. O(N + M) H. O(1) I. O(N) J. O(N) K. 8 seconds L. 21.9 seconds (or 4 * 23 * 5 / 21 or 460 / 21 ) M. 16 seconds N. ABDF O. CBBDB 2. This was suppose to be an easy question, but students had lots of problems. Very few full credit answers. Lot of confusion between a Node variable and a Node object. Not many people drew pictures to help. (Maybe they were on the scratch paper.) // pre: none // post: add obj to the end of this list public void addLast(Object obj){ if( myHead == null) myHead = new Node(obj, null); else { Node temp = myHead; // find last node in list while( temp.getNext() != null ) temp = temp.getNext(); // create and hook up last node temp.setNext( new Node(obj, null) ); } } 3 hanlde empty case 2 use temp Node variable Move temp to end of list 3 attempt 4 correct 3 create new Node and attach correctly 3. Maybe the hardest question on the test. Lots to keep track of, but essentially an array question. public ArrayBasedList removeRange(int start, int stop){ assert start >= 0 && start < size() && stop > start && stop <= size(); //create result and make its container big enough ArrayBaseList result = new ArrayBasedList(); result.myCon = new Object[stop - start]; result.mySize = stop - start; //copy removed elements to result for(int i = 0; i < result.mySize; i++) result.myCon[i] = myCon[i + start]; //shift elements in this int numShift = mySize - stop; for(int i = 0; i < numShift; i++) myCon[start + i] = myCon[stop + i]; // null out new empty elements for(int i = start + numShift; i < mySize; i++) myCon[i] = null; mySize -= result.mySize; return result; } Grading criteria 1 create resulting ArrayBasedList 1 correctly set size to resulting ArrayBasedList copy removed elements into resulting ArrayBasedList 3 attempt 4 correct shift elements down in this ArrayBasedList 3 attempt 5 correct 1 null out empty spots in this list 1 update this lists size 1 return result use of metyhods that aren't available loses correctness points 4. A relatively easy question if you used the methods in the ArrayList class. // pre: none // post: return the cardinality of this MultiSet. This // MultiSet is not changed as a result of this method call. public int cardinality(){ ArrayList