(Solved Homework): Book:- Java An introdcution to problem solving and programming 7ed plz give the write code in Java Chapter 6:

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

 VoteRecorderDemo.java

import java.util.Scanner;

public class VoteRecorderDemo {

/*————————————————————-
| Method: [main]
|
| Purpose: [To create a demo of VoteRecorder and run a mock election to determine which candidates will win
the presidential and vice-presidential races]
|
| Pre-condition: [The VoteRecorder class must have all the necessary methods to run a mock election and
determine the winner]
|
| Post-condition: [Program will display which candidates won each election]
|
| Parameters: [N/A]
|
| Returns: [N/A]
*——————————————————————*/

public static void main(String[] args) {
VoteRecorder.setCandidatesPresident(“Annie”, “Bob”);
VoteRecorder.setCandidatesVicePresident(“John”, “Susan”);
System.out.println(“How many voters are there in this election?: “);
Scanner voters = new Scanner(System.in);
int voterCount = voters.nextInt();
while (voterCount < 0)
{
System.out.println(“Error. Please enter a positive number of voters: “);
voterCount = voters.nextInt();
}
for (int i = 0; i < voterCount; i++)
{
VoteRecorder election = new VoteRecorder();
election.getAndConfirmVotes();
System.out.println(“Thank you voter number: ” + (i + 1) + “n”);
}
VoteRecorder.displayResults();
voters.close();
}

}

VoteRecorder.java

import java.util.Scanner;

public class VoteRecorder {

// Initialize variables
static String nameCandidatePresident1;
static String nameCandidatePresident2;
static String nameCandidateVicePresident1;
static String nameCandidateVicePresident2;
static int votesCandidatePresident1;
static int votesCandidatePresident2;
static int votesCandidateVicePresident1;
static int votesCandidateVicePresident2;
private int myVoteForPresident;
private int myVoteForVicePresident;
private boolean inputYes = true;

//Constructor with votes already in
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++;
}
}

// Default constructor
public VoteRecorder()
{

}

/*————————————————————-
| Method: [setCandidatesPresident(String name1, String name2)]
|
| Purpose: [This method sets the names of the candidates for presidential race]
|
| Pre-condition: [Takes in argument String name1, name2.]
|
| Post-condition: [The names of the presidential candidates will be set.]
|
| Parameters:
|      nameCandidatePresident1 – first candidate’s name
nameCandidatePresident2 – second candidate’s name
|
| Returns: [N/A]
*——————————————————————*/

public static void setCandidatesPresident(String name1, String name2)
{
nameCandidatePresident1 = name1;
nameCandidatePresident2 = name2;
}

/*————————————————————-
| Method: [setCandidatesVicePresident(String name1, String name2)]
|
| Purpose: [This method sets the names of the candidates for vice-presidential race]
|
| Pre-condition: [Takes in argument String name1, name2.]
|
| Post-condition: [The names of the vice-presidential candidates will be set.]
|
| Parameters:
|      nameCandidateVicePresident1 – first candidate’s name
nameCandidateVicePresident2 – second candidate’s name
|
| Returns: [N/A]
*——————————————————————*/

public static void setCandidatesVicePresident(String name1, String name2)
{
nameCandidateVicePresident1 = name1;
nameCandidateVicePresident2 = name2;
}

/*————————————————————-
| Method: [resetVotes()]
|
| Purpose: [To reset the vote count of the two races back to 0]
|
| Pre-condition: [Have parameters defined]
|
| Post-condition: [The votes for every candidate will be 0]
|
| Parameters:
|      votesCandidatePresident1 – first candidate’s votes
votesCandidatePresident2 – second candidate’s votes
votesCandidateVicePresident1 – first vp candidate’s votes
votesCandidateVicePresident2 – second vp candidate’s votes
|
| Returns: [N/A]
*——————————————————————*/

public static void resetVotes()
{
votesCandidatePresident1 = 0;
votesCandidatePresident2 = 0;
votesCandidateVicePresident1 = 0;
votesCandidateVicePresident2 = 0;
}

/*————————————————————-
| Method: [getCurrentVotePresident()]
|
| Purpose: [This method gets the number of votes for the presidential race]
|
| Pre-condition: [Have parameters defined]
|
| Post-condition: [returns the vote for each presidential candidate]
|
| Parameters:
|      nameCandidatePresident1 – first candidate’s name
nameCandidatePresident2 – second candidate’s name
votesCandidatePresident1 – first candidate’s votes
votesCandidatePresident2 – second candidate’s votes
|
| Returns: [returns the vote for each presidential candidate]
*——————————————————————*/

public static String getCurrentVotePresident()
{
return “The first presidential candidate” + nameCandidatePresident1 + “has” + votesCandidatePresident1 + “votes and the second presidential canddiate” + nameCandidatePresident2 + “has” + votesCandidatePresident2 + “votes”;
}

