CMPS 134 Fall 2019
Prog. Assg. #7: Election
Due: 11:59pm, Wed., Nov. 27

Specification

In this assignment, you are to complete the development of the Java class Election, whose instances are responsible for maintaining an election ballot and for tallying the votes cast in that election. Provided is a client application, ElectionClient, whose behavior is illustrated below.

Mayor.txt Data File
Mayor
Smith,Lee,Jones,Brown,Ericson
Jones
Jones
Lee
Jones
Smith
Jones
Brown
Jones
Lee
Lee
Smith
Jones
 

The file shown to the left, Mayor.txt, exemplifies the input format that the ElectionClient program "expects". Specifically, the first line stores the title of the election, and the second line contains the names of the candidates as a CSV string (i.e., a string containing "values" that are separated by commas). Each subsequent line represents a vote having been cast for the candidate named on that line. However, a blank line indicates that the election has closed, and thus any subsequent votes are not to be counted. (The file shown has no blank line until the end.)

Election Client ...
Mayor: Smith,0 Lee,0 Jones,0 Brown,0 Ericson,0
Voting Opened ..................
Mayor: Smith,0 Lee,0 Jones,0 Brown,0 Ericson,0
Mayor: Smith,0 Lee,0 Jones,1 Brown,0 Ericson,0
Mayor: Smith,0 Lee,0 Jones,2 Brown,0 Ericson,0
Mayor: Smith,0 Lee,1 Jones,2 Brown,0 Ericson,0
Mayor: Smith,0 Lee,1 Jones,3 Brown,0 Ericson,0
Mayor: Smith,1 Lee,1 Jones,3 Brown,0 Ericson,0
Mayor: Smith,1 Lee,1 Jones,4 Brown,0 Ericson,0
Mayor: Smith,1 Lee,1 Jones,4 Brown,1 Ericson,0
Mayor: Smith,1 Lee,1 Jones,5 Brown,1 Ericson,0
Mayor: Smith,1 Lee,2 Jones,5 Brown,1 Ericson,0
Mayor: Smith,1 Lee,3 Jones,5 Brown,1 Ericson,0
Mayor: Smith,2 Lee,3 Jones,5 Brown,1 Ericson,0
Mayor: Smith,2 Lee,3 Jones,6 Brown,1 Ericson,0
Voting Closed ..................
Mayor: Smith,2 Lee,3 Jones,6 Brown,1 Ericson,0
Final Results:
Mayor: Smith,2 Lee,3 Jones,6 Brown,1 Ericson,0
Done!
The ElectionClient program interprets the first run argument given to it as the name of the file from which it is to read input data. (If no run arguments are provided, the program prompts the user to enter the name of an input file.) If a second run argument is provided, it is interpreted to specify an upper limit on the number of candidates for whom votes can be cast. To the right is the output that should result from the execution of ElectionClient when given the input data in the Mayor.txt file. Notice that, in response to each vote that was cast, it displays the updated vote tallies.

Other sample data files provided to you, to aid you in testing your work, are Treasurer.txt, Dictator.txt, and Manager.txt.


Some Implementation Details

   +-----------+    +---------+
15 |    -      |    |    -    | 15
   +-----------+    +---------+
14 |    -      |    |    -    | 14
   +-----------+    +---------+
        .                .
        .                .
        .                .
   +-----------+    +---------+
 5 |    -      |    |    -    | 5
   +-----------+    +---------+
 4 | "Ericson" |    |    0    | 4
   +-----------+    +---------+
 3 | "Brown"   |    |    1    | 3
   +-----------+    +---------+
 2 | "Jones"   |    |    6    | 2
   +-----------+    +---------+
 1 | "Lee"     |    |    3    | 1
   +-----------+    +---------+
 0 | "Smith"   |    |    2    | 0
   +-----------+    +---------+
     candidate       voteCount

                +---+
  numCandidates | 5 |
                +---+
 
