Election Day

It's almost election day and the election officials need a program to help tally election results. There are two candidates for office -- Polly Tichen and Ernest Orator. The program's job is to take as input the number of votes each candidate received in each voting precinct and find the total number of votes for each. The program should print the final tally for each candidate -- both the total number of votes each received and the percent of votes each received. Clearly a loop is needed. Each iteration of the loop is responsible for reading in the votes from a single precinct and updating the tallies. A skeleton of the program is in the file Election.java. Open a copy of the program in your text editor and do the following.
  1. Add the code to control the loop. You must use a do...while loop. The loop must be controlled by asking the user whether or not there are more precincts to report (that is, more precincts whose votes need to be added in). The user should answer with the character y or n, though your program should also allow uppercase repsonses. The variable response (type char) has already been declared. Test your program, making sure it terminates once the user enters an 'n' or 'N'.

  2. Add the code to read in the votes for each candidate and find the total votes. Note that variables have already been declared for you to use. Print the totals and the percentages after the loop. (Hint, to print percentages, you might want to use printf. )

  3. Test your program to make sure it is correctly tallying the votes and finding the percentages AND that the loop control is correct (it goes when it should and stops when it should). If there are problems, try adding print statements at appropriate places in the loop to see what the values of various variables are as the program runs.

  4. The election officials want more information. They want to know how many precincts each candidate carried (won). Add code to compute and print this. You'll probably need three new variables: one to count the number of precincts won by Polly, one to count the number won by Ernest, and one to count the number of ties. Test your program after adding this code. Submit this version of your program and copy an example transcript into the lab document.

Here is some sample input and output:

Election Day Vote Counting Program


Are there more precints to report? y
How many votes for Polly? 15
How many votes for Ernest? 21
Are there more precints to report? y
How many votes for Polly? 17
How many votes for Ernest? 7
Are there more precints to report? y
How many votes for Polly? 23
How many votes for Ernest? 31
Are there more precints to report? n
Polly received 55 votes, 48.25% of the total votes cast.Ernest received 59 votes, 51.75% of the total votes cast.Of the 3 precincts reports, Polly won 1, Ernest won 2 and 0 were ties.