vote()
method is invoked. You'll also add a method that looks through all of the vote totals and declares a winner. Of course, this is all just an excuse to get some practice declaring and using arrays, and getting used to their zero-based index values! (You can see examples of the array-based code we wrote in lecture in the Die class.)
Consider letting the less experienced member of the group do the typing if it seems like there's a mismatch in experience or comfort levels. That will help make sure they don't get left behind. Worst case, flip a coin to see who does the typing. Or have Java generate a random boolean value. (Type "(new java.util.Random()).nextBoolean()
" in the codepad without the double quotes.)
At the conclusion of the lab, make arrangements for the typer to share a copy of the code with the other member(s) of the group if desired. (E.g. email it, or put it on a shared Google drive, etc.) My solutions to the lab will get posted as well.
> Election e = new Election(4); > e.printCandidates(); 1) null 2) null 3) null 4) null > e.addCandidate("Alice", 1); > e.addCandidate("Brad", 2); > e.addCandidate("Catherine", 3); > e.addCandidate("Doug", 4); > e.printCandidates(); 1) Alice 2) Brad 3) Catherine 4) Doug
vote
method, we need a place to keep the vote totals for each candidate. Add a new field to the class that will hold an array of integers for this purpose. Don't forget to create this array in the constructor, in addition to the array of candidate names.
vote
method that takes a 1-based candidate index (just like addCandidate
does), and increments the vote count for the corresponding candidate. You should ignore the vote if the candidate index isn't valid. You can test your vote method by invoking the methods below, then using the object inspector to view the contents of your vote count array. ("Brad" should've won, of course.)
> Election e = new Election(4); > e.addCandidate("Alice", 1); > e.addCandidate("Brad", 2); > e.addCandidate("Catherine", 3); > e.addCandidate("Doug", 4); > e.printCandidates(); 1) Alice 2) Brad 3) Catherine 4) Doug > e.vote(1); > e.vote(2); > e.vote(2); > e.vote(3); > e.vote(2);
printResults
method that looks through the vote counts, and declares a winner. The easiest way to do this is keep track of the index of the largest total you've encountered as you work through the array. If you find a larger total, update the index. When you're all done with the loop, you can use the index to get both the name of the winning candidate, and the number of votes they received:
> Election e = new Election(4); > e.addCandidate("Alice", 1); > e.addCandidate("Brad", 2); > e.addCandidate("Catherine", 3); > e.addCandidate("Doug", 4); > e.printCandidates(); 1) Alice 2) Brad 3) Catherine 4) Doug > e.vote(1); > e.vote(2); > e.vote(2); > e.vote(3); > e.vote(2); > e.printResults(); The winner is candidate 2, Brad, with 3 votes.
addCandidate
so that it prints an error message if it's called after voting has started.) The interactions below show that addCandidate
worked normally until vote
was called, but the attempt to replace the candidate "Brad" with "Bart" failed after the first vote:
> Election e = new Election(4); > e.addCandidate("Alice", 1); > e.addCandidate("Brad", 2); > e.addCandidate("Catherine", 3); > e.addCandidate("Doug", 4); > e.printCandidates(); 1) Alice 2) Brad 3) Catherine 4) Doug > e.vote(1); > e.addCandidate("Bart", 2); Hey -- you can't change candidates after voting starts! > e.vote(2);
printResults
so that it finds the second-place candidate in addition to the winner. You can do this in one pass, though it gets a little tricky. It's easier (though less efficient) to find the winner and print them as usual, then set the winner's count to zero and make a second pass through the results to find the "winner" again — which will be the second-place candidate now.