// Miya Natsuhara // 07-15-2019 // CSE142 // TA: Grace Hopper // 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. /* DEVELOPMENT NOTES: ((Note: this is not something you should include in your own programs; this is included here to aid in your understanding and to provide additional context for the program.)) This program works functionally the same way as BadElection.java, but has much better style. We have decomposed the program into methods that capture subtasks of the program and reduce redundancy. We've also addressed some of the redundancy issues in terms of if/else factoring (look at the reportResults method). Play close attention to what each method accepts as parameters and what each method returns (if anything). */ import java.util.*; public class GoodElection { public static final int NUM_VOTERS = 291; // the total number of registered voters public static void main(String[] args) { Scanner console = new Scanner(System.in); giveIntro(); // ask for number of precincts, and get total number of votes for each candidate int totalPajamaVotes = getVotes(console, "Pajama Party"); int totalCostumeVotes = getVotes(console, "Costume Party"); // calculate each candidate's percentage of votes cast reportResults(totalPajamaVotes, totalCostumeVotes); // calculate turnout percentage and print appropriate message reportTurnout(totalPajamaVotes + totalCostumeVotes); } // Prints out a short introduction explaining the purpose of the program public static void giveIntro() { 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 returns the total number of votes for the given party // Scanner console: the Scanner to use for input // String partyName: the name of the party whose votes are returned public static int getVotes(Scanner console, String partyName) { System.out.println("Enter " + partyName + " candidate's totals: "); System.out.print("How many precincts? "); int numPrecincts = console.nextInt(); int totalVotes = 0; for (int i = 1; i <= numPrecincts; i++) { System.out.print(" Votes in next precinct? "); int numVotes = console.nextInt(); totalVotes += numVotes; } System.out.println(); return totalVotes; } // Computes and prints the final results of the election. // int totalPajamaVotes: the total number of votes the Pajama Party candidate received // int totalCostumeVotes: the total number of votes the Costume Party candidate received public static void reportResults(int totalPajamaVotes, int totalCostumeVotes) { int totalVotes = totalPajamaVotes + totalCostumeVotes; double pajamaPct = calculatePercent(totalPajamaVotes, totalVotes); double costumePct = calculatePercent(totalCostumeVotes, totalVotes); // output each candidate's vote share and who won // NOTE: another difference from BadElection.java here is factoring the printlns // reporting the percentage of votes that each party received as well as the println // congratulating the candidates. 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"); } // 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?"); } } // Computes and returns the percentage with the given numerator and denominator // int numerator: numerator to be used in calculation // int denominator: denominator to be used in calculation public static double calculatePercent(int numerator, int denominator) { return 100.0 * numerator / denominator; } // Rounds the given number to 2 decimal places and returns it // double num: the number to be rounded public static double round2(double num) { return Math.round(num * 100.0) / 100.0; } }