Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that will record the votes for one of two candidates by using th

ID: 3625014 • Letter: W

Question

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)—astaticme-
thod that sets the names of the two candidates for president
• setCandidatesVicePresident(String name1, String name2)—astatic
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 using JOptionPane. 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.

Explanation / Answer

public class VoteRecorder { //static things: static String nameCandidatePresident1; //holds the name of the first candidate for president static String nameCandidatePresident2; //holds the name of the second candidate for president static String nameCandidateVicePresident1; //holds the name of the first candidate for vice president static String nameCandidateVicePresident2; //holds the name of the second candidate for vice president static int votesCandidatePresident1 = 0; //holds the number of votes for the first candidate for president static int votesCandidatePresident2 = 0; //holds the number of votes for the second candidate for president static int votesCandidateVicePresident1 = 0; //holds the number of votes for the first candidate for vice president static int votesCandidateVicePresident2 = 0; //holds the number of votes for the second candidate for vice president //instance variables: private int myVoteForPresident; //holds the vote of a single individual for president (0 for no choice, 1 for the first candidate, and 2 for the second candidate) private int myVoteForVicePresident; //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) //Constructor for an instance: increments the counters //the first field is the pres vote, the second is the vpres vote public VoteRecorder(int myVoteForPresident, int myVoteForVicePresident) { this.myVoteForPresident = myVoteForPresident; this.myVoteForVicePresident = myVoteForVicePresident; if (myVoteForPresident == 1) { votesCandidatePresident1++; } else if (myVoteForPresident == 2) { votesCandidatePresident2++; } if (myVoteForVicePresident == 1) { votesCandidateVicePresident1++; } else if (myVoteForPresident == 2) { votesCandidateVicePresident2++; } } public int getMyVoteForPresident() { return myVoteForPresident; } public int getMyVoteForVicePresident() { return myVoteForVicePresident; } //static methods public static void setCandidatesPresident(String name1, String name2) { //method that sets the names of the two candidates for president nameCandidatePresident1 = name1; nameCandidatePresident2 = name2; } public static void setCandidatesVicePresident(String name1, String name2) { //method that sets the names of the two candidates for vice president nameCandidateVicePresident1 = name1; nameCandidateVicePresident2 = name2; } public static void resetVotes() { //method that resets the vote counts to zero votesCandidatePresident1 = 0; votesCandidatePresident2 = 0; votesCandidateVicePresident1 = 0; votesCandidateVicePresident2 = 0; } public static String getCurrentVotePresident() { //method that returns a string with the current total number of votes for both presidential candidates return "Pres: " + nameCandidatePresident1 + " has " + + votesCandidatePresident1 + " votes. " + nameCandidatePresident2 + " has " + + votesCandidatePresident2 + " votes."; } public static String getCurrentVoteVicePresident() { //method that returns a string with the current total number of votes for both vic return "Vice Pres: " + nameCandidateVicePresident1 + " has " + + votesCandidateVicePresident1 + " votes. " + nameCandidateVicePresident2 + " has " + + votesCandidateVicePresident2 + " votes."; } //main method for testing public static void main(String args[]) { //set candidate names setCandidatesPresident("Annie", "Bob"); setCandidatesVicePresident("John", "Susan"); //check: System.out.println(getCurrentVotePresident()); System.out.println(getCurrentVoteVicePresident()); System.out.println(); //cast some votes and check new VoteRecorder(1,1); System.out.println(getCurrentVotePresident()); System.out.println(getCurrentVoteVicePresident()); System.out.println(); new VoteRecorder(2,2); System.out.println(getCurrentVotePresident()); System.out.println(getCurrentVoteVicePresident()); System.out.println(); new VoteRecorder(2,1); System.out.println(getCurrentVotePresident()); System.out.println(getCurrentVoteVicePresident()); System.out.println(); //reset them to test that resetVotes(); System.out.println(getCurrentVotePresident()); System.out.println(getCurrentVoteVicePresident()); System.out.println(); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote