CS307 Midterm 1 2005 Suggested Solutions and Grading Criteria Abbreviations definitions: OBOE - Off By One Error AIOBE - Array Index Out of Bounds Exception LE - Logic Error GCE - Gross Conceptual Error ATQ - Answer the Question BOD - Benefit of the Doubt Gack - works, but poor style or difficult to understand. (no points off) 1. Answer as stated or -2. Differences in spacing, capitalization, or extra "s okay. A. 4 8 B. 35 C. Knife D. 24 E. 3 4 5 1 3 4 F. 1 8 8 G. 1 8 1 H. Syntax error / Compile error (due to myLength being private. Do not need to state reason for full credit.) I. color: pink, length 2 J. color: orange, length 1 K. Syntax error / Compile error (due to trying to instantiate an object of an abstract class.Do not need to state reason for full credit.) L. color: Yellow, length 2 M. false (or f) -2 if answer 0 N. No, because object variables can refer to objects of their declared type or any descendant. All classes are descendents of Object so variables or type Object can refer to any object. (or words to that effect.) O. Yes. A null pointer exception could occur if the value copied into c is null. (or words to that effect.) 2. Suggested solution: efficient: public int findBusiestDaysInARow(int[] hits, int numDays) { int max = 0; int sumOfCurrent = 0; int indexOfMax = 0; //get the first max: for(int i = 0; i max ) { max = sumOfCurrent; indexOfMax = i; } } return indexOfMax ; } The following was the most popular solution. public int findBusiestDaysInARow(int[] hits, int numDays) { int max = 0; int sumOfCurrent = 0; int indexOfMax = 0; for(int i = 0; i <= hits.length - numDays; i++) { sumOfCurrent = 0; for(int j = 0; j < numDays; j++) sumOfCurrent += hits[i + j]; if( sumOfCurrent > max ) { max = sumOfCurrent; indexOfMax = i; } } return indexOfMax; } Point break down: 4 - iterate through hits, attempt 4 - iterate through hits, correct 4 - calculate sum of each series of days in a row, attempt 4 - calculate sum of each series of days in a row, correct 4 - track max and index of max, attempt 4 - track max and index of max, correct 1 - return index of max common errors 1. Off by one in i <= hits.length - numdays 2. Not limiting out counter at all 3. mixing up the limits on the inner loop counter for(int j = i; j < numDays; j++) this simply doesn't work. 4. not resetting the temp sum 5. always looking at the same set of days for(int j = i; 0 < numDays; j++) sumOfCurrent += hits[j]; 3. Suggested Solution: public boolean isTriangular() { boolean result = false; //check square if( numRows() == numCols() ) { // check if all zeros above below main diagonal result = true; for(int row = 1; row < numRows() && result; row++) for(int col = 0; col < row && result; col++) result= getVal(row, col) == 0; if( !result ) { result = true; // check if all zeros above below main diagonal for(int row = 0; row < numRows() && result; row++) for( col = row + 1; col < numCols() && result; col++) result = getVal(row, col) == 0; } } return result; } Point break down: 1 - check calling object is square, attempt 1 - check calling object is square, correct 5 - check upper, attempt 6 - check upper, correct 5 - check lower, attempt 6 - check lower, correct 1 - return common errors: 0. errors in calculating range of indices to check. (most common) 1. Off by one errors 2. accessing things via Matrix. instead of myCells. 3. Array index out of bounds exceptions 4. Suggested solution. (Arrays are your friend!) public class Lock { public static final int NUM_TUMBLERS = 4; private int[] mySettings; private int[] myRequired; public Lock(int[] required) { //pre: required != null, required.length = NUM_TUMBLERS, // all elements of required are between 0 and 9 inclusive mySettings = new int[NUM_TUMBLERS]; myRequired = new int[NUM_TUMBLERS]; for(int i = 0; i < NUM_TUMBLERS; i++) myRequired[i] = required[i]; } public void alterTumbler(int tumbler, int newSetting) { // pre: 0 <= tumbler < NUM_TUMBLERS, 0 <= newSetting <= 9 mySettings[tumbler] = newSetting; } public boolean canOpen() { // pre: none boolean correct = true; int i = 0; while( correct && i < NUM_TUMBLERS) { correct = mySettings[i] == myRequired[i]; i++; } return correct; } public boolean sameSettings(Lock otherLock) { //pre: otherLock != null boolean same = true; int i = 0; while( same && i < NUM_TUMBLERS; { same = mySettings[i] == otherLock.mySettings[i]; i++; } return same; } } Point breakdown: 2 - instance var(s) for current settings. 2 - instance var(s) for required settings. the constructor and methods are worth 4 points each 1 point: for stated precondition (no need to check precondition) 1 point: attempt to carry out purpose of method / constructor 2 points: correctly carry out purpose of method / constructor (-1 minor error, -2 major error)