CS 307, Fall 2002, Midterm Solution and Criteria A word of explanation. If students ask what a particular acronym means here are some common ones: GCE: Gross Conceptual Error. The student was way off base and did not seem to really understand what the question required. AIOBE: Array Index Out of Bounds Expcetion. The code could result in an Array Index Out of Bounds Excpetion. Not always, but at some point. NPE: Null Pointer Exception. The code could result in a Null Pointer Exception. BOD: Benfit of the Doubt. We spotted them this one. ABA: Answer by accident. They have the right answer, but the wrong approach. This one does not show up much In general on coding questions if it really works (we could type it in, correct the minor syntax errors, and run it) the student should have gotten full credit. But people make lots of logic errors, major syntax errors, incorrect algoirthms. GACK: The code is very difficult to understand or follow. 1. Answers as shown or -2 A. 3.5 (extra 0's on end okay, e.g. 3.50000) B. 9 C. 5 10 6 8 12 4 15 20 (all on one line okay) D. 10 40 30 35 E. syntax error or compile error F. 50 75 6 4 Scale Factor 5 (different capitilzation okay) G. false (ignore capitilzation differences, f -> -1, 0 -> -2) H. true ( ignore capitilzation differences, t -> -1, 1 -> -2) I. 2 1 8 J. Runtime error or Null Pointer Exception K. Runtime error or Array Index Out of Bounds Exception (-2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 then runtime error okay too) L. 25 16 9 4 1 M. 5 N. list.length ^ 3 O. 20 or compile error (temp not initialized) 2. Model Solution public EncodeData[] compress(boolean[] data) { EncodeData[] result = new EncodeData[data.length]; int numDataObjects = 0; byte runLength = 1; boolean currentVal = data[0]; int index = 1; while(index < data.length) { if(data[index] == currentVal) runLength++; if(runLength == 127 || data[index] != currentVal) { result[numDataObjects] = new EncodeData(currentVal, runLength); numDataObjects++; currentVal = data[index]; if(runLength == 127) runLength = 0; else runlength = 1; } index++; } // get the last one result[numDataObjects] = new EncodeData(currentVal, runLength); numDataObjects++; EncodeData[] temp = new EncodeData[numDataObjects]; //trim it to correct size for(int i = 0; i < numDataObjects; i++) temp[i] = result[i]; return temp; } Criteria: Big problems: A lot of people assumed private variables and that this method was in the same class as the EncodedData class. The question did not make any mention of the private instance vars so to assume and use them was a major misconception. A lot of people did not catch the last run of data Many people did not trim the array of EncodedData objects to make sure it was the right size. no return -1 AIOBE -3 Missing the last run of data -3 Failure to trim array / ensure correct size -3 Major Logic Error -5 Minor Logic Error -2 GCE Anywhere from -12 to -25 depending on severity. Mike determined these. Major syntax error -1 Violation of encapsulation -5 3. Model Solution public byte[][] decompress(EncodeData[] code, int numRows, int numCols) { byte[][] result = new byte[numRows][numCols]; int limit = numRows * numCols; int bitsUsed = 0; int row = 0; int col = 0; for(int i = 0; i < code.length && bitsUsed < limit; i++) { for(int j = 0; j < code[i].getNum() && bitsUsed < limit; j++) { if( code[i].getValue() ) result[row][col] = 1; bitsUsed++; col = (col + 1) % numCols; if(col == 0) row++; } } return result; } Sigh. Maybe we should add a new acronym, TBW, Think Before Writing. It boggled my mind how badly people did on this question. What was difficult and therefore interesting was tracking the row and column in the resulting matrix, while also tracking how long the current run of data is and what EncodeData object we currently have. Many people made a mess of this. A lot of other people did not take precautions to handle the situation when the matrix would not hold all the data or the matrix is bigger than the available data. These situations were explicitly mentioned in the question. no return -1 major error in handling the row / column and which object / run length -5 other major logic error -5 minor logic error -2 AIOBE -3 not considering matrix will not hold all data (matrix < data) -5 not considering matrix is bigger than data (matrix > data) -5 accessing private instance vars (which were not specified) -5 no attempt to convert boolean to byte -1 casting incorrect (can't cast boolean to byte) -0 major syntax error -1 minor syntax error -0 GCE Anywhere from -12 to -25 depending on severity. Mike determined these. 4. Model Solution public class Packet implements Comparable { private static final int DATA_SIZE = 1000; private long lMySender; private long lMyDestination; private int iMyIDNum; private byte[] myData; /* pre: 0 <= idNum <= 2000000000, data != null, data.length <= DATA_SIZE */ public Packet(long sender, long destination, int idNum, byte[] data) { lMySender = sender; lMyDestination = destination; iMyIDNum = idNum; myData = new byte[DATA_SIZE]; for(int i = 0; i < data.length; i++) myData[i] = data[i]; byte pad = -1; for(int j = data.length; j < DATA_SIZE; j++) { myData[j] = pad; pad = (byte)-((pad + 1) % 2); } } public long getDestinantion() { return lMyDestination; } /* pre; 0 <= index <= DATA_SIZE */ public byte getData(int index) { return myData[index]; } public boolean equals(Object otherObject) { // perfect equals method code not necessary for full credit if( this == otherobject) return true; if( otherObject == null) return false; if( getClass() != otherObject.getClass()) return false; // otherObject is a non null Packet object Packet other = (Packet)otherObject; // if using String must use .equals method from String class return lMySender == other.lMySender && lMyDestination == other.lMyDestination && iMyIDNum == other.iMyIDNum; } public int compareTo(Object otherObject) { Packet other = (Packet)otherObject; return iMyIDNum - other.iMyIDNum; } } Criteria: -2 no constant for fixed length of data array. should be 1000 -4 data members public or not data members / instance var (1 per) -3 shallow copy of byte[] sent to constructor. i.e. myData = data instead of dynamic allocation and loop to copy values -3 no padding on data array note, if a student simply did the shallow copy and did not pad they most likely lost 8 points on this question. This was the only algorithmically interesting thing about the question and that is why it was so heavliy weighted. -2 using == instead of .equals on Strings (in equals method most likely.) -2 compareTo or equals compares data array -1 equals parameter data type NOT object -1 compareTo method parameter data type not Object or Comparable. (Object is the correct one, but we let Comprabel slide.) -1 using double on any of the data types (loss of precision) -1 other minor error Points per method guide 4 for data members 6 for constructor 2 fo ID accessor 2 for data array accessor 4 for equals methods 2 for comparteTo method if method missing all points lost for that method.