CS 149 Intro to Programming

Fall 2021

PA 1 -- 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.
  • Write JUnit tests that cover 100% of the program.
  • Submit code to Autolab and interpret feedback.

Background

Election season is upon us, and many people don't realize just how involved computers, algorithms, and network security are in this process. While we won't get into all the details here, you might check out the wikipedia article on electronic voting. Also of interest is the humanitarian open source software project Ushahidi which develops crowdsourcing tools and began in response to post-election violence in Kenya in 2008.

But on with our own election tools! The leaders involved want to compare strategies of unconventional ballet counting and have asked you to implement several ballot counting approaches as outlined below. Some of them are "fairly odd".

You will implement these 4 counting approaches in four separate methods in a class called OddElection. Each method will be passed three parameters and will return an integer corresponding to the winner of the election. You will also need to write JUnit tests that "cover" (execute) 100% of your statements. See OddElectionTest for examples.

Counting Approaches/Method Specifications

Each counting method takes 3 arguments: the count for candidate 0 (count0), the count for candidate 1 (count1), and the count for candidate 2 (count2). The behavior of each method is outlined below. Each method must return the candidate number (0, 1, or 2) of the winner (or -1 in some situations indicating no winner).
method signature method signature method description
Popular vote int countPopular(int count0, int count1, int count2) the 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 count0, int count1, int count2) 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).
Odd int countOdd(int count0, int count1, int count2) 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.
Even Odder int countEvenOdder(int count0, int count1, int count2) all candidates with an even number of votes gets their vote count doubled, unless the count end is even and ends in a 4, in which case 10 is subtracted from the vote count. 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!

Part A -- Write Test Cases

It might seem odd, but the first step in this PA is to construct JUnit tests that verify the correct operation of your (future) code. You can write test cases that exercise the instructor solution .class file.

The following source files are provided as a starting point:

DO NOT create an OddElection.java file until you have completed Part A. The methods you are testing are contained within the OddElection.class file you are downloading.

Create JUnit Test File

You may have noticed that the template OddElectionTest.java does not have a main method inside it. To run these tests without having OddElection.java, follow these steps:
  1. Create a new folder/directory for this project, call it CS149PA1
  2. Download OddElection.class and OddElectionTest.java to this new folder
  3. Within jGRASP, select the Project menu and then select new. Type OddElection as the Project nameand click next. At the next window, press the Create button. If these steps were successful, you will see a file named OddElection.gpj get created within the folder.
  4. Within jGRASP, right click on the file OddElectionTest.java and select Add to project. After you do this, you will see a [P] after the filename.
  5. Run OddElectionTest.java. When you do, it should state that the 4 jUNIT tests compelted OK. If you get a window stating No main methods then something went wrong during these steps. Please review and repeat them.

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 #1 -- Due Friday, Oct 15th at 11:00 pm

Submit OddElectionTest.java to Autolab. These test cases should achieve a 100% coverage of all methods within the instructor's solution code (provided to you as OddElection.class). Make sure you do not have a OddElection.java file at this point, since compiling this could overrwrite the instructor solution in OddElection.class.

Part B -- Write OddElection

You now need to write your own OddElection class which contains the 4 methods you tested in Part A. As you work on the 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 OddElection, but do NOT change the design of any of the provided methods. Duplicating logic/code within your methods (which are clearly performed by existing methods) will result in point deductions.

The following source file is provided as a sample program for running your methods:

 

Submission #2 -- Due Monday, Oct 18th at 11:00 pm

Submit OddElection.java to https://autolab.cs.jmu.edu.

 

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

You can run the OddElectionDriver for an example of running your program.

Grading/Rubrics

This assignment is worth 50 points and are broken down as follows:

Requirement Points
Checkstyle 5 points
testCountPopular test 5 points
testCountMajority JUnit test 10 points
testCountOdd JUnit test 10 points
testCountEvenOdder JUnit test 10 points
Popular vote method 10 points
Absolute majority 15 points
Odd 15 points
Even Odder 15 points
Instructor style points 5 points

This assignment was modified from a version created by Chris Mayfield and Dee Weikle.

Back to Top