/*————————————————————-
| Method: [getCurrentVoteVicePresident()]
|
| Purpose: [This method gets the number of votes for the vice-presidential race]
|
| Pre-condition: [Have parameters defined]
|
| Post-condition: [returns the vote for each vice-presidential candidate]
|
| Parameters:
|      nameCandidateVicePresident1 – first vp candidate’s name
nameCandidateVicePresident2 – second vp candidate’s name
votesCandidateVicePresident1 – first vp candidate’s votes
votesCandidateVicePresident2 – second vp candidate’s votes
|
| Returns: [returns the vote for each vice-presidential candidate]
*——————————————————————*/

public static String getCurrentVoteVicePresident()
{
return “The first vice-presidential candidate” + nameCandidatePresident1 + “has” + votesCandidateVicePresident1 + “votes and the second vice-presidential candiate” + nameCandidateVicePresident2 + “has” + votesCandidateVicePresident2 + “votes”;
}

/*————————————————————-
| Method: [getAVote(String name1, name2)]
|
| Purpose: [This method gets a single user’s vote for presidential election]
|
| Pre-condition: [Have parameters defined]
|
| Post-condition: [returns the vote for each vice-presidential candidate]
|
| Parameters:
|      nameCandidateVicePresident1 – first vp candidate’s name
nameCandidateVicePresident2 – second vp candidate’s name
myVoteForPresident – who user voted for
|
| Returns: [returns who the user voted for]
*——————————————————————*/

@SuppressWarnings(“resource”)
private String getAVote(String name1, String name2)
{
nameCandidatePresident1 = name1;
nameCandidatePresident2 = name2;
System.out.println(“To vote for ” + nameCandidatePresident1 + ” for President please enter 1: “);
System.out.println(“To vote for ” + nameCandidatePresident2 + ” for President please enter 2: “);
System.out.println(“To vote for neither candidate, enter 0: “);
Scanner singlePresidentVote = new Scanner(System.in);
myVoteForPresident = singlePresidentVote.nextInt();
return “You’re vote for the presidential race is in. You voted for: ” + myVoteForPresident;
}

/*————————————————————-
| Method: [getVotes()]
|
| Purpose: [This method returns a vote choice for president and vp from an individual]
|
| Pre-condition: [Have parameters defined]
|
| Post-condition: [returns the vote of user for each race]
|
| Parameters:
nameCandidatePresident1 – candidate’s name
nameCandidatePresident2 – second candidate’s name
|      nameCandidateVicePresident1 – first vp candidate’s name
nameCandidateVicePresident2 – second vp candidate’s name
myVoteForPresident – who user voted for president
myVoteForVicePresident – who user voted for vice-president
|
| Returns: [N/A]
*——————————————————————*/

@SuppressWarnings(“resource”)
private void getVotes()
{
System.out.println(“To vote for ” + nameCandidatePresident1 + ” for President please enter 1: “);
System.out.println(“To vote for ” + nameCandidatePresident2 + ” for President please enter 2: “);
System.out.println(“To vote for neither candidate, enter 0: “);
Scanner presidentVote = new Scanner(System.in);
myVoteForPresident = presidentVote.nextInt();
while (myVoteForPresident < 0 || myVoteForPresident > 2)
{
System.out.println(“Error. That is not a valid candidate. Please enter a valid number 0-2: “);
myVoteForPresident = presidentVote.nextInt();
}
System.out.println(“To vote for ” + nameCandidateVicePresident1 + ” for Vice-President please enter 1: “);
System.out.println(“To vote for ” + nameCandidateVicePresident2 + ” for Vice-President please enter 2: “);
System.out.println(“To vote for neither candidate, enter 0: “);
Scanner vicePresidentVote = new Scanner(System.in);
myVoteForVicePresident = vicePresidentVote.nextInt();
while (myVoteForVicePresident < 0 || myVoteForVicePresident > 2)
{
System.out.println(“Error. That is not a valid candidate. Please enter a valid number 0-2: “);
myVoteForVicePresident = vicePresidentVote.nextInt();
}
}

/*————————————————————-
| Method: [getAndConfirmVotes()]
|
| Purpose: [gets an individuals votes, confirms them, then records them]
|
| Pre-condition: [Have methods getVotes(), confirmVotes(), recordVotes(inputYes) defined]
|
| Post-condition: [will process a user’s vote for each race and confirm it, then record it]
|
| Parameters:
|      boolean inputYes – argument for recordVotes() method
|
| Returns: [N/A]
*——————————————————————*/

public void getAndConfirmVotes()
{

getVotes();
confirmVotes();
recordVotes(inputYes);
}

