(Solved Homework): 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

iWriteHomework
Order NOW for a 10% Discount
Pages (550 words)
Approximate price: -

Why Us?

Top Quality and Well-Researched Papers

All ourbpapers are written from scratch. In fact, Clients who ask for paraphrasing services are highly discouraged. We have writers ready to craft any paper from scratch and deliver quality ahead of time.

Professional and Experienced Academic Writers

Our writers keeps you posted on your papers progress - providing you with paper outline/draft. You are also at liberty to communicate directly with your writer.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time, in many cases quite ahead of time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents