Answered! Book:- Java An introdcution to problem solving and programming 7ed plz give the write code in Java Chapter 6:       Programming Projects 10:  This project is found starting on page 480…

Book:- Java An introdcution to problem solving and programming 7ed

plz give the write code in Java

Chapter 6:

Programming Projects 10:  This project is found starting on page 480

Assignment guidelines:

 

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 object for each voter.

After all the voters are done, present the resultsVoteRecorder. Create a class, VoteRecorderDemo, 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 as many voters are available, no fixed or specific number. For voting, create a new VoteRecorder object for each voter. After all the voters are done, present the results.

Note:

The assignment must have the following files:

1. class voteRecorder, in file voteRecorder.java

2. class voteRecorderDemo, in a file voteRecorderDemo.java

Expert Answer

import java.util.Scanner;

public class VoteRecorder {

private static String nameCandidatePresident1;
private static String nameCandidatePresident2;
private static String nameCandidateVicePresident1;
private static String nameCandidateVicePresident2;

private static int votesCandidatePresident1;
private static int votesCandidatePresident2;
private static int votesCandidateVicePresident1;
private static int votesCandidateVicePresident2;

int myVoteForPresident;
int myVoteForVicePresident;

/**
*
*/
public VoteRecorder() {

}

/**
* method that sets the names of the two candidates for president
*
* @param name1
* @param name2
*/
public static void setCandidatesPresident(String name1, String name2) {
nameCandidatePresident1 = name1;
nameCandidatePresident2 = name2;
}

/**
* method that sets the names of the two candidates for vice president
*
* @param name1
* @param name2
*/
public static void setCandidatesVicePresident(String name1, String name2) {
nameCandidateVicePresident1 = name1;
nameCandidateVicePresident2 = name2;
}

/**
* method that resets the vote counts to zero
*/
public static void resetVotes() {
votesCandidatePresident1 = 0;
votesCandidatePresident2 = 0;
votesCandidateVicePresident1 = 0;
votesCandidateVicePresident2 = 0;
}

/**
* method that returns a string with the current total number of votes for
* both presidential candidates
*
* @return
*/
public static String getCurrentVotePresident() {
return “Votes for ” + nameCandidatePresident1 + “: ”
+ votesCandidatePresident1 + “n” + “Votes for ”
+ nameCandidatePresident2 + “: ” + votesCandidatePresident2;
}

/**
* method that returns a string with the current total number of votes for
* both vice presidential candidates
*
* @return
*/
public static String getCurrentVoteVicePresident() {
return “Votes for ” + nameCandidateVicePresident1 + “: ”
+ votesCandidateVicePresident1 + “n” + “Votes for ”
+ nameCandidateVicePresident2 + “: ”
+ votesCandidateVicePresident2;
}

/**
* method that gets an individual’s votes, confirms them, and then records
* them
*/
public void getAndConfirmVotes() {
Scanner scanner = new Scanner(System.in);
getVotes();
if (confirmVotes() == true) {
System.out
.println(“Are there any other voters? Please enter ‘Yes’ or ‘No'”);
String i = scanner.next();
if (i.equalsIgnoreCase(“Yes”)) {
getAndConfirmVotes();
}
} else {
getAndConfirmVotes();
}

}

/**
* method that returns a vote choice for president and vice president from
* an individual
*/
private void getVotes() {
myVoteForPresident = getAVote(nameCandidatePresident1,
nameCandidatePresident2);
myVoteForVicePresident = getAVote(nameCandidateVicePresident1,
nameCandidateVicePresident2);

}

/**
* 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
*
* @return
*/
private boolean confirmVotes() {
Scanner scanner = new Scanner(System.in);
if (myVoteForPresident == 0)
System.out.println(“You did not cast a vote for President.”);
if (myVoteForPresident == 1)
System.out.println(“You voted for ” + nameCandidatePresident1
+ ” as President.”);
if (myVoteForPresident == 2)
System.out.println(“You voted for ” + nameCandidatePresident2
+ ” as President.”);
if (myVoteForVicePresident == 0)
System.out.println(“You did not cast a vote for Vice President.”);
else if (myVoteForVicePresident == 1)
System.out.println(“You voted for ” + nameCandidateVicePresident1
+ ” as Vice President.”);
else if (myVoteForVicePresident == 2)
System.out.println(“You voted for ” + nameCandidateVicePresident2
+ ” as Vice President.”);
System.out
.println(“Are you satisfied with these results? Enter ‘Yes’ or ‘No'”);
String choice = scanner.next();
recordVotes();
if (choice.equalsIgnoreCase(“Yes”))
return true;
if (choice.equalsIgnoreCase(“No”))
return false;
else
return false;
}

/**
* 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)
*
* @param name1
* @param name2
* @return
*/
private int getAVote(String name1, String name2) {
Scanner scanner = new Scanner(System.in);
System.out
.println(“Please enter your vote choice. Enter 0 for no choice. Enter 1 for ”
+ name1 + “. Enter 2 for ” + name2);
int choice = scanner.nextInt();
return choice;
}

/**
* method that will add an individual’s votes to the appropriate static
* variables object for each voter
*/
private void recordVotes() {
if (myVoteForPresident == 1)
votesCandidatePresident1++;
else
votesCandidatePresident1 = votesCandidatePresident1;
if (myVoteForPresident == 2)
votesCandidatePresident2++;
else
votesCandidatePresident2 = votesCandidatePresident2;
if (myVoteForVicePresident == 1)
votesCandidateVicePresident1++;
else
votesCandidateVicePresident1 = votesCandidateVicePresident1;
if (myVoteForVicePresident == 2)
votesCandidateVicePresident2++;
else
votesCandidateVicePresident2 = votesCandidateVicePresident2;

}
}

import java.util.Scanner;

public class VoteRecorderDemo {

/**
* @param args
*/
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
VoteRecorder voteRecorder = new VoteRecorder();
voteRecorder.setCandidatesPresident(“Annie”, “Bob”);
voteRecorder.setCandidatesVicePresident(“John”, “Susan”);
voteRecorder.getAndConfirmVotes();
System.out
.println(“Would you like to reset the votes? If so, please enter ‘Yes'”);
String i = scanner.next();
if (i.equalsIgnoreCase(“Yes”)) {
voteRecorder.resetVotes();
}
System.out.println(voteRecorder.getCurrentVotePresident());
System.out.println(voteRecorder.getCurrentVoteVicePresident());
}
}

OUTPUT:

Please enter your vote choice. Enter 0 for no choice. Enter 1 for Annie. Enter 2 for Bob
1
Please enter your vote choice. Enter 0 for no choice. Enter 1 for John. Enter 2 for Susan
2
You voted for Annie as President.
You voted for Susan as Vice President.
Are you satisfied with these results? Enter ‘Yes’ or ‘No’
Yes
Are there any other voters? Please enter ‘Yes’ or ‘No’
Yes
Please enter your vote choice. Enter 0 for no choice. Enter 1 for Annie. Enter 2 for Bob
2
Please enter your vote choice. Enter 0 for no choice. Enter 1 for John. Enter 2 for Susan
1
You voted for Bob as President.
You voted for John as Vice President.
Are you satisfied with these results? Enter ‘Yes’ or ‘No’
Yes
Are there any other voters? Please enter ‘Yes’ or ‘No’
Yes
Please enter your vote choice. Enter 0 for no choice. Enter 1 for Annie. Enter 2 for Bob
1
Please enter your vote choice. Enter 0 for no choice. Enter 1 for John. Enter 2 for Susan
2
You voted for Annie as President.
You voted for Susan as Vice President.
Are you satisfied with these results? Enter ‘Yes’ or ‘No’
Yes
Are there any other voters? Please enter ‘Yes’ or ‘No’
No
Would you like to reset the votes? If so, please enter ‘Yes’
No
Votes for Annie: 2
Votes for Bob: 1
Votes for John: 1
Votes for Susan: 2

Still stressed from student homework?
Get quality assistance from academic writers!