PA2
CS 149 Intro to Programming
Fall 2021
PA 2 -- Another Odd Election

Objectives
- Write if statements to make decisions in a program.
- Use relational and logical operators in expressions.
- Design one or more methods to simplify the code.
- Utilize arrays to store values
- Write JUnit tests that cover 100% of the program.
Background
Your implementation of OddElection (PA 1) was such a success that you are being asked to enhance the software to be more flexible. These enhancements include:- The ability to accept any number of candidates (not just 3).
- The votes will be supplied in comma separated files (csv) that you will need to process.
Input to Your Program
Command Line Arguments
- the number of candidates for this election
- an arbitrary number of voting file names (this could be from zero to infinity in theory)
java OddElection2 7 votefile1.txt votefile2.txt
invalid command line arguments
usage:java Election [nbr of candidates] [voting files...]
Voting Files
To read in a voting file, you can use the supplied VoteUtility class and the readVoteFile method. This method accepts the name of the file to read and returns an array of Strings (an entry for each line in the file). If there are problems reading in the file, a null reference will be returned.
An example of two elements of the String array returned by the readVoteFile method is shown below:
Machine A,1 | Machine B,0 | Machine A,0 |
Each line consists of the machine name that recorded the vote (which may contain spaces), followed by a comma, followed by the candidate number that was selected for this vote.
Specifications
Utility Methods
These methods are provided for you in the VoteUtility.class file (you do not need to write them, but you will need to use them).method name | method signature | method description |
---|---|---|
isNumeric | boolean isNumeric(String strNum) | returns true if strNum can be translated safely into an int, otherwise it returns false. in an invalid election (return -1 in that case). |
readVoteFile | String[] readVoteFile(String fileName) | returns an array of strings where each element of the array is a line from the file fileName. For example, if the file had 10 lines in it, the returned array will have 10 elements, where each String directly corresponds to a line in the file. |
Enhancing Methods from PA1
method name | method signature | method description |
---|---|---|
Popular vote | int countPopular(int[] voteCounts) | candidate with the highest number of votes wins. A tie results in an invalid election (return -1 in that case). |
Absolute majority | int countMajority(int[] voteCounts) | the candidate with the highest number of votes wins, but only if the number of votes is more than 50% of all votes cast in the election. If the highest number of votes is not a majority, the election is invalid (return -1 in that case). |
countOdd | int countOdd(int[] voteCounts) | all candidates with an "odd" number of votes get their vote count doubled, unless the count ends with the digit 5. Once all vote counts are manipulated, the winner is determined by popular vote. |
All of your counting methods must check the values of the parameters to make sure that they are reasonable. If any paremters are negative, your counting methods should return -1. Don't forget to test these cases too!
New Methods for PA2
method name | method signature | method description |
---|---|---|
countVotes | int[] countVotes(String[] votes, int candidateCount) | The votes parameter is the array of vote strings from the voting file. candidateCount is the number of candidates to consider. This method returns an array of ints sized according to the candidateCount parameter. Each element represents the number of votes that candidate got from totaling the votes. If a vote was cast for an invalid candidate, this method should discard that vote. |
addVotes | void addVotes(int[] sumVotes, int[] votesToAdd) | This method modifies the sumVotes array by adding the corresponding element in the votesToAdd array to sumVotes. |
processVoteFiles | int[] processVoteFiles(String[] args) | This method processes the command line arguments (args) and returns an array of voteCounts. Recall that the first element in the args array is the number of candidates and the remaining elements are file names. This method must use the readVoteFile method to read the file, countVotes to process the String representations of the votes, and addVotes to accumulate the votes from each file. Hint Recall that readVoteFile can return null if it encounters issues, and your method should handle this situation. |
isValidElection | boolean isValidElection(int[] voteCounts) | In PA1, an invalidElection existed when any of the votes counters were less than 0. For this PA, the following criterion define an invalid election:
|
main | void main(String[] args) | This method is provided for you, so, DO NOT MODIFY IT. The processVoteFiles method is invoked to obtain the array of votes from the files. It then proceeds to run the each of the elections and prints out the results. |
Testing your Methods
String[] args = {"2", "data/votemachine1.txt"}
You can enable this same call to work on your local machine (or a lab machine) by placing the example files votemachine1.txt in the data directory in your pa folder.
To receive credit for this portion of the PA, you will need to have your tests achieve a 75% coverage of your code.
Provided Files
- OddElectionPA2.java A template file for all methods (including the provided main method).
- VoteUtility.class The VoteUtility class which includes the readVoteFile and isNumeric method.
- votemachine1.txt A sample vote file for testing. To run this vote file, use the following command. If you are typing this into jGrasp, you can skip the "java OddElectionPA2" portion.
java OddElectionPA2 2 votemachine1.txt
- votemachine2.txt A sample vote file for testing. To run this vote file, use the following command. If you are typing this into jGrasp, you can skip the "java OddElectionPA2" portion.
java OddElectionPA2 2 votemachine2.txt
Write JUnit Cases
Recall that the counting methods check the values of the parameters to make sure that they are reasonable. If any paremters are negative, your counting methods should return -1. Don't forget to implement code for these cases.
Submission -- See Canvas for your class due date
Submit a zip file named OddElectionPA2.zip to https://autolab.cs.jmu.edu. This file must contain both OddElectionPA2.java and OddElectionPA2Test.java. Your test cases must achieve an 80% coverage of all methods within your solution.
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 CS149, 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. If you do receive help from a TA or the instructor, make sure you acknowledge that help in your Javadoc comments.
Example Output
java OddElectionPA2 3 votemachine2.txt
The winner of the popular vote is: 1
The winner of the absolute majority vote is: -1
The winner of the count odd vote is: 0
When running the program for 2 candidates, the expected output becomes:
java OddElectionPA2 2 votemachine2.txt
The winner of the popular vote is: 1
The winner of the absolute majority vote is: 1
The winner of the count odd vote is: 0
Grading/Rubrics
Requirement | Points |
---|---|
Checkstyle | 5 points |
testCountPopular test | 10 points |
testCountMajority JUnit test | 5 points |
testCountOdd JUnit test | 5 points |
Popular vote method | 10 points |
Absolute majority | 10 points |
Odd | 10 points |
countVotes | 10 points |
addVotes | 10 points |
processVoteFiles | 10 points |
isValidElection | 10 points |
Instructor style points | 5 points |