Write a program that will record the votes for one of two candidates by using the class VoteRecorder, which you will design and create. VoteRecorder will have static variables to keep track of the total votes for candidates and instance variables to keep track of the votes made by a single person. It will have the following attributes:
• nameCandidatePresident1—a static string that holds the name of the first
candidate for president
• nameCandidatePresident2—a static string that holds the name of the second candidate for president
• nameCandidateVicePresident1—a static string that holds the name of the
first candidate for vice president
• nameCandidateVicePresident2—a static string that holds the name of the
second candidate for vice president
• votesCandidatePresident1—a static integer that holds the number of
votes for the first candidate for president
• votesCandidatePresident2—a static integer that holds the number of
votes for the second candidate for president
• votesCandidateVicePresident1—a static integer that holds the number
of votes for the first candidate for vice president
• votesCandidateVicePresident2—a static integer that holds the number
of votes for the second candidate for vice president
• myVoteForPresident—an integer that holds the vote of a single individual
for president (0 for no choice, 1 for the first candidate, and 2 for the second candidate)
• myVoteForVicePresident—an integer that holds the vote of a single individual for vice president (0 for no choice, 1 for the first candidate, and 2 for the second candidate)
In addition to appropriate constructors, VoteRecorder has the following methods:
• setCandidatesPresident(String name1, String name2)—a static
method that sets the names of the two candidates for president
• setCandidatesVicePresident(String name1, String name2)—a
static method that sets the names of the two candidates for vice president
• resetVotes—a static method that resets the vote counts to zero
• getCurrentVotePresident—a static method that returns a string with the
current total number of votes for both presidential candidates
• getCurrentVoteVicePresident—a static method that returns a string
with the current total number of votes for both vice presidential candidates
• getAndConfirmVotes—a nonstatic method that gets an individual’s votes,
confirms them, and then records them.
• getAVote(String name1, String name2)—a private method that returns
a vote choice for a single race from an individual (0 for no choice, 1 for the first candidate, and 2 for the second candidate)
• getVotes—a private method that returns a vote choice for president and vice president from an individual
• confirmVotes—a private method that displays a person’s vote for president and vice president, asks whether the voter is happy with these choices, and returns true or false according to a yes or no response
• recordVotes—a private method that will add an individual’s votes to the appropriate static variables
Create a program that will conduct an election. The candidates for president are Annie and Bob. The candidates for vice president are John and Susan. Use a loop to record the votes of many voters. Create a new VoteRecorder object for each voter. After all the voters are done, present the results.
This project demonstrates how static variables/methods can be used to record common information for a class of objects. In this case, we can have many instances of the vote recorder class getting votes and keep a cumulative total in static variables. While the description looks complicated, many of the methods are similar in nature.
See the code in VoteRecorder.java.
You might also like to view...
As little as _____ volts of static electricity can damage an electronic component
Fill in the blank(s) with correct word
You can turn on the ____________________ modifier in order to draw a perfect circle with the Oval tool.
Fill in the blank(s) with the appropriate word(s).