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.
// ***************************************************************
// 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
}
}
import java.util.Scanner;
public class Election {
public static void main (String[] args)
{
// Initializations
int votesForPolly = 0; // number of votes for Polly in each precinct
int votesForErnest = 0; // number of votes for Ernest in each precinct
int totalPolly = 0; // running total of votes for Polly
int totalErnest = 0; // running total of votes for Ernest
String response; // answer (y or n) to the "more precincts" question
int winsOfPOlly = 0; // number of precints won by polly
int winsofErnest = 0; // number of precints won by ernest
int numOfTies = 0; // number of ties
System.out.println ("Election Day Vote Counting Program");
Scanner scan = new Scanner(System.in);
// Loop to "process" the votes in each precinct
do{
//input : number of votes for Polly in each precint
System.out.println("Votes for Polly in this Precint: ");
votesForPolly = scan.nextInt();
//input : number of votes for Ernest in each precint
System.out.println("Votes for Ernest in this Precint: ");
votesForErnest = scan.nextInt();
totalPolly = totalPolly+votesForPolly; // adding each precint votes of Polly to get the total votes
totalErnest = totalErnest+votesForErnest; // adding each precint votes of Ernest to get total votes
// Additional information of how many polly won, ernest won and number of ties
if(votesForPolly>votesForErnest){
winsOfPOlly +=1; //each time winsOfPOlly is incremented by 1 when polly has more votes
} else if(votesForPolly<votesForErnest){
winsofErnest+=1; //each time winsOfErnest is incremented by 1 when ernest has more votes
}else{
numOfTies+=1; // incremented by 1 when votes are equal
}
//controlled by asking the user whether or not there are more precincts to report
System.out.println("Are there more precints to report(input y for yes and n for no)?");
response = scan.next();
} while(response.equalsIgnoreCase("y")); //if y then continue the loop otherwise exit the loop
// Print out the results
// Printing total number of votes of each candidate
System.out.println ("Total votes for Polly: "+totalPolly);
System.out.println ("Total votes for Ernest: "+totalErnest);
// Printing percentage of votes of each candidate
System.out.println ("Percentage of Polly votes: "+totalPolly*100/(totalPolly+totalErnest));
System.out.println("Percentage of Ernest votes: "+totalErnest*100/(totalPolly+totalErnest));
//Printing additional information of number of wins and ties
System.out.println("Precints won by Polly: "+winsOfPOlly);
System.out.println("Precints won by Ernest: "+winsofErnest);
System.out.println("Number of ties: "+numOfTies);
}
}
It's almost election day and the election officials need a program to help tally election results....
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...
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...
PYTHON (A voting machine consists of an election district, together with the names of the candidates and the number of votes cast for each candidate. Define a VotingMachine class, wrote a docstring for the class, and define the following three class methods: 1. An initialization method. The initialization method should: * take a string parameter, electionDistrict, and assign it to the instance attribute elctionDistrict of the voting machine being created. * create an instance attribute named candidates for the voting...
Write a program 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 Miller 4000 20.73 Duffy...
(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...
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...
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,...
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...
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....