// 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 = 1013; // total number of eligible voters public static void main(String[] args) { Scanner console = new Scanner(System.in); intro(); pizzaParty(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 pizza party candidate's total votes. // // Scanner console - the Scanner to use for input public static void pizzaParty(Scanner console) { System.out.println("Enter Pizza Party candidate's totals:"); System.out.print("How many precincts? "); int numPrecincts = console.nextInt(); int pizza = 0; for (int i = 1; i <= numPrecincts; i++) { System.out.print(" Votes in next precinct? "); int votes = console.nextInt(); pizza += votes; } System.out.println(); pajamaParty(console, pizza); } // Reads in and computes the pajama party candidate's total votes. // // Scanner console - the Scanner to use for input // int pizza - the number of votes the pizza party candidate received public static void pajamaParty(Scanner console, int pizza) { 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, pizza, 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("Pizza Party received " + round2(pct1) + "% of the votes cast"); System.out.println("Pajama Party received " + round2(pct2) + "% of the votes cast"); System.out.println("Pizza Party wins!"); double turnoutPct = 100.0 * totalVotes / NUM_VOTERS; System.out.println("Turnout was " + round2(turnoutPct) + "%"); } else if (pct2 > pct1) { System.out.println("Pizza 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("Pizza 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; } }