An Election object makes use of two arrays, candidate[] and voteCount[], to store the names of the candidates and the corresponding vote tallies, respectively. These arrays are "in parallel", meaning that the elements at corresponding locations are bound to each other in some way. Here, what that means is that, for each relevant value of i, voteCount[i] is the number of votes that have been cast for the candidate whose name is candidate[i]. As an example, the contents of these arrays at the conclusion of the mayoral election (as depicted above) might be as shown to the left. (A dash shown in an array element represents "don't care", meaning that the value occupying that array element is irrelevant.) Notice the other instance variable depicted there, numCandidates. As its name suggests, its purpose is to keep track of the number of candidates on the ballot, which tells us which segments of the two arrays contain meaningful data. It also tells us in which array location the data associated to a newly-added candidate should be stored (which is "of interest to" the addCandidate() method).

Many of the methods in the Election class have been designed to throw exceptions when called upon to do things that, due to the current state of the election, are not allowed (such as casting a vote after voting has been closed). The ElectionClient program has been correspondingly designed to catch these exceptions, report their occurrence, and then to continue executing until the end of the data file has been reached. As an example, consider the Manager.txt file and the output that the ElectionClient program should produce when processing it (assuming that a second run argument was provided indicating that the limit upon the number of candidates is five), shown below.

Manager
Groucho,Chico,Harpo,Zeppo
Chico
Chico
Harpo
Gummo
Chico
Chico
Chico
Groucho
Groucho
Groucho
Groucho
Groucho
Groucho
Groucho
PMJ
Groucho

Groucho
RWM














Election Client ...
Manager: Groucho,0 Chico,0 Harpo,0 Zeppo,0 
Voting Opened ..................
Manager: Groucho,0 Chico,0 Harpo,0 Zeppo,0
Manager: Groucho,0 Chico,1 Harpo,0 Zeppo,0
Manager: Groucho,0 Chico,2 Harpo,0 Zeppo,0
Manager: Groucho,0 Chico,2 Harpo,1 Zeppo,0
Manager: Groucho,0 Chico,2 Harpo,1 Zeppo,0 Gummo,1
Manager: Groucho,0 Chico,3 Harpo,1 Zeppo,0 Gummo,1
Manager: Groucho,0 Chico,4 Harpo,1 Zeppo,0 Gummo,1
Manager: Groucho,0 Chico,5 Harpo,1 Zeppo,0 Gummo,1
Manager: Groucho,1 Chico,5 Harpo,1 Zeppo,0 Gummo,1
Manager: Groucho,2 Chico,5 Harpo,1 Zeppo,0 Gummo,1
Manager: Groucho,3 Chico,5 Harpo,1 Zeppo,0 Gummo,1
Manager: Groucho,4 Chico,5 Harpo,1 Zeppo,0 Gummo,1
Manager: Groucho,5 Chico,5 Harpo,1 Zeppo,0 Gummo,1
Manager: Groucho,6 Chico,5 Harpo,1 Zeppo,0 Gummo,1
Manager: Groucho,7 Chico,5 Harpo,1 Zeppo,0 Gummo,1
java.lang.IllegalStateException: candidate limit reached
Continuing ...

Manager: Groucho,8 Chico,5 Harpo,1 Zeppo,0 Gummo,1
Voting Closed ..................
Manager: Groucho,8 Chico,5 Harpo,1 Zeppo,0 Gummo,1

java.lang.IllegalStateException: voting not allowed now
Continuing ...

java.lang.IllegalStateException: voting not allowed now
Continuing ...

Final Results:
Manager: Groucho,8 Chico,5 Harpo,1 Zeppo,0 Gummo,1

Done!

Notice that Gummo did not appear on the original list of four candidates, but he was added to that list as a result of receiving (apparently) a write-in vote. This illustrates the point that an Election object is capable of adding new candidates after voting has begun.

However, notice that when an attempt was made to record a vote for PMJ, who would have been the sixth candidate (exceeding the limit of five specified by the second run argument), an IllegalStateException was thrown with the error message "candidate limit reached".

Finally, the blank line near the end of the file indicates that voting has closed, which explains why each of the two "attempted" votes that come afterwards are not counted and result in exceptions being thrown. (Of course, even if voting had not yet closed, the vote for RWM would not count because the limit of five candidates had already been reached.)


Program Submission

Follow the usual procuedure to submit your work, which in this case is the revised and completed Election.java source code file.

As with all assignments, you are to revise and update the comments appearing at the beginning of the files you submit. You should add your name as another author, change the "Collaboration" statement to provide the names of any and all persons with whom you collaborated in developing the program you submit, and list any known defects. (If you did not collaborate with anyone, say so.)