Question
Java Assignment (covers Graphics, Classes, Arrays and Files) Assignment: In this exercise, we will write a program to count votes. Most polling places now use
Java Assignment (covers Graphics, Classes, Arrays and Files)
Assignment:
In this exercise, we will write a program to count votes. Most polling places now use computerized voting machines for this process. We'd like to get an idea of how a voting program might work (if your program is good, it can always be used next year!). In this program you will implement a simple yet flexible "voting machine" with the following abilities:
1) It can accept an arbitrary number of ballots, each with an arbitrary number of candidates, and allow users to vote for one candidate for each ballot.
2) It allows users to vote only if they are "registered" and allows them to vote only one time.
3) It keeps track of the number of votes for each candidate in each office in a well-organized way, by associating a file name with each ballot.
4) It updates all data files (vote count files and voter file) as soon as possible and in a way that (mostly) protects the data if the program crashes during the update.
Execution / Implementation Details:
Your voting program should have a similar look, feel and functionality to the one demonstrated in the snapshots file (linked from the GitHub assignment page). Look over this file in detail to see how your program should handle various situations. I have some additional comments in the A4snap.htm file, so read it over very carefully.
In addition to the look, feel and functionality shown in A4snap.htm, your program MUST also follow the specifications below:
- All interaction with the user should be handled graphically, using JButtons (within JPanels) and JOptionPanes. No console I/O should be done (except for debugging purposes).
- The ballots for a given election must be stored in a single text file, formatted as specified in A4snap.htm.
- The voters for a given election must be stored in a single text file, formatted as specified in A4snap.htm.
- The name of the ballot file can be arbitrary and should be read in from the command line when the program is started. For details on reading from the command line, see Section 7.12 in the Gaddis text. The idea behind this is that the voter would not actually be starting the program rather the voting coordinator / inspector would start it, knowing which file of ballots to utilize.
- Each individual ballot for a given election must be encapsulated into an object of class Ballot, which must be a subclass of class JPanel. This class must contain the data for a ballot, the buttons needed to select a candidate for that office, and the ActionListener (or listeners) necessary to respond to the candidate choices. The buttons should be laid out in a nice way within the JPanel. Note that this idea is similar to that of the Question class in Assignment 3. However, now in your Ballot class you are providing a graphical component to each ballot as well as the data. See Counters2.java for an example of encapsulating data within a JPanel.
- Since it is not known in advance how many offices there will be, your main program should have an array or ArrayList of Ballot, which will be sized using the ballots file.
- Your main window (JFrame) should show all of your Ballot objects in a transparent way. In other words, the user of the program should see a set of ballots without having any idea how they are actually being store internally. This can be done by having a layout manager for your JFrame to which you add all of your Ballot objects (which is allowed since your Ballot class is a subclass of JPanel).
- You will have to communicate between your main method / class and your Ballot class this can be done via instance methods in your Ballot class.
- A button should only be enabled when appropriate during the execution of your program. For example, a user should not be able to vote until he/she has logged in. See the setEnabled() method for JButtons.
Once a voter has logged in, voting must be done in three steps:
1) Selection of candidates from the ballots. The choices must be able to be repeatedly changed if desired.
2) Casting the ballots. This should be a button in the main window that becomes enabled once the voter has logged in. Clicking on the button attempts to cast the ballots.
3) Confirmation of the choices. This should be a JOptionPane dialog that asks the voter to confirm his/her choices. If the voter confirms, the choices are recorded. If the voter does not confirm, he/she goes back to the ballots and can once again modify them if desired. Note that for a given Ballot, it is possible that no choice is selected. This is ok and should be handled. For help with the confirmation dialog, see the API for the JOptionPane class.
After a user votes, the files (both for the offices and for the users) should be updated immediately in a "safe" way. You should do this in the following way:
1. Read in the original file line by line
2. If a line needs to be modified (ex: a vote count or voter status), do so in your program
3. Write the lines back to a new file (i.e. you are copying the file, but with the appropriate modifications). The temporary name for this new file should be something that is not likely to be a name of a regular file. For example: temptemp.txt or something similar should work.
4. When the new file is complete, delete the original file and rename the new file with the original file name
The file name for each ballot should be the id number for the ballot followed by the ".txt" extension. For example, for ballot "12345" the file should be "12345.txt". The file name for the voters should be "voters.txt".
The idea is that if something bad happens while you are writing the new file, the original file will not be affected, so only the most recent vote will be lost. For some help with this, see the API for the File class and its various methods.
SAMPLE OUTPUT SNAPSHOTS (A4SNAP.HTM):
http://people.cs.pitt.edu/%7Eramirez/cs401/assigs/assig4/A4snap.htm
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started