In this lab, you'll build a complete voting system application using a simplified version of the Candidate class developed in lab 8 and two more instantiable classes you'll implement below. The voting system application uses a voting machine object, which makes ballot objects; one ballot for each voter. Ballot objects use candidate objects to represent information about the candidates in this election. We'll be doing the implementation in an object-oriented (OO) manner to gain more experience with OO programming concepts (even though this program could easily be written without OOP).
First, you'll design and implement a Ballot class, which uses Candidate objects. A ballot has a list of candidates running for one or more offices, and is used to record a person's vote for each office up for election. Next, you'll design and implement a VotingMachine class that makes ballots (one per voter), tallies votes on ballots, and produces a report of the votes. You'll finish by writing the application that uses these classes to make a simple voting system.
Begin by creating a new project in Eclipse named ElectionLab. Then create two new source files in your project, one named Ballot.java and another named ElectionApp.java that will evolve into the election application. Also add our Candidate class source file, Candidate.java to your project (put it in the "src" folder if you created one). The Javadoc for this class can be found here. Take a look at this doc to re-familiarize yourself with this class that you implemented in an earlier lab.
The Ballot class represents a single election ballot having a list of Candidates on it for each office that is up for election. Each voter gets a ballot on which they mark their choice of candidate for each office like the paper ballots commonly used in voting. Below is an example of a ballot with two offices up for election and the names of the candidates running for each particular office:
PRESIDENT: 1. Thomas Jefferson: X 2. George Washington: 3. Ben Franklin: SHERIFF: 1. Wyatt Earp: 2. Bill Hickok: X
Note that a ballot is used by one person so they can record their votes as indicated by the X next to the names above. Each person gets their own ballot. Later, we'll design a voting machine that uses the Ballot class to make one ballot for each voter. Design your Ballot class by sketching it out on paper in pseudocode before you enter any Java code in Eclipse. Your ballot class will not do any user I/O; this will be done in the application program in the last task. Design your ballot class with appropriate accessors and mutators so that the information about the candidates can be accessed and a vote for a particular candidate can be stored. Keep your ballot class simple by limiting it to only ONE office (e.g., just candidates running for president). The challenge task will be to modify your classes to handle elections for more than one office. Spend about 15 minutes on this task using the design considerations below as a guide.
Once you've completed your design, show it to your Lab TA before proceeding. Make sure that you've included the following items:
When designing an instantiable a class to represent a particular kind of object you need to ask yourself two questions:
Deciding what data to store is the first step in the design of a class. In general, for each data member you'll need to answer these questions:
Write pseudocode for the data members by answering the questions for each item above (write your answers on paper to develop your design):
Once you've designed the data part of a class, you'll next design its constructors and methods. Here are some questions you'll need to answer with your design:
Now incrementally code and test your Ballot class. Use your ElectionApp as the driver program (in task 4 you'll replace the driver code with code to do the application). When you believe your Ballot class is working, show your completed work to your Lab TA.
Switch so that your partner is in control of the keyboard
Now, add a new class, named VotingMachine, to your project. The VotingMachine class represents a single voting machine that makes and tallies ballots, and produces a report of its election results. As with the Ballot class, we'll limit the voting machine to handle candidates running for ONE office and this class should not do any user I/O, that will be done by the application. Come up with a design that enables your machine to:
As you develop this class it might cause you to reconsider the design of your Ballot class. If needed, you can make changes to it also so that these two class work together well. When working on the design of multiple classes making changes to classes you thought you had finished is not unusual. Once you've completed your design for your VotingMachine class, show it and any modifications to your Ballot class to your Lab TA before coding.
After your Lab TA approves your design, next code up your class. Use your ElectionApp class as a driver program to develop your code. When you're done, show your VotingMachine class to your Lab TA.
Finish by replacing your code in the ElectionApp class so that it does the following:
PRESIDENT: George Washington: 121 Thomas Jefferson: 101
When you're finished, have your Lab TA try your program.
In any remaining lab time, determine how you'd modify the classes to handle elections for more than one office. Show your Lab TA the work you're able to complete before the lab ends.