Question
Asked by:
mulem
mulem
Rating (1): A+
Questions Asked: 24
Tutorials Posted: 2, earned $31.39
 

$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

 


   
   
   
   
 
Available Tutorials to this Question
Posted by:
ravi_cs2008
ravi_cs2008
Rating (6): D
Questions Asked: 0
Tutorials Posted: 39, earned $83.54
 

$8.00 Java program using array to solve the winner in voting problem

  • This tutorial hasn't been purchased yet.
  • Posted on Mar. 05, 2009 at 02:22:06AM
A:
Preview: ... tchin a java file containg the code for problem.<br>Each line of code has been commented so u ...

The full tutorial is about 72 words long plus attachments.

Attachments:
voting.java (2K) (Preview)
Posted by:
rainman
rainman
Rating (40): A+
Questions Asked: 2
Tutorials Posted: 174, earned $906.64
 

$10.00 Java Voting Program Tutorial. Uses Dialog Boxes For Input, Console For Output. Outputs Into Columns. Lots Of Comments.

  • This tutorial hasn't been purchased yet.
  • Posted on Mar. 05, 2009 at 02:38:07AM
A:
Preview: ... s[])<br> {<br> //Create 2 arrays to hold the candidates names and their vote counts<br> String[] name = new String[5];<br> int[] vote = new int[5];<br> <br> //Total votes cast to computer percentages<br> int votesCast = 0;<br> <br> //Create a new array to hold the percentages<br> double[] percent = new double[5];<br> <br> //keep track of who has the most votes<br> int winnerIndex = 0;<br> int maxVotes = 0;<br> <br> //Get input for 5 names<br> for (int i = 0; i < 5; i++)<br> {<br> //Use a dialog box to get the name<br> name[i] = JOptionPane.showInputDialog(null,<br> "Enter Candidate " + (i + 1) + "'s Name",<br> "Input The Candidate Names.",<br> JOptionPane.QUESTION_MESSAGE);<br> }<br> <br> //Get ...

The full tutorial is about 1618 words long plus attachments.

Attachments:
Vote.java (4K) (Preview)
preview.JPG (24K) (Preview)
Posted by:
nzpapillon
nzpapillon
Rating (256): A+
Questions Asked: 0
Tutorials Posted: 549, earned $4,988.47
 

$10.00 Fully-working, commented tutorial code & sample output

  • This tutorial was purchased 1 time and rated A+ by students like you.
  • Posted on Mar. 07, 2009 at 09:52:52AM
A:
Preview: ... Here you ar ...

The full tutorial is about 8 words long plus attachments.

Attachments:
VoteAnalysis.java (5K) (Preview)
Vote_Analysis_Program_Sample_Output.txt (0K) (Preview)
   
Join Now or Log In
Get Tutoring
Get Paid
Academic Honesty