CS 307 Fall 03 Midterm 1 Solution and Criteria 1 Criteria For all A-F the answer as below (ignore caps, spacing and line breaks) or -2 For G-H, words to the effect of those in the answer or -2. Use -1 at your disgression A 1 26 65 B Runtime error due to an array index out of bound exception. This was an unintended runtime error so the intened asnwer 13 was also accepted. C 5 3 5 3 10 9 6 2 10 9 5 3 D Kit-Kat 200 Crunchy Kit-Kat 200 Crunchy Kit-Kat 600 Crunchy Kit-Kat 600 Crunchy E Snickers 300 with Peanuts Snickers 300 with Peanuts Snickers 500 with Peanuts Snickers 300 with Peanuts F false true Snickers 200 with Peanuts Snickers 200 with Peanuts G Runtime error due to a Null Pointer Exception. When cList is created it has 3 ChocolatyCancy object variables that are all null. There are 0 ChocolatyCandy objects. (or words to that effect) H SugaryCandy will not compile becuase it extends Candy but does not proivde an implemenation for the abstract method getDescriptor. If this were not a syntax error, then I could create an object for which a beahavior is not defined. In other words there is no code to carry out a method, I Polymorphism allows an object variable to hold the memory address of any object of that type, or any object vthat is a descendant of the declared type of the object vairable, or if the data type is an interface any classes that implement the interface. J Syntax error. iMycalories is declared with private access, thus code in a class other than Candy cannot access that variable. Question 4 on back 2 int[] numOfSuit = new int[4]; for(int i = 0; i < hand.length; i++) numOfSuit[hand[i].getSuit()]++; boolean flush = false; for(int i = 0; i < numOfSuit.length; i++) if( numOfSuit[i] >= 5 || numOfSuit[i] == hand.length ) flush = true; return flush; /* simple array processing question. Most people did great. */ 3 int[] result = new int[numCols()]; int max = 0; for(int col = 0; col < numCols(); col++) { max = myCells[0][col]; for(int row = 0; row < numRows(); row++) { if( getVal(row, col) > max ) max = getVal(row,col); if( rhs.getVal(row, col) > max ) max = rhs.getVal(row,col); } result[col] = max; } return result; /* The biggest problems I saw on this question were not initializing max to a proper value for each column. Many people assigned it 0 for each column, but what if all values for that column, in both matrices were negative? The second big problem, which is a lot more serious, was confusion between rhs, a Matrix variable, and rhs.myCells, the 2 dimensional array of ints inside the Matrix object. Saying things like rhs[row][col] shows a fundamental misunderstanding between an object variable and the instance varaibles that object uses to model something. */ 4 public class RoadSection { private boolean hilly; private boolean curvy; private boolean div; pribate int lanes; public RoadSection() { hilly = false; curvy = false; div = false; lanes = 2; } public RoadSection(boolean h, boolean c, boolean d, int la) { hilly = h; curvy = c; div = d; lanes = la; } public boolean slow() { return (hilly || curvy || (!div && lanes < 3)); } public boolean isHighway() { return div && lanes >= 4; } public boolean equals(Object other) { RoadSection r = (RoadSection)other; return hilly == r.hilly && curvy == r.curvy && div == r.div && lanes == r.lanes; } public String to String() { String h = (hilly) ? "Hilly," : "Flat,"; String c = (curvy) ? "Curvy," : "Straight,"; String d = (div) ? "Divided," : "Undivided, "; return lanes + " lanes, " + h + c + d; } } -1 per missing instance var (may be named different, essentially need 4) -2 if any instance vars not declared private -3 per missing method or constructor -1 if any mutators or accessors included. The problem specifcally said to not include any other accessors or mutators. -1 minor logic error in a method (ignore minor syntax errors) max 1 off per method -2 major logic error in a method (ignore minor syntax errors) max 2 off per method Send me scores for yoru seciton FirstName LastNamerawScore (out of 100) Add up points off and subtract from 100 for raw score.