CS307 Fall 2007, Midterm 1 Key and Grading Criteria: Grading acronyms OBOE - Off by one error. Calculation is off by one. AIOBE - Array Index out of Bounds Exception will occur NPE - Null Pointer Exception will occurs ABA - Answer by Accident GCE - Gross Conceptual Error. Did not answer the question asked or showed fundamental misunderstanding NAP - No answer provided. No answer given on test ECF - Error carried forward. BOD - Benefit of the Doubt. Not certain code works, but, can't prove otherwise Gacky or Gack - Code very hard to understand even though it works or solution is not elegant. (Generally no points off for this.) 1. Same as key or -2. Ignore differences in spacing, capitalization. On short answer equivalent answers are acceptable. A. 16 B. 6_2 C. 0 D. kpuz E. Parameters in Java are passed by value. A copy of the value in x and y are made and so the method cannot change the value stored in the arguments x and y. (Or words to that affect.) F. -2 G. No, it is not a proper use of a try catch block. The NullPointerException and ArrayIndexOutofBounds should be prevented using an if statement or assert. (Or words to that affect.) H. Cannot instatiate objects of an abstract class. (Or words to that affect.) I. true J. engine: false Mountain Bike K. 20 L. Object, Vehicle, Bike, and MountainBike M. Yes, it inherits the equals method from Object. N. The code will not compile becuase the declared tupe of b is Bike and the Bike class does not have a gears method. (Or words to that affect.) O. 6 2 2. Suggested Solution public boolean equalSuits(Hand otherHand){ boolean result = false; if( myCards.length == otherHand.myCards.length){ int[] suitCount = new int[4]; for(int i = 0; i < myCards.length; i++){ suitCount[ myCards[i].getSuit()]++; suitCount[otherHand.myCards[i].getSuit()]--; } result = true; for(int i = 0; i < suitCount.length; i++) if( suitCount[i] != 0 ) result = false; } return result; } Criteria: iterate through both arrays of cards 4 attempt 4 correct determine one for one matching of suits between hands this can be done in many ways. The suggested solution counts the number of cards of each suit in each array and ensures they are equal 7 attempt 7 correct return result 3 Comments and comment problems. - lots of solutions with 8 seperate variables for the number of cards of each suit. Remember arrays. They are your friend. - The Hand is not the array. Many solutions thought the Hand was the array. For example otherHand[i].getSuit(). That is incorrect. otherHand.myCards[i].getSuit() is correct. - Some solutions did not consider the fact that the number of cards in the two Hand object may be different and thus the result must be false. - the .equals method for arrays does not compare the individual array elements, just the references 4. Suggested Solution public class CablePlan implements Comparable{ private double base; private int numRegular; private int numPremium; public CablePlan(double b, int reg, int pre){ base = b; numRegular = reg; numPremium = pre; } public double cost(){ return base + numRegular * 0.25 + numPremium * 1.50; } public int compareTo(CablePlan other){ double diff = cost() - other.cost(); int result = 0; if( diff < 0 ) result = -1; else if( diff > 0 ) result = 1; return result; } } point break down class header -> 1 point instance variables -> 4 points constructor -> 5 points cost method -> 5 points compareTo -> 5 points possible point losses header: implements Comparable okay instance vars: instance vars not private -1 wrong type on ints -1 wrong type on double -1 missing instance var -1 constructor: incorrect header -1 parameters missing or wrong -2 assignments -2 cost method any parameters -1 incorrect header (name can vary) -1 problems with calculation -2. (-1 if minor) missing return -1 compareTo method static method -1 two CablePlan parameters -1 incorrect logic or calculation -1 incorrect header -1 (Data type may be Object, Comparable, or CablePlan) no return -1 missing cast is okay