Question: // ************************************************************ // Election.java // // Thisfilecontainsaprogramthattalliestheresultsof // an election. It reads in the number of votes for each of // two candidates in each
// ************************************************************ // Election.java // // Thisfilecontainsaprogramthattalliestheresultsof // an election. It reads in the number of votes for each of // two candidates in each of several precincts. It determines // thetotalnumberofvotesreceivedbyeachcandidate,the // percentofvotesreceivedbyeachcandidate,thenumberof // precinctseachcandidatecarries,andthe // maximumwinningmargininaprecinct. // ************************************************************
import java.util.Scanner; public class Election
{ public static void main (String[] args) {
Scanner scan = new Scanner(System.in); int votesForErnest; int votesForPolly; // number of votes for Polly in each precinct int votesForErnest;// 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 perPolly = 0; int perErn = 0; int perTie = 0; System.out.println (); System.out.println ("Election Day Vote Counting Program"); System.out.println (); do { System.out.println("Enter votes for polly"); votesForPolly = scan.nextInt(); System.out.println("Enter votes for ernest"); votesForErnest = scan.nextInt(); totalPolly += votesForPolly; totalErnest += votesForErnest;
if ( votesForPolly > votesForErnest ) perPolly++; else if ( votesForErnest > votesForPolly ) perErn++; else perTie++;
System.out.println("Do you want to add more precincts (enter y/n)"); response = scan.next(); } while ( response.equalsIgnoreCase("Y") ); System.out.println("Candidate\tTotal\tPercent\tPrecincts"); System.out.println("============================================="); System.out.println("Polly\t\t" + totalPolly + "\t" + (100*totalPolly/(totalPolly+totalErnest)) + "%\t" + perPolly ); System.out.println("Ernest\t\t" + totalErnest + "\t" + (100*totalErnest/(totalPolly+totalErnest)) + "%\t" + perErn); System.out.println("Precincts Tied: " + perTie ); } }
Election Results using array
The results from the mayor's race have been reported by each precinct as follows: Candidate Candidate Candidate Candidate Winner Runoff Precinct A B C D 1 192 48 206 37 2 147 90 312 21 3 186 12 121 38 4 114 21 408 39 5 267 13 382 29
Write a program using a two-dimensional array to do the following: a. Read the raw vote totals from a data file that contains one row for each precinct. b. Display the table with appropriate headings for the rows and columns. c. Compute and display the total number of votes received by each candidate and the percent of the total votes cast. d. If any one candidate received over 50% of the votes, the program should print, in the last column, a message declaring that candidate the winner. e. If no candidate received 50% of the votes, the program should print a message declaring a run-off between the two candidates receiving the highest number of votes; the two candidates should be identified by their letter names. f. For testing, run the program with the above data, and also with another data file where Candidate C receives only 108 votes in precinct 4.
Data file:
(precinct, candiadateAvotes, , candiadateBvotes, candiadateCvotes, candiadateDvotes
1,192,48,206,37 2,147 90,312,21 3,186,12,121,38 4,114,21,408,39 5,267,13,382,29
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
