CMPSCI 187 Fall 2011: Solutions to Midterm #1 David Mix Barrington, 8 October 2012 1: a: A "String [ ]" is a one-dimensional array of Strings. A "String [ ] [ ]" is a two-dimensional array of Strings. b: The first constructor sets up an array of the default size, while the second sets up an array of length k. These sizes are the maximum number of strings the StringLog can hold without becoming full. c: Software is the totality of code, protocols, and documentation that solve an information processing task. Code is just the code -- instructions to a machine in a computer language. d: O(1) running time is bounded by a constant independent of the input size -- no matter how long the input is, the time will be bounded by some fixed limit. O(n) running time grows at most proportionally to the input size -- there is some number c such that the running time is never more than cn on input of length n. e: The StringLog ADT is a collection of Strings that supports insertion (at the end), reporting size, and testing whether a particular String is a member. The StringBag ADT is a collection of Strings that supports insertion at the end and removal of a random element. The two classes mentioned are each array implementations of the their ADT's. f: Postfix expressions like "3 2 *" have the operator after the operands on which it operates. Infix expressions like "3 * 2" have the operator between the operands. g: Guarding an exception is preventing it from occurring by testing for the condition that causes it before running the code that would cause it. Catching the exception is allowing it to occur within a try block, where their is a matching catch block afterward with code to be run after the exception occurs. h: In the java.util.Stack class a pop operation removes _and returns_ the top element of the stack. In the DJW stack classes, the pop operation removes the top element without returning it. i: An object is an entity containing data in its fields and able to run the methods defined in its class. A primitive is a member of one of the eight basic data types of Java (six for numbers, char, and boolean). Primitives are passed by value, and objects by reference. j: An observer is an instance method that returns information about the object without changing its state. A transformer is an instance method that changes the state of the object. 2: a: The replace method would be much like the zero-parameter remove method, in that it would use a pseudorandom number to choose a random element of the array (in the range of indices actually used for elements) and remove it. But it would replace the new element with its parameter, rather than with the last element in the used range. b: It would be easy to modify the backtrack search of SudokuSolver so that instead of stopping when it finds a solution, it would go on to the next value for the last cell and continue, until it either finds a second solution (and returns false) or empties its stack (and returns true). Of course if it empties the stack before finding a solution it should return false. 3: a: We first push four dogs onto the right stack. We then shift dogs from the right to the left stack as long as the dogs are older than Sydney. Ace and Cardie are shifted, then Biscuit is not. We push Sydney onto the right stack (which now has, from the top, Sydney, Biscuit, and Duncan) and print the name of the top dog on the left stack, which is "Cardie". b: We make an array of nine ints and fill them with the numbers 1 through 9. We then go through all pairs of indices in this array, looking for any case in which two indices have the same value. Since these loops include the case of testing an index against itself, we find lots of matches and the badly-named variable "duplicate" is set to true. Thus we print "Duplicate element exists" although this is not true in the normal meaning of the word. c: Five dog variables are defined, but only three dogs. To keep it straight, we'll refer to the dogs by their ages. After the first five lines, "ace" and "pointer" point to the age-6 dog, "biscuit" points to the age-3 dog, and "cardie" and "golden" both point to the age-5 dog. We then rename the age-6 dog "Cardie" in the sixth line. The seventh line changes nothing because it sets the age-5 dog's name to "Cardie", which it already was. The eighth line sets the age-3 dog's name to "Cardie" as well. Then the ninth line renames the age-6 dog as "Biscuit". The variable "ace" still points to the age-6 dog, so we print out "Biscuit". The variable "cardie" still points to the age-5 dog, so we print out "Cardie". 4: a: The last line causes a compiler error because you cannot create an array of a generic type. You have to create an array of Objects and cast it into the generic type. b: We put three dogs on the stack and start popping them off until there are none left. But after the third dog is popped, we call the method "top" on an empty stack, throwing a StackUnderflowException. The programmer presumably meant to say "while (!isEmpty( ))…" c: We create an array with room for ten Integers, and create objects for the first five locations by autoboxing in the first loop. The second loop goes fine for the first five locations because the Integer is unboxed, incremented, and boxed. But the attempt to increment ia[5] throws a NullPointerException because this location is still null. 5: a: This takes O(N) time in the while loop because we go through the loop once for each element of the list, and the inside of the loop does the same set of operations no matter how long the list is. The operations outside the loop also take O(1) time, and O(N) + O(1) = O(N). b: Because 9 is a constant, _any_ operation on a 9 by 9 board, even something as complicated as solving the sudoku puzzle, takes constant time (maybe a large constant) as it does not depend on the number N. But applying the Move objects to the Board takes O(1) time for each object, or O(N) time total. (Note that if there are more than 81 Moves, some of them are going to reset numbers that have been set by previous Moves. The total time is thus O(N) + O(1) = O(N). 6: public class Kennel { Dog [ ] dogs; int size; public Kennel (int k) { dogs = new Dog[k]; size = 0;} public boolean free (int pos) { return (dogs[pos] == null);} public void insert (Dog d, int pos) { if (free(pos)) { dogs[pos] = d; size++;} public Dog remove (int pos) { Dog out = null; if (!free(pos)) { out = dogs[pos]; dogs[pos] = null; size--;} return out;} public void consolidate( ) { int scan = 0; for (int i = 0; i < size; i++) { while (free(scan)) scan++; dogs[i] = dogs[scan]; // copying higher to lower index dogs[scan] = null;}}} // position will be null and be skipped 7: public class AADL { LLNode top; public AADL ( ) { top = null;} public void add (Dog d) { if (isEmpty( ) || d.getAge( ) <= top.getAge( )) { LLNode newNode = new LLNode (d); newNode.setLink(top); top = newNode;}} public void canAdd (Dog d) { return (isEmpty( ) || d.getAge( ) <= top.getAge( ));} public Dog remove( ) { // throws NullPointerException if list is empty Dog out = top.getInfo( ); top = top.getLink( ); return out;} public boolean isEmpty( ) { return (top == null);} public void insert (Dog d) { DADL storage = new DADL (dogs.length); while (!canAdd(d)) storage.add(remove( )); add(d); while (!storage.isEmpty( )) add(storage.remove( ));}} public class DADL { LLNode top; public DADL ( ) { top = null;} public void add (Dog d) { if (isEmpty( ) || d.getAge( ) >= top.getAge( )) { LLNode newNode = new LLNode (d); newNode.setLink(top); top = newNode;}} public void canAdd (Dog d) { return (isEmpty( ) || d.getAge( ) >= top.getAge( ));} public Dog remove( ) { // throws NullPointerException if list is empty Dog out = top.getInfo( ); top = top.getLink( ); return out;} public boolean isEmpty( ) { return (top == null);} public void insert (Dog d) { AADL storage = new AADL (dogs.length); while (!canAdd(d)) storage.add(remove( )); add(d); while (!storage.isEmpty( )) add(storage.remove( ));}}