CS307 Spring 2005 Midterm 1 Suggested Solutions and Grading Key 1. Answer as written or -2. Ignore differences in whitespace and capitalization and small spelling errors on F - L A. 10 B. 2 9 3 5 9 7 6 (Something showing contents of array. Lack of indices okay) C. 0 -1 -2 1 0 -1 D. ABAABCCCBAB E. 3 4 4 3 3 4 F. Robin's power is acrobatics Nightwing's power is acrobatics G. Matches's power is Disguises Batman's power is Intelligence H. Syntax error (Just error = -2) I. Wonder Woman's power is Golden Lasso and can fly J. gone's power is power ring and can fly K. false L. Super Strength's power is Agile and can fly M. eirt N. Flashlas O. Yes. An array out of bounds index could occur if x is less than 0 or greater than list.length. (Or words to that effect. It is also possible a null pointer exception could occur if list is null, but students do not have to mention this.) 2. Suggested Solution. (There were many ways of solving this problem. I think the simplest was to find the max and then check to make sure the values ascend from the start to the max and then descend from the max to the end of the array. Most of the students that tried this approach got it right. Another option was to look at the changes in elevation. A third and popular choice was to look for local maximum's and make sure there was only one. This approach also needed to ensure the road started going up and finished going down.) //max approach public static boolean peak(int[] road) { int i = 1; boolean climb = false;; while( i < road.length - 1 && climb ) { climb = road[i] > road[i - 1]; //System.out.println( i + " " + climb ); i++; climb = true; } boolean descent = true; while( i < road.length && descent ) { descent = road[i] < road[ i - 1]; //System.out.println( i + " " + descent ); i++; } //System.out.println( i + " " + climb + " " + descent); return i == road.length && descent && climb; } //checking elevation changes correctly (start out up. Go up till down. Down till end) //student answer public static boolean peak(int[] road) { boolean peak = true; boolean afterPeak = false; if( road[1] < road[0]) peak = false; for(int i = 1; i < road.length; i++) { if(road[i] > road[i-1] && afterPeak ) peak = false; else if( road[i] < road[i-1] && !afterPeak) afterPeak = true; else if( road[i] == road[i-1]) peak = false; } if(!afterPeak) peak = false; return peak; } //looking for local max's method //student answer public static boolean peak(int[] road) { boolean result = true; int numPeaks = 0; if( road[0] >= road[1] || road[road.length - 1] >= road[road.length - 2] ) result = false; for(int i = 1; i < road.length - 1 && result; i++) { if(road[i] == road[i-1] || road[i] == road[i+1]) result = false; else if( road[i] > road[i-1] && road[i] > road[i+1] && numPeaks == 0 ) numPeaks++; else if( road[i] > road[i-1] && road[i] > road[i+1] && numPeaks > 0 ) result = false; } return result; } 3. Biggest problems were not calculting size of result correctly and setting element of result to true if the corresponding elements of input and maask were equal. Question clearly stated the mask and input both had to be true for the result to be true. public boolean[][] applyMask(boolean[][] input, boolean[][] mask) { int rows = Math.min(input.length, mask.length); int cols = Math.min(input[0].length, mask[0].length); boolean[][] result = new boolean[rows][cols]; for(int r = 0; r < result.length; r++) for(int c = 0; c < result[0].length; c++) result[r][c] = input[r][c] && mask[r][c]; return result; } 4. The hard thing about this question was calculating the difference between the powers of the earthquake. I made the problem harder than necessary by not stating the more powerful earthquake should always be listed first. The problem made it seem the calling earthquake should always be listed first. Either approach is okay. (Although always listing the calling object is harder to get correct IMO.) public class Earthquake { private String myName; private int iMyMagnitude; //pre: name != null public Earthquake(String name, int mag) { myName = name; iMyMagnitude = mag; } //pre: name != null public void changeName(String name) { myName = name; } //pre: other != null public void showDiff(Earthquake other) { int powDiff = 0; //calculte differences in power if( iMyMagnitude != other.iMyMagnitude ) { powDiff = 1; int count = iMyMagnitude - other.iMyMagnitude; boolean smaller = false; if( count < 0 ) { count = -count; smaller = true; } for(int i = 0; i < count; i++) powDiff *= 32; } String message = myName + " and " + other.myName " were the same intensity."; if( powDiff != 0) { message = "The " + iMyName + " earthquake was "; if( smaller ) message += "1/"; message += powDiff + " times more powerful than the " + other.myName + " earthquake."; } System.out.println( message ); } } Point break down instance vars 3 constructor 3 changeName method 3 show difference method 11 Do not take off points for minor syntax errors. instance cars not private -1 no preconditions -1 per method showDiff method taking in 2 params of typ earthquake -2