// election import java.util.*; public class ElectionStyle { public static final int numVoters = 1013; public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("This program reads in data for two candidates"); System.out.println("and computes the result of an election"); System.out.println(); int x = 0; int y = 0; x = getVotes("Pizza Party"); y = getVotes("Pajama Party"); printResults(x, y); reportTurnout(x + y); } // Reads in user input and returns the total votes for one candidate. public static int getVotes(String party) { Scanner console = new Scanner(System.in); System.out.print("Enter " + party); System.out.println(" candidate's totals:"); System.out.print("How many precincts? "); int n = console.nextInt(); int totalVotes = 0; for (int i = 0; i < n; i++) { printVotePrompt(); int votes = console.nextInt(); totalVotes += votes; } System.out.println(); return totalVotes; } public static void printVotePrompt() { System.out.print(" Votes in next precinct? "); } // Computes and prints the final results of the election. public static void printResults(int firstVotes, int secondVotes) { int totalVotes = firstVotes + secondVotes; double pct1 = getPercentage(firstVotes, totalVotes); double pct2 = getPercentage(secondVotes, totalVotes); System.out.println("Pizza Party received " + Math.round(pct1 * 100.0) / 100.0 + "% of the votes cast"); System.out.println("Pajama Party received " + Math.round(pct2 * 100.0) / 100.0 + "% of the votes cast"); if (pct1 > pct2) { System.out.println("Pizza Party wins!"); } else if (pct2 > pct1) { System.out.println("Pajama Party wins!"); } else if (pct1 == pct2) { System.out.println("It's a tie!"); } } public static void reportTurnout(int totalVotes) { double turnoutPct = getPercentage(totalVotes, numVoters); System.out.println("Turnout was " + round2(turnoutPct) + "%"); } public static double getPercentage(int myVotes, int totalVotes) { return 100.0 * myVotes / totalVotes; } public static double round2(double num) { return Math.round(num * 100.0) / 100.0; } }