1 Building a Flexible Voting System
1.1 Step 1: Record three choices per voter
1.2 Step 2: Separate the User Interface from the Voting Data
1.3 Step 3: Add Exceptions to Report Malformed Votes
1.4 Step 4: Encapsulate Recorded Votes
1.5 Step 5: Support Methods to Compute the Winner
1.6 Step 5: Final Check for Clean Code
2 Testing Expectations and What to Turn In

Homework 5: Building a Voting Machine

Due Tuesday April 21 at 11:59 pm via Turnin. myWPI questionnaire due 45 minutes later (closing at 12:45am). Once you access the questionnaire, you will have only 30 minutes to submit your answers.

This assignment gives you practice with many concepts from the course so far, including hashmaps, encapsulation, exceptions, and general OO program design.

If you are planning to go on to 3000- or 4000-level CS classes, this would be a good time to switch over to Eclipse if you’ve been using DrJava all term.

You are welcome, but not required, to use Javadoc on this assignment. Those who plan to go onto more Java programming in any context (upper level CS courses, RBE, etc) should at least use Javadoc to add purpose statements to your methods.

1 Building a Flexible Voting System

You are building software to conduct an election. In this election, voters will state their top three choices (rather than a single choice). There are many different ways to determine winners in such preference-based elections. Unfortunately, the people conducting the election haven’t yet decided how they will count the votes. Therefore, they want you to build a voting system that will record votes, but will also let them experiment with different ways to determine the winner.

This starter file gives you a very rudimentary voting program: it presents a login screen that asks a voter to enter the name to vote for, stores that choice, and counts votes for a given candidate. Your job is to

Your solution to this homework should NOT include the countVotes method from the starter file or the current votes variable of type LinkedList<String>. You will still have a votes variable (or something like it), but with a different type that can handle three ranked choices per voter.

If you want to test your Java skills, spend a little time thinking about how you might do this before reading how we break this assignment into steps below.

Warning: Do this assignment in stages, making sure that your program runs properly after each stage. If you try to make all of the changes at once, you’ll spend far longer than needed on this assignment. Build up to your solution step by step. You can get much more credit for fully working early steps than for having most of the steps only partly working.

1.1 Step 1: Record three choices per voter

Edit the starter code to store the number of first, second, and third choice votes for each candidate on a ballot. Before reading on, think about what data structure(s) you might use for this information. After you’ve come up with your proposal on paper, feel free to look at (and use, or not) our suggested data structure.

Add a processVote method that takes three strings (for the first, second, and third choices, respectively) and returns void. This method stores a single voter’s choices in your data structure.

Include test cases for your processVote method in your Examples class.

1.2 Step 2: Separate the User Interface from the Voting Data

The starter file has put the entire voting system—ballot information, votes, and input-output methods—into a single class. Separate the system into two classes: VotingMachine for the input/output portion, and ElectionData for the ballot and votes information. The VotingMachine class should have a variable that holds an object of the ElectionData class. When you are done, get rid of the original VoteBooth class (everything in it should have moved to one of the two new classes).

1.3 Step 3: Add Exceptions to Report Malformed Votes

A vote is only valid if the voter enters three different names, each of which is a candidate on the ballot. Modify your code to check these criteria, using exceptions to report problems and require the voter to vote again if their vote is not valid.

You do not need to submit test cases that demostrate that the re-prompting behavior works, but you should check it yourself.

1.4 Step 4: Encapsulate Recorded Votes

Now that you have the ability to record votes, encapsulate the data structure that you are using to capture the voting data. Leave the ballots as a LinkedList, but allow your code to support different data structures for the three-choice votes that were cast.

1.5 Step 5: Support Methods to Compute the Winner

Next, add two different methods for counting votes. Each returns the name of the winning candidate. In case of a tie, return the name of one candidate with the high score according to the counting method:

Your test cases should include at least one election in which the two scoring methods would result in different winners. You do not need to add methods to VotingMachine that report winners (since voters would not be the ones computing the election results).

1.6 Step 5: Final Check for Clean Code

Once again, check that your code is well-encapsulated and clean. Make sure you have appropriate access modifiers on all of your variables and methods.

This is your chance to show that you really get the ideas behind encapsulation and how to structure object-oriented programs properly. This assignment is as much about good software practice than it is about the functionality (which is reasonably straightforward).

2 Testing Expectations and What to Turn In

For this assignment, we will be looking for test cases on only the processVote and findWinner methods. We will be checking whether you thought of interesting election configurations to test for each method and vote-counting strategy. Think of what makes elections or cast votes different or interesting relative to these methods, and use those for testing. You shouldn’t need more than 5-8 tests per method to do a good job of this.

We will not be looking for test cases on all of your helper methods. This gives you more flexibility in the access modifiers you choose for methods (since they no longer need to be public for purposes of testing in the Examples class).

Turn in all .java files for this assignment.