Test 1 CompSci 6 Fall 2009 Problem 1 A1. [2] A class represents an object in an abstract way, specifying the common attributes of all objects of a specific kind. An object is a specific instantiation of a class whose attributes have specific values. A2. [2] A constructor is a method that is called only when an object is constructed. It should initialize all instance variables to a reasonable state so that any method can be called safely. B. [5] public boolean majority(boolean a, boolean b, boolean c) { return a&b || a && c || b && c; } C. [2] Change return x to return x - 1 D. [3] "H. Brodhead, Richard" Problem 2 A. [8] int[] numbers; int max; in maxCount = 0; max = Integer.MIN_VALUE; for (int k=0; k < numbers.length; k+=1) if (numbers[k] > max) max = numbers[k]; for (int elem: numbers) if (elem == max) maxCount += 1; B1. [3] For the first loop: max is the largest value of the first k elements B2. [2] {3,8,8,2,4,4,1,7} B3. [2] {3,8,8,3,2,4,1,7} B4. [3] After each iteration of the loop, the following is true numbers[0...k-1] == unknown numbers[k-1...index-1] == either max or other numbers[index...numbers.length-1] == in final order with no maxes B5. [2] {8,8,8,3,2,4,1,7} Problem 3 Part A [5] public int weightToOz(String weight) { int pos = weight.indexOf(" "); int pos2 = weight.indexOf(" ", pos+1); int pos3 = weight.indexOf(" ", pos2+1); int lbs = Integer.parseInt(weight.substring(0,pos)); int oz = Integer.parseInt(weight.substring(pos2+1, pos3)); return 1bs * 16 + oz; } Part A Alternate Soln public int weightToOz(String weight) { int pos = weight.indexOf(" "); int posb = weight.indexOf("b"); int poso = weight.indexOf("o"); int lbs = Integer.parseInt(weight.substring(0,pos)); int oz = Integer.parseInt(weight.substring(posb+2, poso-1)); return 1bs * 16 + oz; } Part A Alternate2 Soln public int weightToOz(String weight) { // convert a String into a stream Scanner stringStream = new Scanner(weight); int lbs = Integer.parseInt(stringStream.next()); stringStream.next(); // ignore "lbs" int oz = Integer.parseInt(stringStream.next()); return 1bs * 16 + oz; } Part B [3] public String ozToWeight(int n) { int lbs = n/16; int oz = n%16; return lbs + " lb " + oz + " oz"; } Part C [7] public String[] readWClasses(Scanner input) { int number = input.nextInt(); String line = input.nextLine(); // get rest of line String [] answer = new String [number]; for (int k=0; k= w) { return ozToWeight(ounces[k]); } } return "Heavyweight"; } Problem 4 Note: Almost everyone assumed that BouncingBall's instance variables were public or protected, as we do below. import java.awt.Dimension; import java.awt.Point; import java.util.Random; public class ColorfulBall extends BouncingBall { public ColorfulBall(Point center, Point velocity) { super(center, velocity, null); Random gen = new Random(); myColor = new Color(gen.nextInt(256),gen.nextInt(256), gen.nextInt(256)); } public void move(Dimension bounds) { super.move(bounds); if (myCenter.x < RADIUS || myCenter.x > bounds.width-RADIUS || myCenter.y < RADIUS || myCenter.y > bounds.height-RADIUS) // shift colors myColor = new Color(myColor.getBlue(), myColor.getRed(), myColor.getGreen()); } }