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 uniques = new ArrayList(); for(Object obj : myCon) if(! uniques.contains( obj ) ) uniques.add( obj ); return uniques.size(); } Loop through myCon 3 attempt 3 correct determine if a given element is unique 3 attempt 3 correct 2 return result 6 shows understanding of difference between array, ArrayList, and MultiSet. Object oriented concepts. 5. The hardest part may have been doing the geometry / algebra to figure out the coordinates of the smaller squares. Saw lots of incorrect attempts with filling 4 squares and 1 call to drawSquare. Many students hard coded in 600 and 1 instead of using the parameters size and limit. Many students forgot to pass the Graphics object. In draw drawSquares(g, size, limit, 0, 0); public static void drawSquares(Graphics g, int size, int limit, int x, int y){ if( size > limit ){ size = size / 3; g.fillRect(x + size, y + size, size, size); drawSquares(g, size, limit, x + size, y); drawSquares(g, size, limit, x, y + size); drawSquares(g, size, limit, x + 2 * size, y + size); drawSquares(g, size, limit, x + size, y + 2 * size); } } 1 correct parameters 3 base case correct 2 correctly fill middle square 3 four recursive calss 3 correct coordinates on recursive calls 2 size changed to size / 3 1 show first call