<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import CS99.*;
import java.text.DecimalFormat;

/**
 * VoteCounter - Lab 7
 * Author: Michael Clarkson
 * NetID: mrc26
 * Date: 7/16/00
 */
class VoteCounter {

	public static void main(String args[]) {

		//////////////////////////////////////////////////////////////////
		// Variables
		//////////////////////////////////////////////////////////////////

		int numCandidates;		// Number of candidates in election
		int[] votes;			// Array of votes for each candidate
		int c;					// Loop index for use with votes[]



		//////////////////////////////////////////////////////////////////
		// Input
		//////////////////////////////////////////////////////////////////
		
		System.out.println("Electronic Vote Counter");
		System.out.println("-----------------------");
		System.out.println();

		// Number of candidates		
			numCandidates = getNumCandidates();
			votes = new int[numCandidates];

		// Votes for each candidate		
			System.out.println("Enter the number of votes for each candidate:");
			for (c = 0; c &lt; numCandidates; c++) {
				votes[c] = getCandVotes(c+1);
			}
			System.out.println();



		//////////////////////////////////////////////////////////////////
		// Output
		//////////////////////////////////////////////////////////////////

		System.out.println("Election Results");
		System.out.println("----------------");
		System.out.println();

		// Total votes
			int totalVotes = 0;
			for (c = 0; c &lt; numCandidates; c++) {
				totalVotes += votes[c];
			}
			System.out.println("Total votes cast = " + totalVotes);

		// Average votes		
			double avgVotes = (double) totalVotes / numCandidates;		
			System.out.println("Average votes received = " + avgVotes);
		
		// Number of candidates above average votes
			int numAboveAvg = 0;
			for (c = 0; c &lt; numCandidates; c++) {
				if (votes[c] &gt; avgVotes) {
					numAboveAvg++;
				}
			}
			System.out.println("Number of candidates with an above average number of votes: " + numAboveAvg + "\n");

		// Table of votes		
			printTable(votes, totalVotes);
			System.out.println();
	
		// Winner	
			int maxVotes = -1;
			int winnerIndex = -1;
			for (c = 0; c &lt; numCandidates; c++) {
				if (votes[c] &gt; maxVotes) {
					maxVotes = votes[c];
					winnerIndex = c;
				}
			}
			System.out.println("The winner is candidate #" + (winnerIndex+1) + " with " + maxVotes + " votes");

		// Runner-up		
			int minDiff = totalVotes;
			int runnerupIndex = -1;
			for (c = 0; c &lt; numCandidates; c++) {
				int diff = maxVotes - votes[c];
				if (c != winnerIndex &amp;&amp; diff &lt; minDiff) {
					minDiff = diff;
					runnerupIndex = c;
				}
			}
			System.out.println("The runner-up is candidate #" + (runnerupIndex+1) + " with " + votes[runnerupIndex] + " votes");
		
	}


	/** 
	 * Yield the number of candidates in the election, read from System.in,
	 * validated to be at least 2.
	 */
	static int getNumCandidates() {
		int numCandidates;

		boolean valid = false;		// is the input valid?			
		do {
			System.out.print("How many candidates are there? ");
			numCandidates = Console.readInt();
			if (numCandidates &lt; 2) {
				System.out.println("There must be at least 2 candidates.");
			} else if (numCandidates &gt; 10) {
				System.out.println("There can be at most 10 candidates.");
			} else {
				valid = true;
			}
		} while (!valid);
		System.out.println();
	
		return numCandidates;
	}
	
	
	/**
	 * Yield the number of votes for candidate #candNum, read from System.in,
	 * validated to be at least 0.
	 */
	static int getCandVotes(int candNum) {
		int candVotes;
		
		do {
			System.out.print("Candidate #" + candNum + ": ");
			candVotes = Console.readInt();
			if (candVotes &lt; 0) {
				System.out.println("Number of votes cannot be negative.");
			}
		} while (candVotes &lt; 0);
		
		return candVotes;
	}


	/**
	 * Print a table of votes and percent of vote for each candidate
	 */	
	static void printTable(int[] votes, int totalVotes) {
		DecimalFormat fmt = new DecimalFormat("0.##%");
	
		System.out.println("Candidate\tVotes\tPercentage");
		for (int c = 0; c &lt; votes.length; c++) {
			double percentage = (double)votes[c]/totalVotes;
			
			System.out.println((c+1) + "\t\t" + votes[c] + "\t" + fmt.format(percentage));
		}
	}
	
	
}
</pre></body></html>