Add the following methods to the class provided: *getAndConfirmVotes - a non-sta
ID: 3634595 • Letter: A
Question
Add the following methods to the class provided:*getAndConfirmVotes - a non-static method that gets an individuals 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 persons 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 individuals votes to the appropriate static variables
*Then finally create a MainClass 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 Vote Recorder object for each voter. After all the voters are done, present the results.
------------------------------------------------------------------------------------------------------
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.";
}
}
Explanation / Answer
1. package voterecorder; 2. import java.util.Scanner; 3. 4. public class VoteRecorder { 5. 6. private static String nameCandidatePresident1; 7. private static String nameCandidatePresident2; 8. private static String nameCandidateVicePresident1; 9. private static String nameCandidateVicePresident2; 10. private static int votesCandidatePresident1 = 0; 11. private static int votesCandidatePresident2 = 0; 12. private static int votesCandidateVicePresident1 = 0; 13. private static int votesCandidateVicePresident2 = 0; 14. private int myVoteForPresident; 15. private int myVoteForVicePresident; 16. 17. Scanner inputDevice = new Scanner(System.in); 18. 19. public VoteRecorder() 20. { 21. myVoteForPresident = 0; 22. myVoteForVicePresident = 0; 23. } 24. 25. public static void setCandidatesPresident(String name1, String name2) 26. { 27. nameCandidatePresident1 = name1; 28. nameCandidatePresident2 = name2; 29. } 30. 31. public static void setCandidatesVicePresident(String name1, String name2) 32. { 33. nameCandidateVicePresident1 = name1; 34. nameCandidateVicePresident2 = name2; 35. } 36. 37. public static void resetVotes() 38. { 39. votesCandidatePresident1 = 0; 40. votesCandidatePresident2 = 0; 41. votesCandidateVicePresident1 = 0; 42. votesCandidateVicePresident2 = 0; 43. } 44. 45. public static String getCurrentVotePresident() 46. { 47. return nameCandidatePresident1 + ": " + votesCandidatePresident1 + " " + nameCandidatePresident2 + ": " + votesCandidatePresident2; 48. } 49. 50. public static String getCurrentVoteVicePresident() 51. { 52. return nameCandidateVicePresident1 + ": " + votesCandidateVicePresident2 + " " + nameCandidateVicePresident2 + ": " + votesCandidateVicePresident2; 53. } 54. 55. public void getAndConfirmVotes() 56. { 57. //A non-static method that gets an individual's votes, confirms them, then records them. 58. 59. while (true) 60. { 61. System.out.println("The candidates for president are: " + nameCandidatePresident1 + " and " + nameCandidatePresident2 + "."); 62. System.out.print("Enter 1 to vote for " + nameCandidatePresident1 + ", 2 to vote for " + nameCandidatePresident2 + ", or 0 for no choice: "); 63. switch (inputDevice.nextInt()) 64. { 65. case 1: myVoteForPresident = 1; 66. break; 67. case 2: myVoteForPresident = 2; 68. break; 69. case 0: myVoteForPresident = 0; 70. break; 71. default: myVoteForPresident = 0; 72. break; 73. } 74. 75. System.out.println("The candidates for vice president are: " + nameCandidateVicePresident1 + " and " + nameCandidateVicePresident2 + "."); 76. System.out.print("Enter 1 to vote for " + nameCandidateVicePresident1 + ", 2 to vote for " + nameCandidateVicePresident2 + ", or 0 for no choice: "); 77. switch (inputDevice.nextInt()) 78. { 79. case 1: myVoteForVicePresident = 1; 80. break; 81. case 2: myVoteForVicePresident = 2; 82. break; 83. case 0: myVoteForVicePresident = 0; 84. break; 85. default: myVoteForVicePresident = 0; 86. break; 87. } 88. 89. if (this.confirmVotes() == true) 90. break; 91. } 92. 93. if (myVoteForPresident == 1) 94. votesCandidatePresident1 ++; 95. else if (myVoteForPresident == 2) 96. votesCandidatePresident2 ++; 97. 98. if (myVoteForVicePresident == 1) 99. votesCandidateVicePresident1 ++; 100. else if (myVoteForVicePresident == 2) 101. votesCandidateVicePresident2 ++; 102. 103. System.out.println("Your votes have been recorded."); 104. } 105. 106. private int getAVote(String name1, String name2) 107. { 108. //A private method that returns a vote choice for a single race from an individual. 109. if (name1.equals(nameCandidatePresident1) && name2.equals(nameCandidatePresident2)) 110. return myVoteForPresident; 111. else if (name1.equals(nameCandidateVicePresident1) && name2.equals(nameCandidateVicePresident2)) 112. return myVoteForVicePresident; 113. else 114. return -1; 115. } 116. 117. private String getVotes() 118. { 119. //A private method that returns a vote choice for president and vice president from an individual. 120. String president = "", vicePresident = ""; 121. if (this.getAVote(nameCandidatePresident1, nameCandidatePresident2) == 0) 122. president = "no vote"; 123. else if (this.getAVote(nameCandidatePresident1, nameCandidatePresident2) == 1) 124. president = nameCandidatePresident1; 125. else if (this.getAVote(nameCandidatePresident1, nameCandidatePresident2) == 2) 126. president = nameCandidatePresident2; 127. 128. if (this.getAVote(nameCandidateVicePresident1, nameCandidateVicePresident2) == 0) 129. vicePresident = "no vote"; 130. else if (this.getAVote(nameCandidateVicePresident1, nameCandidateVicePresident2) == 1) 131. vicePresident = nameCandidateVicePresident1; 132. else if (this.getAVote(nameCandidateVicePresident1, nameCandidateVicePresident2) == 2) 133. vicePresident = nameCandidateVicePresident2; 134. 135. return president + " for president and " + vicePresident + " for vice president."; 136. } 137. 138. private boolean confirmVotes() 139. { 140. //A private method that displays a person's votes, asks whether the voter is happy with these choices, 141. //and returns true or false according to yes or no response. 142. System.out.println("Your current votes: " + this.getVotes()); 143. System.out.print("Are you happy with these votes? yes/no: "); 144. if (inputDevice.nextLine().trim().equalsIgnoreCase("yes")) 145. return true; 146. else 147. return false; 148. } 149. } 150. 151.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.