Question

# It’s almost election day and the election officials need a program to help tally election results....

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

// 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 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 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")) {

// precinct

// updating totals

// finding winner of this precinct

// Polly won

precinctsPolly++;

// finding win margin

// Ernest won

precinctsErnest++;

// finding win margin

} else {

// tie

precinctsTie++;

winMargin = 0;

}

// updating maxWinMargin if current winMargin is bigger than current

// maximum

if (winMargin > maxWinMargin) {

maxWinMargin = winMargin;

}

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

Do you have more precincts? y/n: y

Do you have more precincts? y/n: y

Do you have more precincts? y/n: y

Do you have more precincts? y/n: y

Do you have more precincts? y/n: n

Percentage of votes for Polly: 50.46%

Percentage of votes for Ernest: 49.54%

Precincts won by Polly: 2

Precincts won by Ernest: 2

Maximum win margin in a precinct: 501

#### Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
• ### Write a Python program that creates a class which represents a candidate in an election. The...

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...

(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...

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...

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...

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...

(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:...

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...

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...

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...

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:...