CS307 fall 2006, Midterm 1 Key and Grading Criteria: Grading acronyms OBOE - Off by one error. Calculation is off by one. AIOBE - Array Index out of Bounds Exception will occur NPE - Null Pointer Exception will occurs ABA - Answer by Accident GCE - Gross Conceptual Error. Did not answer the question asked or showed fundamental misunderstanding NAP - No answer provided. No answer given on test ECF - Error carried forward. BOD - Benefit of the Doubt. Not certain code works, but, can't prove otherwise Gacky - Code very hard to understand even though it works Question 1. Same as key or -2. Ignore differences in spacing, capitalization. A. 4 11 B. 16 14 C. 19 D. 4 6 7 E 9 2 3 F. Yes. The comment does nothing to enforce the precondition so someone could still send null as a parameter. (Or words to that effect. Note, the test instructions stated to assume preconditions were true on methods that the student writes. The student is not writing this method.) G. false H. 15 I 10 J. Thrown out. All students get credit. Question had unintended recursion. I intended to call super.cost, but did not. K. Syntax Error L. 2040 (20 40 okay too.) M. No, the class will not compile because it did not implement all of the methods from the interface, (Or words to that effect.) N. Yes, it inherits the Object class version of toString. O. Yes. Could have called the baseCost method instead of referencing the basePlanCost instance variable. (Or words to that effect.) Question 2 (Suggested Solution.) public static void fixData(int[] data){ int lastGoodIndex; int currentIndex = 0; while(currentIndex < data.length){ if( data[currentIndex] == -1){ //we have found the start of bad data lastGoodIndex = currentIndex - 1; // go find the index of the next good element of data while(data[currentIndex] == -1) currentIndex++; int diff = (data[currentIndex] - data[lastGoodIndex]) / (currentIndex - lastGoodIndex); //correct the bad data for(int i = lastGoodIndex + 1; i < currentIndex; i++){ data[i] = data[i - 1] + diff; } } else // current data is good. go on to next element currentIndex++; } } Break down of points: Loop through array 2 points Identify start of bad data attempt 2 points correct 2 points Count number of elements in bad section attempt 3 points correct 5 points calculate interpolated increase per element attempt 3 points correct 5 points add increment to elements in bad section attempt 3 points correct 5 points Question 3. suggested solution: public int[] numElementsEqualToTarget(int[][] mat, int target){ int[] result = new int[ mat[0].length ]; for(int r = 0; r < mat.length; r++) for(int c = 0 C < mat[0].length; c++) if( mat[r][c] == target ) result[c]++; return result; } correctly declaring result. Size must be correct. 2 points looping through rows 4 points looping through columns 4 points checking to see if element is match 4 points correctly updating result for match 4 points returning result 2 points Some confusion on rows vs. columns Question 4. suggested solution: public class Apartment{ private int myBedrooms; private int myRent; private int mySqFt; private double myBathrooms; public Apartment(int bedrooms, int rent, int sqFt, int bathrooms){ myBedrooms = bedrooms; myRent = rent; mySqFt = sqFt; myBathrooms = bathrooms; } public double avePerSqFt(){ return 12.0 * myRent / mySqFt; } public boolean hasMoreRooms(Apartment other){ return (myBedrooms + myBathrooms) > (other.myBedrooms + other. myBathrooms); } } point break down class header -> 1 point instance variables -> 4 points constructor -> 5 points average price per square foot method -> 5 points has more rooms method -> 5 points possible point losses instance vars: instance vars not private -1 wrong type on ints -1 wrong type on double -1 missing instance var -1 constructor: incorrect header -1 parameters missing or wrong -2 assignments -2 average price per square foot method any parameters -1 incorrect header (name can vary) -1 int division instead of double division -1 other problems with calculation -1 missing return -1 has more rooms method static method -1 two Apartment parameters -1 incorrect logic or calculation -1 return (myBedrooms > other.myBedrooms) && myBathrooms > other.myBathrooms); is okay incorrect header -1 no return -1