// 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 has added some structure by defining methods, // but still has a lot of redundancy. In addition, the program uses chaining, // which is considered poor style. Note how each method simply calls the next, // with control never returning to main until the end of the program. import java.util.*; public class ElectionChained { public static final int NUM_VOTERS = 291; // total number of eligible voters public static void main(String[] args) { Scanner console = new Scanner(System.in); intro(); pajamaParty(console); } // 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 and computes the second candidate's total votes. // Scanner console: the Scanner to use for input // int firstVotes: the number of votes the first candidate received public static void pajamaParty(Scanner console) { System.out.println("Enter Pajama Party candidate's totals:"); System.out.print("How many precincts? "); int numPrecincts = console.nextInt(); int pajama = 0; for (int i = 1; i <= numPrecincts; i++) { System.out.print(" Votes in next precinct? "); int votes = console.nextInt(); pajama += votes; } System.out.println(); costumeParty(console, pajama); } // Reads in and computes the first candidate's total votes. // Scanner console: the Scanner to use for input public static void costumeParty(Scanner console, int pajama) { System.out.println("Enter Costume Party candidate's totals:"); System.out.print("How many precincts? "); int numPrecincts = console.nextInt(); int costume = 0; for (int i = 1; i <= numPrecincts; i++) { System.out.print(" Votes in next precinct? "); int votes = console.nextInt(); costume += votes; } System.out.println(); reportResults(console, costume, pajama); } // Computes and prints the final results of the election. // Scanner console: the Scanner to use for input // int firstVotes: the number of votes the first candidate received // int secondVotes: the number of votes the second candidate received public static void reportResults(Scanner console, int firstVotes, int secondVotes) { int totalVotes = firstVotes + secondVotes; double pct1 = 100.0 * firstVotes / totalVotes; double pct2 = 100.0 * secondVotes / totalVotes; System.out.println("Pajama party received " + round2(pajamaPct) + "% of the " + totalVotes + " votes."); System.out.println("Costume party received " + round2(costumePct) + "% of the " + totalVotes + " votes."); if (pajamaPct > costumePct) { System.out.println("Pajama party wins!!"); } else if (costumePct < pajamaPct) { System.out.println("Costume party wins!!"); } else { System.out.println("It's a tie!!"); } System.out.println("Congratulations to all candidates"); reportTurnout(totalVotes); } // Prints the turnout results and an associated message // int totalVotes: the total number of votes cast in the election public static void reportTurnout(int totalVotes) { double turnoutPct = calculatePercent(totalVotes, NUM_VOTERS); System.out.println("Turnout was " + turnoutPct + "%"); if (turnoutPct >= 75) { System.out.println("What a politically-active community!"); } else if (turnoutPct >= 50) { System.out.println("The majority of people came out to vote!"); } else if (turnoutPct >= 25) { System.out.println("We've got some work to do..."); } else { System.out.println("How does no one like pajamas or costumes?"); } } // 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; } }