CS307 Spring 2010 Midterm 1 Solution and Grading Criteria.
Grading acronyms:
ABA - Answer by Accident
AIOBE - Array Index out of Bounds Exception may occur
BOD - Benefit of the Doubt. Not certain code works, but, can't prove otherwise
ECF - Error carried forward.
Gacky or Gack - Code very hard to understand even though it works or solution
is not elegant. (Generally no points off for this.)
GCE - Gross Conceptual Error. Did not answer the question asked or showed
fundamental misunderstanding
LE - Logic error in code.
NAP - No answer provided. No answer given on test
NN - Not necessary. Code is unneeded. Generally no points off
NPE - Null Pointer Exception may occur
OBOE - Off by one error. Calculation is off by one.
1. Answer as shown or -2 unless question allows partial credit.
No points off for differences in spacing and capitalization. If quotes included,
okay.
A. [2, 6] (comma differences and brace differences okay, just need 2 and 6 in that order.)
B. Runtime error OR exception or ArrayIndexOutOfBoundsException
C - F. Can be other than valid / invalid. Some way of saying declaration is okay
or not. Explanations NOT needed. -1 each part
C. 1. valid
2. invalid
D. 1. invalid
2. valid
E. 1. invalid
2. invalid
F. 1. valid
2. valid
G. false
H. 6000
I. J's
J. 100 4000
K. 2000 4000
L. 5000
M. The rooms variable is private and it is a syntax error to try and access it in any class besides the Residential class. (or words to that effect.)
N. 0 [5, 12] (comma differences and brace differences okay.)
O. -1
2. Comments. This questions turned out to be a little harder than I thought. There were two lists with their own containers (arrays of objects) and the size of the list being represented may have been smaller than the length of the container. So, a lot of abstraction going on. Additionally, there was the problem of an item that appeared multiple times in the other list that was not present in the original list. This turned out to be a good problem and I saw a lot of different ways of solving it.
Common problems:
- using == or compareTo instead of .equals
- confusing length of array and list size
- not updateing listSize
- resizing for every element added instead of only when necessary
Suggested Solution:
public void addNewValues(GenericList other){
int startSize = listSize;
for(Object obj : other.container)
if( !contains(0, startSize, obj)
add(obj);
}
private boolean contains(int start, int stop, Object obj){
for(int i = start; i <= stop; i++)
if(container[i].equals(obj)
return true;
return false;
}
public void add(Object obj){
if(listSize == container.length)
resize();
container[size] = obj;
listSize++;
}
Grading Criteria: 20 points
loop through elements on other GenericList (based on its listSize)
attempt: 2 points
correct: 2 points (duplicates not present in original added correctly)
determine if element from other list was already present in original list
attempt: 2 points
correct: 4 points (must use equals or -2)
add elements from other to this GenericList
attempt 2 points
correct 4 points
resize if necessary, 2 points
update size, 2 points
3. Comments. A little bit of design. There had to be some way of storing the positional data and the value for the non zero elements. It is also necessary to track the size since they are no longer implicity availabel in the 2d array of ints. Then that data needed to be stored somewhere. Examples of where to store it were in an ArrayList, a 2d array of ints (3 columns per row), a String (although that would be hard to pull the data out of) or even a 1d array of ints with every three elements storing the information about one none zero element.
3A. Suggested Solution:
I would create a new class named Element which stores a row, a column, and a value for all non zero elements in the matrix. I would then use an ArrayList of elements to store the non zero elements in the matrix. I would also have variables for the number of rows and number of columns in the matrix becuase those are no longer implicity stored in the 2d array of ints.
5 points for a reasonable explantion. Partial credit if skimpy on details.
3B. Suggested Solution
public class Element{
public Element(int row, int col, int value)
public int getRow()
public int getCol()
public int getValue()
}
public class SparseMatrix {
private int numRows;
private int numCols;
private ArrayList nonZeroValues;
public SparseMatrix(int[][] values) {
nonZeroValues = new ArrayList();
numRows = values.length;
numCols = values[0].length;
for(int r = 0; r < values.length; r++)
for(int c = 0; c < values[0].length; c++)
if( values[r][c] != 0 )
nonZeroElements.add( new Element(r, c, values[r][c] );
}
Grading Criteria: 20 points
instance variables declared: 3 points
valid way of storing non zero elements and positional data, 3 points
store size of matrix 3 points
loop through values
attempt: 2 points
correct: 2 points
store non zero elements from values
attempt: 3 points
correct: 4 points
4. Comments. This turned out to be the easiest of the three coding questions. Common problems included:
- using a nest loop. Only a single loop was needed to check consecutive decades.
- not accessing ranks.size() or ranks,get(int) correctly
- logic errors with Math.abs
- no tdealign with the stored 0's correctly.
- hard coding in the number of decades. That makes it very difficult to change the data.
Suggested Solution
public boolean hadMove(int move) {
boolean result = false;
for(int i = 1; i < myRanks.size() && !result; i++){
// get ranks and adjust 0s
int prev = myRanks.get(i - 1);
if(prev == 0)
prev = 1001;
int current = myRanks.get(i);
if( current == 0)
current = 1001;
int diff = prev - current;
if(move >= 0 && diff > move)
result = true;
else if(move < 0 && diff < move)
result = true;
}
return result;
}
Grading criteria: 20 points:
check consecutive decades from ranks using loop (single loop!):
attempt: 3 points
correct: 4 points
adjust / handle ranks of 0 correctly
attempt: 2 points
correct: 2 points
check if difference between decades met move criteria (can ignore move of 0)
attempt: 3 points
correct: 4 points
return answer: 2 points