S06 FINAL KEY -- by Zach Tomaszewski ------------- 1) D, X, F, B, C. Because: A = method signature, B = interface, C = local variable, D = object/instance, E = instance variable, F = exception 2) C, D, A, F, X Because: A = inheritence, B = method overloading, C = protected D = encapsulation, E = private, F = polymorphism Note: if D said "when inaccessible to outside instances or classes" or "when members are inaccessible to outside instances or classes", then the answer would be "private". But if the properties/fields/instance variables are directly inaccessible, but can still be changed through the use of accessible capabilities/methods, that's "encapsulation". 3) E, F, C, A, D Because: A = for, B = continue, C = do-while, D = switch, E = if, F = while, G = break Remember: any for or do-while loop can be rewritten as a while loop. 4) (*#) means footnote; see below. Answers: 40 X (*1) true 4 (*2) 2 true false X (*3) X true true true 4true X (*4) 14 X X (*5) false 11 X *1: This won't compile because c is a primitive data type (boolean), and so it cannot be dereferenced. (That is, primitive data types don't have methods.) If c was a Boolean, then the answer would be true. *2: b contains the value 3 after this operation; but the expression evaluates to the inital value of b, which is 4. That is: System.out.println("Result: " + b--); System.out.println("After: " + b); would print out: Result: 4 After: 3 (Compare this to using --b.) *3: ! can only be applied to a boolean operand *4: can't apply the numeric operator > to boolean. *5: can't compare int and String 5) class Instance { private static int instances = 0; public Instance() { instances++; } public static int numberOfInstances() { return Instance.instances; } } public class Program { public static void main(String[] args) { for (int i = 0; i < 100; i++) { new Instance(); //creates an instance, but doesn't save it System.out.println("Number of instances created: " + Instance.numberOfInstances()); } } } 6) /* * These methods below accept 0, 1, and 2 as input. * * But it's also possible to pass a non-valid integer as a parameter. * In practice, error checking is always required. * On exams, error checking is usually not required (though it's always * best to ask if you're not sure). * * I went ahead and handled the error cases too. * Since the exam doesn't mention anything about using an exception, * I returned an error message String in that case instead. */ /* using an if/else */ public String message(int grade) { if (grade == 2) { return "You got A grade"; }else if (grade == 1) { return "You got B grade"; }else if (grade == 0) { return "You got F grade"; }else { return "Error: " + grade + " doesn't translate to a letter grade."; } } /* using a switch */ public String message(int grade) { switch (grade) { //returning, so break for each case not required case 2: return "You got A grade"; case 1: return "You got B grade"; case 0: return "You got F grade"; default: return "Error: " + grade + " doesn't translate to a letter grade."; } } /* using the grades array */ public String message(int grade) { try { return "You got " + grades[grade] + " grade"; }catch (ArrayIndexOutOfBoundsException aioobe) { return "Error: " + grade + " doesn't translate to a letter grade."; } } 7) //import wheels-like package here import java.awt.Color; public class CheckerBoard { private static final int ROWS = 8; private static final int COLUMNS = 8; private static final int SQUARE_SIZE = 50; //size in pixels private static final Color[] colors = {Color.yellow, Color.red}; /** * Creates a new CheckerBoard of the standard size */ public CheckerBoard() { //Frame window = new Frame(); //pop-up the display window //draw squares for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLUMNS; col++) { //alternate colors depending whether sum of col and row //indices is odd or even Color squareColor = colors[(col + row) % 2]; new Rectangle(row * SQUARE_SIZE, col * SQUARE_SIZE, //x, y SQUARE_SIZE, SQUARE_SIZE, //width, height squareColor); //color } } } public static void main(String[] args) { CheckerBoard board = new CheckerBoard(); } } 8) There are three areas you need to fill in code: in Shape, in Rectangle, and in ShapeAndAreas' contructor. In Shape: /* Constructor */ public Shape(String name) { this.name = name; } /* Declare the abstract method to be overridden by subclasses */ public abstract double area(); In Rectangle: private double width; private double height; public Rectangle(String name, double width, double height) { super(name); this.width = width; this.height = height; } public double area () { return width * height; } In the ShapesAndAreas constructor: double totalArea = 0.0; //loop through each shape, printing it and getting area for (int i = 0; i < shapes.length; i++) { System.out.println(shapes[i].toString()); totalArea += shapes[i].area(); } System.out.println("The total area is " + totalArea);