CS302, UW-Madison
Each student was asked to complete this survey via Piazza on Monday. If you or your lab teammate have not yet completed the survey, please complete it now before beginning work on this week's lab: https://docs.google.com/forms/d/1-tiJWrWeyqxzKMqK9X8FHicETAD9miM5zWqemyWmwUA/viewform?usp=send_form
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 ElectedOffice objects; one ElectedOffice for each office up for re-election. Each ElectedOffice object is used to keep a list of Candidates for that office. 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 elected office, 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 three new source files in your project, one named ElectedOffice.java, another 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 ElectedOffice class represents a single election having the name of the office being elected as well as a list of Candidates and the voting instructions for that office.
Design and implement the ElectedOffice
class. Also, override the public String toString()
method so that it returns a single String
that when diplayed (printed to the console) looks like the listing shown here:
PRESIDENT (Vote for 1): ___ Thomas Jefferson ___ George Washington ___ Ben Franklin
Design your ElectedOffice class with appropriate accessors so that the information about the office and the list of candidates can be accessed. To start with, design a single constructor that accepts the office name, a list of Candidates, and the number of candidates each voter is allowed to vote for. Spend about 5 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 field (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. Here are some questions you'll need to answer with your design:
Once you've designed the fields and constructors of a class, you'll next design its methods. Here are some questions you'll need to answer with your design:
The Ballot class represents an election ballot having multiple elected offices (and the Candidates) for each office. Each voter would get 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 three offices up for election and the names of the candidates running for each particular office:
PRESIDENT (Vote for 1): ___ Thomas Jefferson ___ George Washington ___ Ben Franklin SHERIFF (Vote for 1): ___ Wyatt Earp ___ Bill Hickok SCHOOL BOARD (Vote for 2): ___ Spock ___ Mary Poppins ___ Walt Disney ___ Steven Spielberg
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 can 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. It is the application program that will determine if the application is a console-based or GUI application.
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. To start with, keep your ballot class
simple by limiting it to at most three elected offices. The challenge task will be to modify your classes to handle
elections with more than three elected offices. 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:
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. 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:
ElectedOffice
objects (at least one any way)ElectedOffice
objects (and their Candidates)When the poll worker ends the voting, display the candidates and their
votes (winning candidate first) as shown below:
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.