/*————————————————————-
| Method: [confirmVotes()]
|
| Purpose: [To confirm if the user wants to submit his final votes for each candidate by
typing in “yes” or “no”]
|
| Pre-condition: [User must have already input who he wants to vote for]
|
| Post-condition: [Program will confirm user’s votes and record it]
|
| Parameters:
|      boolean inputYes – argument for recordVotes() method
myVoteForPresident – who the user voted for president
myVoteForVicePresident – who the user voted for vice-president
|
| Returns: [N/A]
*——————————————————————*/

@SuppressWarnings(“resource”)
private void confirmVotes()
{
System.out.println(“Your vote for president is: ” + myVoteForPresident);
System.out.println(“Your vote for vice-president is: ” + myVoteForVicePresident);
System.out.println(“Are you happy with these choices? Type yes or no: “);
Scanner input = new Scanner(System.in);
String next = input.next();
if (next.equalsIgnoreCase(“yes”))
{
inputYes = true;
}
else
{
inputYes = false;
}
}

/*————————————————————-
| Method: [recordVotes(boolean inputYes)]
|
| Purpose: [If the user confirmed his votes by typing “yes”, then this method will record
the user’s votes based on who he voted for by entering 0, 1, or 2.]
|
| Pre-condition: [User must have already voted and confirmed his selection by typing “yes”]
|
| Post-condition: [Program will record user’s votes or say his/her votes were discarded if they did not type “yes”]
|
| Parameters:
|      boolean inputYes – argument for recordVotes() method
myVoteForPresident – who the user voted for president
myVoteForVicePresident – who the user voted for vice-president
votesCandidatePresident1 – first candidate’s votes
votesCandidatePresident2 – second candidate’s votes
votesCandidateVicePresident1 – first vp candidate’s votes
votesCandidateVicePresident2 – second vp candidate’s votes
|
| Returns: [N/A]
*——————————————————————*/

private void recordVotes(boolean inputYes)
{
int votePresident = myVoteForPresident;
int voteVicePresident = myVoteForVicePresident;
if (inputYes)
{
if (votePresident == 0)
{
votesCandidatePresident1 += 0;
votesCandidatePresident2 += 0;
}
if (votePresident == 1)
{
votesCandidatePresident1 += 1;
votesCandidatePresident2 += 0;
}
if (votePresident == 2)
{
votesCandidatePresident1 += 0;
votesCandidatePresident2 += 1;
}

if (voteVicePresident == 0)
{
votesCandidateVicePresident1 += 0;
votesCandidateVicePresident2 += 0;
}
if (voteVicePresident == 1)
{
votesCandidateVicePresident1 += 1;
votesCandidateVicePresident2 += 0;
}
if (voteVicePresident == 2)
{
votesCandidateVicePresident1 += 0;
votesCandidateVicePresident2 += 1;
}
}
else
{
System.out.println(“The votes were not counted. Please enter your votes again. n”);
getAndConfirmVotes();
}

}

/*————————————————————-
| Method: [displayResults()]
|
| Purpose: [This will display a message of who won each election based on
which candidates had the more votes]
|
| Pre-condition: [User must have voted, confirmed, and have his vote recorded.]
|
| Post-condition: [Program will display the winners of each race]
|
| Parameters:
votesCandidatePresident1 – first candidate’s votes
votesCandidatePresident2 – second candidate’s votes
votesCandidateVicePresident1 – first vp candidate’s votes
votesCandidateVicePresident2 – second vp candidate’s votes
|
| Returns: [N/A]
*——————————————————————*/

public static void displayResults()
{

if (VoteRecorder.votesCandidatePresident1 > VoteRecorder.votesCandidatePresident2)
{
System.out.println(VoteRecorder.nameCandidatePresident1 + ” has won the Presidential election!”);
}
if (VoteRecorder.votesCandidatePresident2 > VoteRecorder.votesCandidatePresident1)
{
System.out.println(VoteRecorder.nameCandidatePresident2 + ” has won the Presidential election!”);
}
if (VoteRecorder.votesCandidatePresident1 == VoteRecorder.votesCandidatePresident2)
{
System.out.println(“The presidential race has resulted in a tie!”);
}
if (VoteRecorder.votesCandidateVicePresident1 > VoteRecorder.votesCandidateVicePresident2)
{
System.out.println(VoteRecorder.nameCandidateVicePresident1 + ” has won the Vice-Presidential election!”);
}
if (VoteRecorder.votesCandidateVicePresident2 > VoteRecorder.votesCandidateVicePresident1)
{
System.out.println(VoteRecorder.nameCandidateVicePresident2 + ” has won the Vice-Presidential election!”);
}
if (VoteRecorder.votesCandidateVicePresident1 == VoteRecorder.votesCandidateVicePresident2)
{
System.out.println(“The Vice-Presidential race has resulted in a tie!”);
}
}

}

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