Skip to Main Content

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:
  1. The ability to accept any number of candidates (not just 3).
  2. The votes will be supplied in comma separated files (csv) that you will need to process.

Input to Your Program

Command Line Arguments

Your program will accept the following parameters from the command line:
  1. the number of candidates for this election
  2. an arbitrary number of voting file names (this could be from zero to infinity in theory)
Here is an example of calling your program providing 7 candidates and 2 voting files.
java OddElection2 7 votefile1.txt votefile2.txt
Your program must verify that the number of candidates is a positive integer, and if this is not the case your program should print out a warning message and terminate. A utility method called isNumeric is provided in the VoteUtility class that tests whether a String is a valid int (this method returns a boolean). If your program does not receive a valid integer (> 1 and < 100) or if it receives no voting files, your program should print out a message indicating that there were problems with the parameters:
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

Develop a new class named OddElection2 that contains the methods defined in the sections below. In addition to these methods, you will need to develop JUnit tests in a file named OddElection2Test.java.

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

This section details out the methods that are required to be in your class, Election. The count0, count1, count2 parameters have been replaced by an array of arbitrary size. Candidate numbers correspond to the index of the element of the array (0, 1, ...).
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

Below are the new methods you need to implement for PA 2.
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:
  1. any vote count less than 0
  2. a zero length array
  3. if voteCounts is a null refererence
Each election should check for a valid election using this method.
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

This PA requires that you create JUnit tests to validate your methods. For testing methods processVoteFiles you will need to to send a String array to simulate the command line arguments and will need paths to voting files. Here is a sample setting of the args parameter that will work on Autolab.

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

Listed below are the files provided for this PA:
  • 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

Just like PA 1, you will need to write test cases to adequately cover your implemented solution.

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

When running votemachine2.txt with 3 candidates, your program should produce the following output (all from the supplied code in main)
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

This assignment is worth 100 points and are broken down as follows:
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
This assignment was modified from a version created by Chris Mayfield and Dee Weikle.

Back to Top