Comp 202 winter 2011 final exam solutions: 1)a)No, can't use "this" inside a static method b)No, because no return value in case of x == y c)Yes, compiles d)Note that intPower has a bug in it. If exponent is < 0 and base > 1, since it returns an int, it will always return 0 So the output is just 1 (gives one because of 10^0 = 1) 2)a)Does not compile because x is a pivate property b)Yes, compiles. The output is bacbc c)10 (w is a shared static property) d)3 (since doing new Beta() "resets" the amount by creating a new object 3) Note: for fall 2011, this question is not examinable 4)a)EXIT_CODE, args, fileExtension b)fd, args, fileExtension c)ex, f1, inputFile, outputFile, fileExtension d)input, output, inputFile, outputFile, fileExtension e)f1, inputFile, outputFile, fileExtension, f2, in, out, buf, len 5)The problem is that montrealCafe and moreChoices both contain a property of type ArrayList However, these 2 properties are ALIASES of each other. Thus adding to one will add to the other. The correct solution is to change the makeCopy method to do a "deep" copy and copy individual elements. This can be done either: copy.menu = new ArrayList(this.menu); OR copy.menu = new ArrayList(); /*FIXED NEXT TO LINES: WAS: this.menu.size() which is no good since this.menu does not have a SIZE method defined on it Sorry about the inconvenience*/ for (int i=0; i < this.menu.dishNames.size(); i++) { copy.menu.add(this.menu.dishNames.get(i)); } 6)On line 20, you created an array of Restaurant. However, each one is initialized to be null. To fix this , you should add before line 22: overpricedChainRestaurant[i] = new Restaurant(); 7) public static int sequence(int n) { if (n == 1) { return 1; } if (n % 2 == 0 && n > 1) { return sequence(n-1) + n; } //else return sequence(n-1) + 2*n; } 8) public class VoteMachine { private String[] names; private int[] votes; public VoteMachine(String[] names) { this.names = names; this.votes = new int[names.length]; } public void acceptVote() { bool success = false; while (!success) { for (int i=0; i < names.length; i++) { System.out.println("[" + i + "] " + names[i]); } System.out.println("Enter the number of the candidate you want to vote for"); java.util.Scanner s = new java.util.Scanner(System.in); int vote = s.nextInt(); if (vote < names.length & vote >= 0) { success = true; votes[vote] = votes[vote] + 1; } } } public int[] getVotes() { int[] copy = new int[votes.length]; for (int i=0; i < copy.length; i++) { copy[i] = votes[i]; } return copy; } } 9) import java.io.*; public class PollingPlace { private VoteMachine machine; private ArrayList eligibleVoters; private int numberVotersEligible; public PollingPlace(String repList, String departmentStudentListFile) { this.machine = new VoteMachine(repList); this.eligibleVoters = initializeVoterList(departmentStudentListFile); } //note that we didn't cover this method exactly in fall 2011 although you _should_ be able //to do it if the specification was changed to "read from the keyboard until the user types a string consisting //of just the number 1 public ArrayList initializeVoterList(String filename) { ArrayList names = new ArrayList(); Scanner reader = new Scanner(new File(filename)); while (reader.hasNextLine()) { names.add(reader.nextLine()); } return names; } public boolean handleVoter(String name) { if (this.eligibleVoters.contains(name)) { machine.acceptVote(); this.eligibleVoters.remove(name); return true; } return false; } public int[] getVotes() { return this.machine.getVotes(); } public boolean validateNoCheating() { int[] votes = getVotes(); int sum = 0; for (int i=0; i < votes.length; i++) { sum = sum + votes[i]; } return numberVotersEligible == sum + this.eligibleVoters.size(); } } 10) public class Department { ArrayList pollingPlaces; ArrayList reps; ArrayList kings; public Department(ArrayList reps, ArrayList kings, String studentListFile) { this.pollingPlaces = new ArrayList(); for (int i=0; i < 10; i++) { this.pollingPlaces.add(new PollingPlace(reps, studentListFile)); } } public int getIndexOfWinner() { //iterate over all polling places and add up the votes: int[] totalVotes = new int[reps.size()]; for (int i=0; i < pollingPlaces.size(); i++) { if (pollingPlaces.get(i).validateNoCheating()){ int[] votesAtStation = pollingPlaces.get(i).getVotes(); for (int j=0; j < votesAtStation.length; j++) { totalVotes[j] += votesAtStation[j]; } } //now choose whichever index is biggest: int highestIndex = 0; for (int i=0; i < reps.size(); i++) { if (totalVotes[i] > totalVotes[highestIndex]) { highestIndex = i; } } return i; } public String getWinningLeader() { return this.kings.get(getIndexOfWinner()); } } 11) public class MacHillStudentsVote { public static void main(String[] args) { ArrayList departments = new ArrayList(); for (int i=0; i < 308; i++) { departments.add(new Department(VoterUtility.getRepresentatives(i), VoterUtility.getAllLeaders(), VoterUtility.getStudentList(i))); } VoterUtility.executeDay(departments); int[] ridingsWon = new int[VoterUtility.getAllLeaders().size()]; for (int i=0; i < departments.size(); i++) { ridingsWon[departments.get(i).getIndexOfWinner()]++; System.out.println("Riding " + i + departments.get(i).getWinningLeader() + "'s representative wins."); } //now output the summary: for (int i=0; i < VoterUtility.getAllLeaders().size(); i++) { System.out.println(VoterUtility.getAllLeaders().get(i) + "'s representatives won " + ridingsWon[i] + " departments."); } } }