JMU logo

PA4: Oddest Election (Loops and Arrays)

constitution party

democratic party

Green party

Libertarian party

Republican

0

1

2

3

4

Image Credits: Politics1.com

Part A (30 pts): Due Sunday, Oct 23 at 11:59 PM

Submit your test cases (ArrayElectionTest.java) via  Web-CAT.

If you do not complete Part A by Tuesday 10/25, you will receive a zero for Part B.

Part B (70 pts): Due Friday, Oct 28 at 11:59 PM

Submit ArrayElection.java and ArrayElectionTest.java via Web-CAT.

You may (and should) revise your original test cases in your final submission.

Objectives

Instructions

In the previous assignment, you implemented four "odd" approaches for determining the winner of an election. However, the methods were fairly limited: they only supported three candidates. In a typical presidential election, there are dozens of third party candidates and independents, not to mention hundreds of write-ins. The basic idea of this assignment is to extend your previous code to use loops and arrays to support any number of candidates. The following source files are provided as a starting point:

As with the previous assignment, you may find it useful to reuse existing methods and/or implement additional methods to simplify your code. Feel free to add new methods to ArrayElection, but do NOT change the design of any of the provided methods.

The "count" methods have the same requirements as before: countPopular should return the candidate with the highest number of votes (i.e., return the index of the largest number in the array); countMajority requires the winning candidate to have at least 50% of all votes cast; countOdd doubles any vote counts that are odd, unless they end with a 5; and countEvenOdder doubles any vote counts that are even, unless they end with a 4, in which case it will subtract 10 instead. If there is a tie for the winner, or if any of the vote counts are negative, the result should be -1.

Since arrays are reference types, it is important that none of your methods change the contents of parameters. Doing so will corrupt data in your test methods and may cause them to fail. If you ever need to change array values, make a copy of the array first.

Random Counts

Now that your "count" methods support any number of candidates, it will be fun to test them with a variety of scenarios! You are to implement a new method named randomCounts that generates an array of vote counts, given the number of candidates, the number of voters, and a seed for the random number generator. The seed is necessary for testing with JUnit — since the numbers are only pseudorandom, we can determine exactly what array the method will return.

Here is the algorithm for generating votes: For each candidate, generate a random number between 0 and 1. Multiply this "percentage" by the number of voters, and round the result to the nearest integer. Assign this number of votes to the current candidate, and then decrease the number of voters. Do not generate a random percentage for the final candidate; they should get 100% of the remaining votes. Note that randomCounts should return null if candidates or voters is negative.

For example, consider the provided test case randomCounts(3, 100, 0) where the expected result array is {73, 6, 21}. Given the seed value of 0, Random.nextDouble() returns the values 0.730967787376657, 0.24053641567148587, and so forth. So the first candidate should get 73 of the votes (0.730967787376657 * 100), and there are 27 votes remaining. The second candidate gets 6 of the votes (0.24053641567148587 * 27), leaving the remaining 21 votes for the third and final candidate.

Test Methods

You should implement ArrayElectionTest first, before you start working on ArrayElection. Each of the following test methods should include multiple assertEquals and/or assertTrue statements. For example, you should test whether your solution works regardless of where the winning candidate is located in the array.

  1. testCountPopular - Test for popular election winners (already provided)
  2. testCountMajority - Test for winners that have and don't have a majority
  3. testCountOdd - Test for winners because of odd votes and votes that end with a 5
  4. testCountEvenOdder - Test for winners because of even votes and votes that end with a 4
  5. testRandomCounts - Test several arrays of various sizes using very different random seeds
  6. testNegative - Test sending negative array values to each of the five methods
  7. testTiesInvalid - Test for ties between winning and losing candidates

Finally, your ArrayElectionTest must compile and run against the instructor's solution, pass Checkstyle without any errors, and include proper documentation comments.

Honor Code

This assignment must be completed individually. Your submission must conform to the JMU Honor Code. Authorized help is limited to general discussion on Piazza, the lab assistants assigned to CS 139/149, and the instructor. Copying work from another student or the Internet is an honor code violation and will be grounds for a reduced or failing grade in the course.

You should make an acknowledgements statement in your program in the class JavaDoc to say that either you received no outside help with this assignment and this work is your own or what help you received - aka Prof Chao helped me with the PrintCoord method, or TA Casey helped me with the inputNumber method


Contact chaoaj[at]jmu.edu for more information about this page or Twitter: @chaoaj