// 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 = 1131; // total number of eligible voters public static void main(String[] args) { Scanner console = new Scanner(System.in); intro(); costumeParty(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 first candidate's total votes. // // Scanner console - the Scanner to use for input public static void costumeParty(Scanner console) { 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(); pajamaParty(console, costume); } // 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, int costume) { 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(); 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; if (pct1 > pct2) { System.out.println("Costume Party received " + round2(pct1) + "% of the votes cast"); System.out.println("Pajama Party received " + round2(pct2) + "% of the votes cast"); System.out.println("Costume Party wins!"); double turnoutPct = 100.0 * totalVotes / NUM_VOTERS; System.out.println("Turnout was " + round2(turnoutPct) + "%"); } else if (pct2 > pct1) { System.out.println("Costume Party received " + round2(pct1) + "% of the votes cast"); System.out.println("Pajama Party received " + round2(pct2) + "% of the votes cast"); System.out.println("Pajama Party wins!"); double turnoutPct = 100.0 * totalVotes / NUM_VOTERS; System.out.println("Turnout was " + round2(turnoutPct) + "%"); } else { System.out.println("Costume Party received " + round2(pct1) + "% of the votes cast"); System.out.println("Pajama Party received " + round2(pct2) + "% of the votes cast"); System.out.println("It's a tie!"); double turnoutPct = 100.0 * totalVotes / NUM_VOTERS; System.out.println("Turnout was " + round2(turnoutPct) + "%"); } } // 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; } }