CS 161 Lab #6

October 15th

Goals:

This week you'll extend some code I've written that's supposed to model an electronic voting system. The goal is to end up with code that allows the user to create an Election instance, add the names of the candidates, then record votes each time a 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!

Directions:

  1. Download the ElectionLab project and extract its contents, then start BlueJ and open the project.
  2. Take a moment to familiarize yourself with the Java code in the Election class. Right now, there's an array of Strings declared that will hold the names of the candidates in an election. The constructor creates a new array of the proper size, and there are methods for adding new candidates to the election, and printing the list of candidates. The vote-counting code isn't there yet, but you can still create Election instances and play with candidates:

    > 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
    
  3. Before we can write the 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 ints for this purpose. Don't forget to create this array in the constructor, in addition to the array of candidate names.
  4. Define a 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);
    
  5. Finally, add a 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.
    
If time permits, consider trying one or more of the following exercises:


Brad Richards, 2009