It’s almost election day and the election officials need a program to help tally election results. There are two candidates for office—Polly Tichen and Ernest Orator. The program’s job is to take as input the number of votes each candidate received in each voting precinct and find the total number of votes for each. The program should print out the final tally for each candidate—both the total number of votes each received and the percent of votes each received. Clearly a loop is needed. Each iteration of the loop is responsible for reading in the votes from a single precinct and updating the tallies. A skeleton of the program is in the file Election.java. Open a copy of the program in your text editor and do the following. 1. Add the code to control the loop. You may use either a while loop or a do...while loop. The loop must be controlled by asking the user whether or not there are more precincts to report (that is, more precincts whose votes need to be added in). The user should answer with the character y or n though your program should also allow uppercase repsonses. The variable response (type String) has already been declared. 2. Add the code to read in the votes for each candidate and find the total votes. Note that variables have already been declared for you to use. Print out the totals and the percentages after the loop. 3. Test your program to make sure it is correctly tallying the votes and finding the percentages AND that the loop control is correct (it goes when it should and stops when it should). 4. The election officials want more information. They want to know how many precincts each candidate carried (won). Add code to compute and print this. You need three new variables: one to count the number of precincts won by Polly, one to count the number won by Ernest, and one to count the number of ties. Test your program after adding this code.
Code Skeleton:
// Election.java
// This file contains a program that tallies the results of
// an election. It reads in the number of votes for each of
// two candidates in each of several precincts. It determines
// the total number of votes received by each candidate, the
// percent of votes received by each candidate, the number of
// precincts each candidate carries, and the
// maximum winning margin in a precinct.
// ************************************************************
import java.util.Scanner;
public class Election
{
public static void main (String[] args)
{
int votesForPolly; // number of votes for Polly in each precinct
int votesForErnest; // number of votes for Ernest in each precinct
int totalPolly; // running total of votes for Polly
int totalErnest; // running total of votes for Ernest String response;
// answer (y or n) to the "more precincts" question
Scanner scan = new Scanner(System.in);
System.out.println ();
System.out.println ("Election Day Vote Counting Program");
System.out.println ();
// Initializations
// Loop to "process" the votes in each precinct
// Print out the results
}
}
Please solve this for me. I've been stuck for the last hour, much appreciated thank you!
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Election.java
import java.util.Scanner;
public class Election
{
public static void main(String[] args)
{
int votesForPolly; // number of votes for Polly in each precinct
int votesForErnest; // number of votes for Ernest in each precinct
int totalPolly; // running total of votes for Polly
int totalErnest; // running total of votes for Ernest
String response; // answer (y or n) to the "more precincts" question
int precinctsPolly; // number of precincts carried by Polly
int precinctsErnest; // number of precincts carried by Ernest
int precinctsTie; // number of precincts tied (same votes)
int maxWinMargin; // maximum winning margin in a precinct
int winMargin; // to store win margin of a precinct
double percentPolly; // percentage of votes for polly
double percentErnest; // percentage of votes for ernest
Scanner scan = new Scanner(System.in);
System.out.println();
System.out.println("Election Day Vote Counting Program");
System.out.println();
// Initializations
totalPolly = 0;
totalErnest = 0;
precinctsPolly = 0;
precinctsErnest = 0;
precinctsTie = 0;
response = "y";
maxWinMargin = 0;
// Loop to "process" the votes in each precinct
while (response.equalsIgnoreCase("y")) {
// asking and reading votes received by each candidate in current
// precinct
System.out.print("Enter votes for Polly: ");
votesForPolly = scan.nextInt();
System.out.print("Enter votes for Ernest: ");
votesForErnest = scan.nextInt();
// updating totals
totalPolly += votesForPolly;
totalErnest += votesForErnest;
// finding winner of this precinct
if (votesForPolly > votesForErnest) {
// Polly won
precinctsPolly++;
// finding win margin
winMargin = votesForPolly - votesForErnest;
} else if (votesForPolly < votesForErnest) {
// Ernest won
precinctsErnest++;
// finding win margin
winMargin = votesForErnest - votesForPolly;
} else {
// tie
precinctsTie++;
winMargin = 0;
}
// updating maxWinMargin if current winMargin is bigger than current
// maximum
if (winMargin > maxWinMargin) {
maxWinMargin = winMargin;
}
// asking user if he likes to add more precincts
System.out.print("Do you have more precincts? y/n: ");
response = scan.next();
System.out.println();
}
// Print out the results
System.out.println("Total votes for Polly: " + totalPolly);
System.out.println("Total votes for Ernest: " + totalErnest);
// finding the percentage of votes for each candidate
percentPolly = (double) totalPolly / (totalErnest + totalPolly);
percentPolly *= 100;
percentErnest = (double) totalErnest / (totalErnest + totalPolly);
percentErnest *= 100;
// displaying percentages with 2 digits precision followed by a %
// character
System.out.printf("Percentage of votes for Polly: %.2f%%\n",
percentPolly);
System.out.printf("Percentage of votes for Ernest: %.2f%%\n",
percentErnest);
System.out.println("Precincts won by Polly: " + precinctsPolly);
System.out.println("Precincts won by Ernest: " + precinctsErnest);
System.out.println("Precincts where votes tied: " + precinctsTie);
System.out.println("Maximum win margin in a precinct: " + maxWinMargin);
}
}
/*OUTPUT*/
Election Day Vote Counting Program
Enter votes for Polly: 1230
Enter votes for Ernest: 1340
Do you have more precincts? y/n: y
Enter votes for Polly: 2300
Enter votes for Ernest: 1799
Do you have more precincts? y/n: y
Enter votes for Polly: 1515
Enter votes for Ernest: 1515
Do you have more precincts? y/n: y
Enter votes for Polly: 3900
Enter votes for Ernest: 4210
Do you have more precincts? y/n: y
Enter votes for Polly: 7000
Enter votes for Ernest: 6788
Do you have more precincts? y/n: n
Total votes for Polly: 15945
Total votes for Ernest: 15652
Percentage of votes for Polly: 50.46%
Percentage of votes for Ernest: 49.54%
Precincts won by Polly: 2
Precincts won by Ernest: 2
Precincts where votes tied: 1
Maximum win margin in a precinct: 501
It’s almost election day and the election officials need a program to help tally election results....
Write a Python program that creates a class which represents a candidate in an election. The class must have the candidates first and last name, as well as the number of votes received as attributes. The class must also have a method that allows the user to set and get these attributes. Create a separate test module where instances of the class are created, and the methods are tested. Create instances of the class to represent 5 candidates in the...
(Write a program for C++ )that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate Votes Received % of Total Votes Johnson 5000 25.91...
Using C language, write a program for the following: In a student representative council election, there are ten (10) candidates. A voter is required to vote a candidate of his/her choice only once. The vote is recorded as a number from 1 to 10. The number of voters are unknown beforehand, so the votes are terminated by a value of zero (0). Votes not within and not inclusive of the numbers 1 to 10 are invalid (spoilt) votes. A file,...
COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize votes for a local election Specification: Write a program that keeps track the votes that each candidate received in a local election. The program should output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. To solve this problem, you will use one dynamic...
Write a program that supports the three phases (setup, voting and result-tallying) which sets up and executes a voting procedure as described above. In more details: You can start with the given skeleton (lab6_skeleton.cpp). There are two structures: Participant structure, which has the following members: id -- an integer variable storing a unique id of the participant (either a candidate or a voter). name -- a Cstring for storing the name of the participant.. hasVoted -- a boolean variable for...
(2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...
Please post following code with criteria below... please leave as many comments as possible Exam Four: the election Outcome: Student will demonstrate all core competencies from this class: Program Design (design tools) Variables Decision Error Checking Looping Functions Arrays Program Specifications: Let’s pretend that we are tracking votes for the next presidential election. There will be two candidates: Ivanka Trump and Michele Obama. You can assume that there are fifty states casting votes. You will do not need to deal...
To combat election fraud, your city is instituting a new voting procedure. The ballot has a letter associated with every selection a voter may make. A sample ballot is shown:- (Voter places a tick next to his or her preferred candidate, proposition and measure to indicate his/her vote) 1. Mayoral Candidates A. Pincher, Penny B. Dover, Skip C. Perman, Sue 2. PROP 17 D. YES E. NO 3. MEASURE 1 F. YES G....
Ranked choice voting is a system of tallying election ballots that is used in many national and local elections throughout the world. Instead of choosing a single candidate, voters must rank the available candidates in the order of their choice. For example, if three candidates are available, a voter might choose #2, #1, and #3 as their choices, with #2 being their first choice, #1 the second, and #3 the third. The outcome is determined by a runoff, which follows...
Create a program using the Random class and While loops in Java. The program should generate a random number from 50 through 100. The loop should add the five random numbers generated. On each iteration of the loop the program should print out the number generated, how many numbers have been generated thus far, and the sum so far. On the last iteration, the printout should say the The final total is.... Teach comment the last time I did it:...