CompSci 6 Test 2 Spring 2010 Solutions Problem 1 1. output is: Chi La Re Me Rod 2. Those strings in names in positions 0 to k-1 have been modified by removing the first word and blank. If there is no blank in the String, then it is unchanged. 3. output is: Duke Problem 2 1. Hayward working out ... resting ... Hayward 12 2. NCAA champ: Zoubek super workout ... resting ... Great Game Zoob NCAA champ: Zoubek 23 3. Error on the 5th line. jon is a variable of type BasketballPlayer and a BasketballPlayer does not know about the praise method. 4. Error on the first line. A BasketballPlayer object cannot be assigned to a variable of type DukeBasketballPlayer. A DukeBasketballPlayer "is a" BasketballPlayer from inheritance but a BasketballPlayer IS NOT a DukeBasketballPlayer. 5. Jukes NCAA champ: Plumlee Problem 3 public TreeSet uniqueNames(ArrayList people, String year) { TreeSet temp = new TreeSet(); for (Student stud: people ) { if (stud.getYear().equals(year)) { temp.add(stud.getName()); } } return temp; } } Problem 4 public class Reflect extends Command { public Reflect () { super("Reflect"); } public void execute (Pixmap target) { Dimension bounds = target.getSize(); Random rand = new Random(); int ystart = rand.nextInt(bounds.height/2) + bounds.height/2; for (int x = 0; x < bounds.width; x++) { for (int y = ystart+1; y < bounds.height; y++) { int newy = ystart-(y-ystart); // Alternative: int newy = 2 * ystart -y; Color newColor = target.getColor(x, newy); target.setColor(x, y, newColor); } } } } Problem 5 PART A Charity is Nasher: Mel Gates $2150.0 Jo Tan $500.0 Bob Jo Yu $300.0 PART B // given a name, return the amount they donated public double totalAmountFromDonor(String donorName) { double total = 0.0; for (Donor don: myDonors) { if (don.getName().equals(donorName)) { return don.getDonations(); } } return total; } PART C // return the name of the donor who gave the most money to charities public String DonorWithLargestDonations() { String largestDonor = ""; ArrayList allDonors = getAllDonors(); double maxDonation = 0; for (String str: allDonors) { double amount = totalDonations(str); if (amount > maxDonation) { maxDonation = amount; largestDonor = str; } } return largestDonor; }