CompSci 6 Spring 2006 Test 2 Solutions Problem 1 Part A a. 5 6 24 9 8 28 b. When an item is removed from the array, the elements shift down, but the counter k is moved forward by one. Each time an item is removed from the array the adjacent item one higher is shifted to its spot and not considered. Problem 1 Part B The elements in "values" from positions 0 to k-1 have been copied into the array temp if they are even. Problem 2 Part A int NumberOccurrences(String item, ArrayList sets) { int count = 0; for (TreeSet current: sets) { if (current.contains(item)) count++; } return count; } OR int NumberOccurrences(String item, ArrayList sets) { int count = 0; for (int k=0; k < sets.size(); k++) { if (sets.get(i).contains(item)) count++; } return count; } Problem 2 Part B public ArrayList inTooManyClubs(ArrayList membersets) { ArrayList nameList = new ArrayList(); TreeSet nameSet = new TreeSet(); for (int k=0; k current = membersets.get(k); for (String name : current) { if (NumberOccurrences(name, membersets) > 3) { nameSet.add(name); } } } for (String name : nameSet) { nameList.add(name); } return nameList; } Problem 3 public class BarsOn extends Command { public BarsOn () { super("BarsOn"); } public void execute (Pixmap target) { Dimension bounds = target.getSize(); for (int x = 0; x < bounds.width - 10; x = x + 40 ) { for (int y = 0; y < bounds.height; y++ ) { for (int k = 0; k<10 && x+k(); myAnswers = new ArrayList(); try { File data = new File(filename); Scanner input = new Scanner(data); while (input.hasNext()) // assumes each question has an answer { myQuestions.add(input.nextLine()); myAnswers.add(input.nextLine()); } } catch (IOException e) { System.out.println("Error reading file " + filename); System.out.println(e); } } Problem 4 Part C.2 public void createQuestion() { int num = myGenerator.nextInt(myQuestions.size()); setQuestion(myQuestions.get(num)); setCorrectAnswer(myAnswers.get(num)); }