CompSci 6 Test 2 Spring 2008 Solution 1. Part A a. 60 b. c is the sum of those numbers from values[0] through values[k-1] that are divisible by 5. PART B a. 36 b c is the sum of the length of words in values[0] through values[k-1] 2. public class Window extends Command { // code not shown that is not needed public void execute (Pixmap target) { // loop over each color in pixmap Dimension bounds = target.getSize(); for (int x = bounds.width/2 - 10; x< bounds.width/2 + 10; x++) { for (int y = 0; y < bounds.height; y++) { Color old = target.getColor(x, y); target.setColor(x, y, new Color(0,0,0)); } } for (int y = bounds.height/2 - 10; y < bounds.height/2 + 10; y++) { for (int x = 0; x < bounds.width; x++) { Color old = target.getColor(x, y); target.setColor(x, y, new Color(0,0,0)); } } } } 3. // Given an ArrayList of Sets of animal types - return the number of // unique animals over all the sets. public static int NumberUniqueTypes (ArrayList > zoolists) { TreeSet allAnimals = new TreeSet (); for (TreeSet zoo: zoolists) { for (String critter: zoo) { allAnimals.add(critter); } } return allAnimals.size(); } 4. PART A public class Member { private String myName; // name of member private String myCarType; // type of car member has // constructor public Member (String name, String car) { myName = name; myCarType = car; } // return name of member public String getName() { return myName; } // return type of car member has public String getCarType() { return myCarType; } // set the car type to car public void setCarType(String car) { myCarType = car; } } PART B1 1. AddMembers 2. numberOfMembers() allCarTypes() getCandidates() 3. myHouseName myMembers myCandidates 4. // Constructor public HouseGroup(String houseName) { myHouseName = houseName; myMembers = new ArrayList(); myCandidates = new ArrayList(); } // returns number of members in house group public int numberOfMembers() { return myMembers.size(); } // add one member to the house group public void addOneMember(Member someone) { myMembers.add(someone); } PART B2 // read in current Members info public void setupCurrentMembers(Scanner input) { while (input.hasNext()) { String car = input.next(); String name = input.nextLine(); myMembers.add(new Member(name,car)); } } PART B3 // returns set of all car types for current Members public TreeSet allCarTypes() { TreeSet cars = new TreeSet(); for (Member temp: myMembers) { cars.add(temp.getCarType()); } return cars; } Part C // Adds "number" members based on its algorithm for picking // members, then returns an ArrayList of Members from candidate that // were not picked. if number is smaller then the possible candidates, then // just add in all candidates. public void AddMembers(int number) { // list of possible candidates ArrayList candidates = getCandidates(); // list of possible candidates // set of car types of current members TreeSet carPicked = allCarTypes(); ArrayList notPicked = new ArrayList(); ArrayList carPossible = new ArrayList(); Random rand = new Random(); for (Member temp: candidates) { if (carPicked.contains(temp.getCarType())) { notPicked.add(temp); } else { carPossible.add(temp); } } while (number > 0 && carPossible.size() > 0) { int pos = rand.nextInt(carPossible.size()); addOneMember(carPossible.get(pos)); carPossible.remove(pos); number--; } while (number > 0 && notPicked.size() > 0) { int pos = rand.nextInt(notPicked.size()); addOneMember(notPicked.get(pos)); notPicked.remove(pos); number--; } // reset candidates to those not picked for (Member temp: carPossible) { notPicked.add(temp); } resetCandidates(notPicked); }