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.
|
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! |
Other sample data files provided to you, to aid you in testing your work, are Treasurer.txt, Dictator.txt, and Manager.txt.
|
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.)
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.)