Oct 3, 2007 Name __________________ CS241 Exam 1 Fall 2007 1. 2. 3. 4. (max = 10) 5. (max = 10) (max = 28) 6. (max = 12) (max = 10) 7. (max = 7) (max = 10) Total Score ________(max=87) Final grade: ________/ 100 1) (10 pts) What are the features of good programming? Briefly describe each feature. What are the trade-offs, if any, between them, e.g., is it possible to get one at the expense of another? 1 2) (4 pts each, 28 pts total) Define the following terms. Include an example for each. If you run out of space for your answer, use the back of the last page. a) Polymorphism b) Overloading c) Java Call Stack and Call Frame d) Wrapper 2 e) Encapsulation f) Abstract Data Type g) LIFO vs FIFO 3 3) (10 pts) The picture below shows the structure of a 2D array of cards called myCards, . Write the java code necessary to create the structure shown below. Use loops as much as possible. 4 4) (10 pts) Suppose s1 and s2 are String objects. Explain the difference between the following three expressions. What exactly is each expression (e.g. assignment statement, conditional, …)? Where appropriate, include a UML instance diagram to aid in your explanation. a) s1 = s2 b) s1 == s2 c) s1.equals(s2) 5) (10 pts) Iterable and Iterator are both interfaces associated with lists. a) (5 pts) What method(s) are in the Iterable interface? What does each method do? b) (5 pts) What method(s) are in the Iterator interface? What does each method do? 5 6) (12 pts total) Exceptions. Consider the code below where s is a String entered by a user somewhere else in the code. Line Line Line Line Line Line Line 1: public double doSomething(String s, double[] nums) { 2: System.out.println(“My string is “+s); 3: double d = Double.parseDouble(s); 4: double sum = 0; 5: for (int i=0; i<10;i++) sum += nums[i]; 6: return sum/d; 7: } a) (6 pts) For each of Java exceptions: NullPointerException, NumberFormatException, ArithmeticException, ArrayIndexOutOfBoundsException, indicate which line or lines (if any) might throw the exception. Explain your answers. b) (6 pts) Rewrite the above code so that when an exception is thrown, the code does not crash but instead prints out an error message (e.g. to System.out) that identifies the specific problem. In addition, when any exception is thrown, the method should return the value -1. Include a catch-all so that if an unanticipated exception is thrown, then the code will still not crash. 6 7) (7 pts) Generics a) (5 pts) Suppose you have the following class called Node which stores a single Card object public class Node { Card c; public Node(Card c) this.c = c; } { public void setValue(Card c) { this.c = c; } public Card getValue() { return c; } public String toString() { return c.toString(); } } Modify the above code (by crossing out and/or inserting) so that the Node class can store any generic object and not just a Card object. b) (2 pts) To test the original Node code, one might have added the following main method: public static void main(String args[]) { int rank = 1; int suit = 3; Node n = new Node(new Card(1,3)); System.out.println("node " + n); } How would the line Node n = new Node(new Card(1,3)); need to be re-written so that it would work with your Generic Node class instead? Assume you are still storing the same Card object. 7