This past week we once again had a chance to vote for our favorite candidates in the general election. 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 program MUST 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 theJPanel. 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 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:
*       Read in the original file line by line
*       If a line needs to be modified (ex: a vote count or voter status), do so in your program
*       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.
*       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.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

public class Assig4 extends JFrame{
    // attributes
    private Voters voters;
    private ArrayList<Ballot> ballots;
    private JButton login;
    private JButton cast;   
    private final String votersFileName = "voters.txt";
    private Voter currentVoter;
    * constructor
    * @param lines
    * @throws HeadlessException
    public Assig4(String [] lines) throws HeadlessException {
       super("E-Vote Version 1.0");
       voters = null;
       currentVoter = null;
       // lauyout
       FlowLayout fl = new FlowLayout(5, 15, 5);
       // list of panel
       ballots = new ArrayList<>();
       for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            try {            
                Ballot ballot = new Ballot(line);
            } catch (Exception ex) {            

By purchasing this solution you'll be able to access the following files:

for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats