CS 302 Lab 12

Lab 12: Designing Classes: Candidates 2

CS302, UW-Madison

Today We'll be:

Overview

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 instantiates (creates) objects of type Ballot; one Ballot instance 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).

Begin by creating a new project in Eclipse named ElectionLab. Add the Candidate class source file, Candidate.java to your project. See Candidate Javadocs to review the usage details of the class.

Next, create a class named ElectionApp. ElectionApp.java will evolve into the election application and thus will have a main method to get everything started.

Next, create a class named Ballot.java. Ballot.java will eventually represent a single ballot with multiple offices and the candidates for each office as well as the individual vote for each office.

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 creates the instances of class Ballot (one per voter), tallies votes from 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.

Task 1: Designing the Ballot Class

Each instance of 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 their own ballot [instance] on which they mark their choice of candidate for each office, much like the paper ballots commonly used in voting. Below is an example of a single Ballot instance with two offices up for election and the names of the candidates running for each particular office:  However: your first Ballot class will only need to list candiates for a single election (race).

PRESIDENT:
1. Thomas Jefferson: X
2. George Washington:
3. Ben Franklin:

SHERIFF:
1. Wyatt Earp:
2. Bill Hickok: X

Note that we have made several design decisions for the Ballot class.  Each ballot is used by one person so they can record their individual votes for different offices 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. For now, design your Ballot class (object type) by completing a class diagram like the one shown here or by sketching it out on paper in pseudocode before you enter any Java code in Eclipse. List the data fields that you will need in the top area, and list the actions (methods) that are needed in the bottom section.  Constructors should also be listed in the bottom section.  See the javadocs for the Candidate class for an example.

Ballot class diagram (empty)

Your Ballot class will not do any user I/O; this will be done in the application program in the last task. Each instance of Ballot will need to have a list of Candidates for each office on the ballot. There must be a way (method) that allows a "vote" to be cast for only one of the candidates in each election.

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:

Design Considerations:

When designing an instantiable a class to represent a particular kind of object you need to ask yourself two questions:

  1. What do you want these objects to be able to do? That is, what methods or operations are needed to make use of the object in the greater context of the program or application?
  2. What data do you need to store for each object in order to accomplish the methods you desire for your class? That is, what data is needed to represent these objects?

The Methods

The methods provide the public interface (a way to access and change) to an instance.  Decide what your objects need to be able to do?  For a ballot, you would need to have a way to display the candidates for each office and a way to record a vote for a single candidate for an office.  Once you've designed the methods of a class, you'll need to add data members (fields) to store the data needed for each method. Here are some questions you'll need to answer with your design:

The Data

Deciding what data to store is the next step in the design of a class. In general, for each method, 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):

The Constructors

Once you've designed the data part of a class, you're ready to design its constructors. Here are some questions you'll need to answer with your design:

Task 2: Implementing the Ballot Class

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.

Task 3: Designing and Implementing the VotingMachine Class

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.

Task 4: Finishing the Application that uses your Classes

Finish by replacing your code in the ElectionApp class so that it does the following:

When you're finished, have your Lab TA try your program.

Challenge Task

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.