CompSci 6, Spring 2007, Test 2 Problem 1. Part A: a. 5 b. count is equal to the number of items in the array in positions 0 to K whose value is less than item. Part B: a. 5 13 14 18 21 31 33 b. Each value in the array in positions 0 to K is equal to the sum of the elements in the array before the code executes, from position 0 to their position. Problem 2. public class Rotate extends Command { // code not shown public void execute (Pixmap target) { // code not shown int x_change = scale.width; // amount to rotate Dimension bounds = target.getSize(); // size of pixmap Pixmap copy = new Pixmap(target); // Keep a copy to rotate for (int x = 0; x < bounds.width; x++) { for (int y=0; y < bounds.height; y++) { target.setColor((x + x_change) % bounds.width, y, copy.getColor(x,y)); } } } } Problem 3. Part A. 1. PlayGame and CreateStartingLineup 2. getCoach, getNumGames, and getNumWins 3. myCoach, myNumGames, myNumWins, and myStarters 4. // returns name of coach public String getCoach() { return myCoach; } // sets name of coach public void setCoach(String coach) { myCoach = coach; } // returns total number of games to play public int getNumGames() { return myNumGames; } // sets number of games to play public void setNumGames(int number) { myNumGames = number; } // returns number of wins public int getNumWins() { return myNumWins; } // add a player to the starting lineup public void AddPlayerStartingLineup(String name) { myStarters.add(name); } Part B.1 // constructor public BaseballTeam(Scanner input) { super("", 0); myPlayers = new ArrayList(); if (input.hasNext()) { setCoach(input.next()); setNumGames(input.nextInt()); } while (input.hasNext()) { String name = input.next(); String position = input.next(); double avg = input.nextDouble(); myPlayers.add(new BaseballPlayer (name, position, avg)); } } Part B.2 // returns a set of all the possible positions on this team public TreeSet uniquePositions() { TreeSet startPositions = new TreeSet(); for (BaseballPlayer current: myPlayers) { startPositions.add(current.getPosition()); } return startPositions; } Part B.3 // returns an ArrayList of all the players who play this position public ArrayList possiblePlayers(String position) { ArrayList names = new ArrayList(); for (BaseballPlayer current: myPlayers) { if (current.getPosition().equals(position)) { names.add(current.getName()); } } return names; } Part B.4 public void CreateStartingLineup() { TreeSet startPos = uniquePositions(); Random randGenerator = new Random(); for (String position: startPos) { ArrayList names = possiblePlayers(position); AddPlayerStartingLineup(names.get(randGenerator.nextInt(names.size()))); } } Problem 4 public TreeSet friendsInClub(ArrayList > clubs, String name) { TreeSet names = new TreeSet(); for (TreeSet club: clubs) { if (club.contains(name)) { for (String person: club) { names.add(person); } } } names.remove(name); return names; }