CS305J Fall 2006 final Exam suggested solutions and grading criteria. 1. Answer as written or -1. Missing "'s okay. 5 5 "2AT1" 3.5 DC23 true true 2. Answer as written or -2. [] or {} or () okay. {} { 25 } {7, 21} {7, 3, 2, 5} 3. Suggested Solution: public static boolean passed(double ave, boolean under){ return (ave >= 70) || (under && ave >= 60); } criteria 1 point, header and parameters 3 points logic correct 1 point return 4. Suggested Solution public static boolean alternateSigns(int[] data){ if( data.length == 0 ) return true; if( data[0] == 0 ) return false; boolean positive = data[0] > 0; boolean good = true; int position = 1; while( good && position < data.length - 1){ if( positive ) good = data[position + 1] < 0; else good = data[position + 1] > 0; position++; positive = !positive; } return good; } criteria: 1 point, special cases empty list and first element 0 3 points, attempt to traverse list 3 points, correctly traverse list 3 points, attempt to check alternating sign 4 points, correctly check alternating sign. (-2 if 0s not handled correctly) 1 point, return result 5. Suggested Solution public static int columnWithMaxTrues(boolean[][] mat){ int maxTrues = -1; int colWithMax = -1; int numTrues = 0; for(int c = 0; c < mat[0].length; c++){ numTrues = 0; for(int r = 0; r < mat.length; r++){ if( mat[r][c] ) numTrues++; } if( numTrues > maxTrues ) { colWithMax = c; maxTrues = numTrues; } } return colWithMax; } criteria: 1 point, attempt to traverse columns 2 points, correctly traverse columns (columns must be outer loop to earn this or must use temp array) 1 point, attempt to traverse rows 2 points, correctly traverse rows (columns must be inner loop to earn this or must use temp array) 2 points, attempt to track trues per column 2 points, correctly track trues per column 2 points, attempt to check and update information for column with max trues 2 points, correctly check and update information for column with max trues 1 point, return value 6. Suggested Solution: public class Longhorn implements Critter{ private int direction; private int stepsThisLeg; private int stepsSoFar; public Longhorn(){ if( Math.random() > 0.5 ) direction = NORTH; else direction = SOUTH; stepsThisLeg = 1; stepsSoFar = 0; } public int fight(String opponent){ int result; if( Math.random() > 0.5 ) result = SCISSORS; else result = ROCK; return result; } public Color getColor(){ return Color.ORANGE; } public String toString(){ return "B"; } public int getMove(CritterInfo info){ if( stepsSoFar == stepsThisLeg){ stepsSoFar = 0; stepsThisLeg++; if( Math.random() > 0.5 ) direction = NORTH; else direction = SOUTH; } stepsSoFar++; return direction; } } criteria: 1 point, header. Lose this if does not include implements Critter 3 points, fields / instance variables (-2 if not private) (partial credit possible) 2 points, constructor. (could get tricky if getMove correct) (partial credit possible) 2 points, fight (partial credit possible) 1 point, getColor 1 point, toString 5 points, getMove (partial credit possible) 7. Suggested Solution: public static boolean oneDifferent(String s1, String s2){ // check if lengths different by 2 or more characters if( Math.abs( s1.length() - s2.length() ) > 1 ) return false; int smallestLength = Math.min( s1.length(), s2.length() ); // set initial value of number of differences. If lengths different by 1 start at 1 // difference, otherwise start at 0 differences int numDiff; if( s1.length() != s2.length() ) numDiff = 1; else numDiff = 0; // move through each character in the Strings, incrementing number of differences // if the characters are not equal. int position = 0; while( position < smallestLength && numDiff <= 1){ if( s1.charAt(position) != s2.charAt(position) ) numDiff++; position++; } return numDiff == 1; } criteria: 2 points, attempt to traverse both Strings 2 points, correctly traverse both Strings 2 points, attempt to track number of differences 3 points, correctly track number of differences (-1 if doesn't handle case of Strings with difference in lengths is greater than 2) 1 return result 8. Suggested Solution: public static void moveToFront(ArrayList list, int length){ int positionForShort = 0; String temp; for(int i = 0; i < list.size(); i++) { if( list.get(i).length() <= length ){ temp = list.remove( i ); list.add(positionForShort, temp); positionForShort++; } } } criteria: 1 point, attempt to traverse elements of list 2 points, correctly traverse elements of list 1 point, attempt to identify elements with Strings of proper length 2 points, correctly identify elements with Strings of proper length 1 point, attempt to move elements to front 3 points, correctly move elements to front while maintaining relative order 9. Suggested Solution: public static int[] reverseInRange(int[] data, int min, int max){ int numInRange = 0; for(int i = 0 ; i < data.length; i++){ if( data[i] >= min && data[i] <= max ){ numInRange++; } } int[] result = new int[numInRange]; int positionInResult = 0; for(int i = data.length - 1; i >= 0; i--){ if( data[i] >= min && data[i] <= max ){ result[positionInResult] = data[i]; positionInResult++; } } return result; } criteria: 2 points, attempt to find number of values in range 3 points, correctly find number of values in range 2 points declare result of proper size 3 points, attempt to add elements from data in range to result 4 points, correctly add elements from data in range to result. (must correctly reverse order) 1 point, return result