// A program to read in the number of votes each of two candidates received // in a number of precincts, then compute the winner of the election. // // This version of the program demonstrates good information flow by making // proper use of parameters and return values. Notice how main is a concise // summary of the program and major methods return their results to main. // // Notice also the factoring of common code out of the branches of the if/else // statement in printResults. import java.util.*; public class ElectionBuggy { public static final int NUM_VOTERS = 1013; public static void main(String[] args) { Scanner console = new Scanner(System.in); intro(); int votes1 = 0; int votes2 = 0; getVotes("Pizza Party"); getVotes("Pajama Party"); printResults(votes1, votes2); reportTurnout(votes1 + votes2); } // Prints out an introduction to the program. public static void intro() { System.out.println("This program reads in data for two candidates"); System.out.println("and computes the result of an election"); System.out.println(); } // Reads in user input and returns the total votes for one candidate. // // Scanner console - the Scanner to use for input // String party - the name of the party public static int getVotes(String party) { System.out.println("Enter " + party + " candidate's totals:"); System.out.print("How many precincts? "); int numPrecincts = console.nextInt(); int totalVotes = 0; for (int i = 0; i <= numPrecincts; i++) { System.out.print(" Votes in next precinct? "); int votes = console.nextInt(); totalVotes += votes; } System.out.println(); return totalVotes; } // Computes and prints the final results of the election. // // int firstVotes - the number of votes the first candidate received // int secondVotes - the number of votes the second candidate received 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 " + round2(pct1) + "% of the votes cast"); System.out.println("Pajama Party received " + round2(pct2) + "% of the votes cast"); if (pct1 < pct2) { System.out.println("Pizza Party wins!"); } else if (pct2 < pct1) { System.out.println("Pajama Party wins!"); } else { // pct2 == pct1 System.out.println("It's a tie!"); } } // Compute and print the precentage of eligible voters // who cast a vote. // // int totalVotes - the total number of votes cast public static void reportTurnout(int totalVotes) { double turnoutPct = getPercentage(totalVotes, NUM_VOTERS); System.out.println("Turnout was " + round2(turnoutPct) + "%"); } // Computes and returns the percentage of votes earned. // // int myVotes - number of votes earned // int totalVotes - total votes cast public static double getPercentage(int myVotes, int totalVotes) { return 100 * myVotes / totalVotes; } // Returns the given number rounded to two decimal places. // // double num - the number to round public static double round2(double num) { return Math.round(num * 100.0) / 100.0; } }