// This program calculates the percentage of votes a candidate has received. import java.util.*; public class ElectionGoodVersion { public static final int TOTAL_POPULATION = 160; // CSE 142 population public static void main(String[] args) { intro(); Scanner console = new Scanner(System.in); double result1 = getCandidateResult(console); double result2 = getCandidateResult(console); reportResults(result1, result2); } // prints an introduction of the program for the user public static void intro() { System.out.println("This program reads in data for two candidates"); System.out.println("and computes their standing in the election"); System.out.println(); } // uses the provided console Scanner to prompt for a candidate's // vote counts, and returns the percentage of the population that // voted for this candidate public static double getCandidateResult(Scanner console) { System.out.println("Enter next candidate's information:"); System.out.print("number of votes? "); int votes = console.nextInt(); System.out.println(); return getPercentage(votes); } // returns the percentage of the population that voted, // given a vote count public static double getPercentage(int votes) { return 100.0 * votes / TOTAL_POPULATION; } // given the status of two candidates, prints a report to the user // about the candidate's popularity public static void reportResults(double result1, double result2) { System.out.println("Candidate #1 has = " + round(result1) + "% of the votes"); reportStatus(result1); System.out.println("Candidate #2 has = " + round(result2) + "% of the votes"); reportStatus(result2); } // reports the candidate's election status given a percentage public static void reportStatus(double result) { if (result > 50) { System.out.println("winning"); } else if (result < 50) { System.out.println("losing"); } else { // result == 50 System.out.println("stalemate"); } } // returns the result of rounding n to 1 digit // after the decimal point public static double round(double n) { return Math.round(n * 10.0) / 10.0; } }