COMP-202A, Fall 2009, All Sections FINAL EXAM SOLUTIONS SHORT QUESTIONS --------------- 1. a) A loop is a control flow construct which causes a set of statements to be repeatedly executed as long as some condition is true. b) A method is a list of instructions which has been given a name; the name of this list of instructions can be used as a single instruction elsewhere in the program, and the execution of this single instruction causes all the instructions in the list to be executed. c) A reference variable contains the address in memory where the actual value (an array or object) is stored instead of containing the actual value. d) A class defines a category of objects, and specifies a blueprint for the objects which belong to this category; this blueprint specifies how the objects which belong to the category defined by the class look and behave. e) A constructor is a method-like list of instructions which specifies what should happen when a new object is instantiated with the new operator. 2. a) The final reserved word is used as a modifier in a variable declaration, and makes the variable a constant; the variable can only be assigned one value (at declaration) and this value cannot be changed by a statement during the execution of the program. b) The public reserved word is used as a modifier for class members (instance or class variables or methods) to indicate that the member is accessible from methods declared in any class. c) The void reserved word is used as a return type in the header of a method to specify that a method returns no value. d) The return reserved word is used as a statement in a method to indicate that the method should immediately cease its execution; a statement involving the return reserved word must specify a return value if the method is specified to return such a value. e) The new reserved word is used to create a new object or array. 3. add(int, int): 1 + 3 + 4 add(int, int): 2 + 3 + 4 add(int, double): 1 + 5 + 6.0 add(double, double): 2 + 7.0 + 8.0 n1.add(3, 4) == 8.0 n2.add(3, 4) == 9.0 n3.add(5, 6.0) == 12.0 n4.add(7.0, 8.0) == 17.0 LONG QUESTIONS -------------- 4. a) - elements (instance variable) - myElements (formal parameter) b) - elements (instance variable) - result (local variable) - i (local variable) c) - elements (instance variable) - result (local variable) d) - args (formal parameter) - a (local variable) - s (local variable) 5. 1, 2 {4, 3} {{7, 8}, {5, 6}} [L: 10; R: 9], [L: 11; R: 12], [L: 13; R: 14] {[L: 19; R: 20], [L: 17; R: 18]} {[L: 15; R: 16], [L: 21; R: 22]} PROGRAMMING QUESTIONS --------------------- 6. a) public class Robot { private String name; private int hitPoints; private double accuracy; private String weapon; private Weakness[] weakPoints; public Robot(String n, int hp, double a, String w, Weakness[] wP) { name = n; hitPoints = hp; accuracy = a; weapon = w; weakPoints = new Weakness[wP.length]; for (int i = 0; i < weakPoints.length; i++) { weakPoints[i] = wP[i]; } } public String getName() { return name; } public boolean isFunctional() { return (hitPoints > 0); } public boolean takeDamage(String weapon) { Weakness weakSpot; boolean damaged; int i; damaged = false; i = 0; while (i < weakPoints.length && !damaged) { if (weapon.equalsIgnoreCase(weakPoints[i].getWeapon())) { damaged = true; hitPoints = hitPoints - weakPoints[i].getDamage(); if (hitPoints < 0) { hitPoints = 0; } } else { i = i + 1; } } return damaged; } public boolean attack(Robot target) { double randomNumber; boolean hit; randomNumber = Math.random(); hit = false; if (randomNumber < accuracy) { hit = target.takeDamage(weapon); } return hit; } public String toString() { return name + " (" + hitPoints + " HP)"; } } b) public static Robot fight(Robot robot1, Robot robot2) { Robot winner; winner = null; while (robot1.isFunctional() && robot2.isFunctional()) { robot1.attack(robot2); robot2.attack(robot1); } if (robot1.isFunctional()) { winner = robot1; } else { if (robot2.isFunctional()) { winner = robot2; } } return winner; } 7. public static void diff(String left, String right) throws IOException { Scanner leftScanner; Scanner rightScanner; String leftLine, rightLine; leftScanner = new Scanner(new File(left)); rightScanner = new Scanner(new File(right)); while(leftScanner.hasNextLine() || rightScanner.hasNextLine()) { if (leftScanner.hasNextLine() && rightScanner.hasNextLine()) { leftLine = leftScanner.nextLine(); rightLine = rightScanner.nextLine(); if (!rightLine.equals(leftLine)) { System.out.println("<" + leftLine); System.out.println(">" + rightLine); } } else if (leftScanner.hasNextLine()) { System.out.println("<" + leftScanner.nextLine()); } else { System.out.println("> " + rightScanner.nextLine()); } } leftScanner.close(); rightScanner.close(); } 8. public static void eliminateComments(ArrayList lines, String path) throws IOException { PrintStream fileWriter; String line; int size; int position; fileWriter = new PrintStream(path); size = lines.size(); for (int i = 0; i < size; i++) { line = lines.get(i); position = line.indexOf("//"); if (position != -1) { fileWriter.println(line.substring(0, position)); } else { fileWriter.println(line); } } fileWriter.close(); }