Question
Asked by:
$10.00 Array problem
- From Education: General-Education
- Closed, but you can still post tutorials
- Due on Mar. 07, 2009
- Asked on Mar. 05, 2009 at 01:06:13AM
Q:Write a program that prompts the user to enter the names and vote totals for five candidates in an election, computes each candidate's percentage of the vote, and determines the winner, as follows:
1. First, use a for loop to prompt for the five candidates' names and store them in an array of five Strings. (We did not see any examples that created arrays of Strings tonight, but you use the same kind of statement: for example, String name = new String[5];). Use a second for loop to prompt for the votes received by each of the five candidates; includd the candidate's name in the prompt asking for their number of votes. For each number of votes entered, re-prompt until the user enters a non-negative value. Store the five numbers of votes received in an array of five ints.
2. Next, compute the total number of votes cast, and store this in an int variable. For each candidate, compute the percentage of the total votes that were received by the candidate. (This is the number of votes received by the candidate, divided by the total number of votes, multiplied by 100 -- be sure to use floating-point division.) Display each candidate's name, the number of votes they received, and the percentage of the total votes that they received on one line. Arrange this information in three columns (name, votes, and percent) with headings, and format the output nicely so that the vote totals are lined up and the percentages are lined up and displayed to two decimal places in the output. Beneath this, display the total number of votes cast.
3. Finally, determine which candidate received the largest number of votes, and report that he/she is the winner. (To simplify matters, you may assume that there is not a tie for first place.) Also, check to see if the winning candidate received a majority of the votes (that is, more than half of the total votes), and report either that the winner received a majority or that no candidate received a majority.
All input should be done with dialog boxes, and all output should be done with System.out.println (print and printf are also okay, of course).
A sample output (not including the dialog boxes used for input) is below:
Name Votes Pct
Kate 1002 41.29%
Uncle Joe 250 10.30%
Betty Jo 400 16.48%
Billie Jo 500 20.60%
Bobbie Jo 275 11.33%
Total Votes 2427
The winner is Kate, with 1002 votes
No candidate received a majority of the votes



