For this assignment, you have the task of completing the Election Java class, several methods of which are "stubbed" and several parts of whose constructor are missing. An instance of this class maintains an array that stores the names of the candidates and each one's vote count. Among its methods is one, namely recordVoteFor(), that is called to record that a vote has been cast for a specified candidate.
Provided, in full, is the ElectionApp program, which creates an instance of the Election class and makes use of it to process the ballots described in an input file.
In addition to the just-mentioned "ballots" file, the ElectionApp program also makes use of two "vote-counts" files. The "before-vote-counts" file describes the prior state of the election. That is, it lists all the candidates together with each one's vote count based upon previously-processed ballots. Employing the constructor in the Election class, the ElectionApp program creates an Election object whose initial state is made to be in accord with these vote counts.
The ElectionApp program then processes the records in the "ballots" file (each of which specifies a vote for some candidate). Afterwards, it writes the updated state of the election to a second "vote-counts" file (which may then be used as input for a later run of the same program to process another bunch of ballots). (For that matter, the two vote-counts files could be one and the same file, in which case its original contents will be destroyed when the updated state of the election is written into it.)
The ElectionApp program expects to receive three command-line arguments (or what jGrasp calls run arguments), which are, respectively, the names of the input vote-counts file, the ballots-file, and the output vote-counts file, minus their assumed .txt extensions.
To illustrate, suppose that we are dealing with an election to choose the voters' favorite flavor of ice cream. In the figure below, going from left to right, are the contents of the two input files (named flavor_votes_before.txt and flavor_ballots.txt) and the output file (flavor_votes_after.txt) that results from running ElectionApp using the command
Notice that the .txt extensions are omitted in the command-line arguments (because the ElectionApp program assumes that they are absent and makes the necessary adjustments). Other details to notice about vote-counts files are that the first record indicates the number of candidates and that, if no vote counts are indicated for a given candidate, it is interpreted to mean zero.
7 butter pecan chocolate cookies and cream moose tracks neopolitan strawberry vanilla |
vanilla moose tracks chocolate vanilla strawberry chocolate butter pecan vanilla butter pecan neopolitan |
7 vanilla:3 chocolate:2 butter pecan:2 moose tracks:1 strawberry:1 neopolitan:1 cookies and cream:0 |
flavor_votes_before.txt | flavor_ballots1.txt | flavor_votes_after.txt |
---|
Now suppose that ElectionApp is run again, this time for the purpose of processing another bunch of ballots (in the file flavor_ballots2.txt) and producing an updated output file. This time the command is
and the files' contents are as follows:
7 vanilla:3 chocolate:2 butter pecan:2 moose tracks:1 strawberry:1 neopolitan:1 cookies and cream:0 |
moose tracks vanilla chocolate vanilla cookies and cream chocolate strawberry chocolate butter pecan strawberry broccoli chocolate |
7 chocolate:6 vanilla:5 butter pecan:3 strawberry:3 moose tracks:2 neopolitan:1 cookies and cream:1 |
flavor_votes_after.txt | flavor_ballots2.txt | flavor_votes_after2.txt |
---|
In addition to producing an updated vote-count file, the ElectionApp program produces a message for every ballot that it processes. It also displays a ranking of the candidates before it terminates. In gory detail, this is what that looks like for the two runs of the program described above:
$ java ElectionApp flavor_votes_before flavor_ballots1 flavor_votes_after Processing vote for vanilla; vote count: 1; rank: 1 Processing vote for moose tracks; vote count: 1; rank: 1 Processing vote for chocolate; vote count: 1; rank: 1 Processing vote for vanilla; vote count: 2; rank: 1 Processing vote for strawberry; vote count: 1; rank: 2 Processing vote for chocolate; vote count: 2; rank: 1 Processing vote for butter pecan; vote count: 1; rank: 3 Processing vote for vanilla; vote count: 3; rank: 1 Processing vote for butter pecan; vote count: 2; rank: 2 Processing vote for neopolitan; vote count: 1; rank: 4 Election standings: 1. vanilla:3 2. chocolate:2 2. butter pecan:2 4. moose tracks:1 4. strawberry:1 4. neopolitan:1 7. cookies and cream:0 |
$ java ElectionApp flavor_votes_after flavor_ballots2 flavor_votes_after2 Processing vote for moose tracks; vote count: 2; rank: 2 Processing vote for vanilla; vote count: 4; rank: 1 Processing vote for chocolate; vote count: 3; rank: 2 Processing vote for vanilla; vote count: 5; rank: 1 Processing vote for cookies and cream; vote count: 1; rank: 5 Processing vote for chocolate; vote count: 4; rank: 2 Processing vote for strawberry; vote count: 2; rank: 3 Processing vote for chocolate; vote count: 5; rank: 1 Processing vote for butter pecan; vote count: 3; rank: 3 Processing vote for strawberry; vote count: 3; rank: 3 Processing vote for broccoli: not counted (no such candidate) Processing vote for chocolate; vote count: 6; rank: 1 Election standings: 1. chocolate:6 2. vanilla:5 3. butter pecan:3 3. strawberry:3 5. moose tracks:2 6. neopolitan:1 6. cookies and cream:1 |
Because the instance variables of Candidate (name and voteCount) are declared to be public, they can be referenced directly from inside the Election class. For example, if cand were a variable of type Candidate, you can legally write cand.name and cand.voteCount and use them just like other variables. For that matter, because the elements of the candidates[] array are of type Candidate, you can legally write expressions such as candidates[k].name and candidates[k].voteCount (where k is an int variable).
Note that you can submit multiple versions of the same Java class (e.g., as you make improvements or corrections), but only the last submission will be examined by your instructor, unless you make a request to the contrary).
The source code files that are provided to you include comments describing the intended purpose/behavior of the program/class; a comment identifying the course, semester, and assignment number; a comment where you are expected to describe any deficiencies, and a comment intended to identify the program's authors (including you!) and those with whom you collaborated on this assignment.