Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE SOLVE THIS QUESTION CODE PROVIDED : Ranked Choice Voting is a system that allows voters to select as many candidates as they like on

PLEASE SOLVE THIS QUESTION

image text in transcribed

image text in transcribed

image text in transcribed

CODE PROVIDED :

image text in transcribed

image text in transcribed

image text in transcribed

Ranked Choice Voting is a system that allows voters to select as many candidates as they like on their ballots, but must distinctly rank each choice, identifying first preference, second preference, etc., with no ties allowed. It is beginning to be adopted for some elections in the United States, including national elections in Alaska and Maine. The winner is determined as follows. Tally the votes for each of the candidates appearing as the first preference and count the total number of ballots. 1. Determine whether any candidate has earned more than half of the votes. If so, stop, that candidate wins. 2. Determine the candidate with the lowest number of votes and remove that candidate from every ballot (so if this candidate was the first preference on a particular ballot, then the second preference on that ballot now becomes the first preference, etc.) 3. Remove all ballots that are exhausted, i.e., no longer have any candidates. 4. Return to step 1 and repeat the process. Here's an example. Suppose Alice, Bob, and Carl are candidates and 105 voters submit a ballot, broken down as follows. (Step 1) The vote tally is Alice 35 , Bob 40 , and Carl 30 votes. (Step 2) 53 votes are needed to win and no candidate has earned a majority (Step 3) Carl has the fewest votes. Carl is eliminated from all of the ballots (Step 4) 5 ballots are exhausted (the 5 with Carl only) leaving 100 ballots. The vote breakdown after the steps above is as follows. (Step 1) The vote tally is Alice 55 , Bob 45 (Step 2) Alice wins, since 51 votes is a majority and Alice earned 55. You might be wondering: why do this? This system allows for numerous candidates to run with an "instantaneous" runoff election in the event of no majority winner. This system could also be used in, for example, a U.S. Presidential election to allow an independent or small-party candidate to run without fear of "splitting up" the vote for a candidate from a main-party (Democratic or Republican) candidate. Changing the voting approach might aid in allowing additional parties to grow in the U.S. Methods 1. Method name: convert 0 a. Goal: Convert data into a format that will make the rest of the tasks easier. b. Input: A String array c. Output: An ArrayList of Ballot objects d. Extra info: The input String array should be "fileContents", a String array provided with the starter code. Each String in the array represents one ballot in the election. A ballot with 2 or more candidates separates each name with a comma. Ballots with one candidate have just the name of the candidate. Candidates are listed in preferred order (EX: "Alice,Bob" would indicate that the voter ranked Alice first and Bob second). e. Tasks: i. Create an ArrayList to hold Ballot objects ii. Convert each String in the array to a Ballot object iii. Store each new Ballot object in the ArrayList you created iv. Return the ArrayList 2. Method name: tallies 0 a. Goal: Count how many votes each candidate received b. Input: An ArrayList of Ballot objects (representing all ballots cast) c. Output: A HashMap with candidate names as keys and a count of votes for the candidate as values d. Tasks: i. Create a HashMap and use techniques we have learned to insert candidates into the HashMap as they appear and tally the votes for each candidate. 3. Method name: countTotalVotes 0 a. Goal: Count how many total votes there are b. Input: A HashMap containing candidate names as keys and a count of votes for the candidate as values c. Output: An integer representing the total number of votes d. Task: Count the total number of votes in the HashMap 4. Method name: analyze 0 a. Goal: Determine if there is a winner or loser, and which candidate won or lost b. Input: A HashMap containing candidate names as keys and a count of votes for the candidate as values c. Output: A Result object storing the name of the winner/loser and whether they won or lost. d. Tasks: i. Determine how many votes are needed to win (HINT: use the countTotalVotes method to help) ii. Determine if any candidate earned enough votes to win. If so, return the candidate's name as the winner. If no candidates meet this criteria, then return the candidate with the fewest votes as the loser. 5. Method name: printCounts() a. Goal: Print out the tally of votes for each candidate (see examples below) b. Input: A HashMap containing candidate names as keys and a count of votes for the candidate as values c. Output: Nothing d. Tasks: i. Print the following information, one line per key in the HashMap given 1. "Vote Tallies" 2. >: // Examples below a. Alice: 2 b. Bob: 2 c. Bill: 1 6. Method name: remove( a. Goal: This method is called if no one wins a round of voting and the losing candidate needs to be removed from all votes cast. b. Inputs: i. A String with the name of the candidate to be removed ii. An ArrayList of Ballot objects representing all ballots cast c. Output: Nothing d. Tasks: i. For each Ballot in the given ArrayList, remove the given candidate name from the Ballot ii. If the Ballot is empty (no more candidates that were on the Ballot exist any more), then the Ballot should be removed from the ArrayList entirely. 1. HINT: ArrayList removals can be tricky! Be sure you review how to do this. 7. Method name: printPercentages ( a. Goal: When a winner has been found, print out the final percentages for each candidate b. Input: A HashMap containing candidate names as keys and a count of votes for the candidate as values c. Output: Nothing d. Tasks: i. Use the countTotalVotes method to count the total number of votes cast ii. Print the following information, one line per key in the HashMap given 1. "Vote Percentages" 2. >% I/ Examples below Ranked Choice Voting is a system that allows voters to select as many candidates as they like on their ballots, but must distinctly rank each choice, identifying first preference, second preference, etc., with no ties allowed. It is beginning to be adopted for some elections in the United States, including national elections in Alaska and Maine. The winner is determined as follows. Tally the votes for each of the candidates appearing as the first preference and count the total number of ballots. 1. Determine whether any candidate has earned more than half of the votes. If so, stop, that candidate wins. 2. Determine the candidate with the lowest number of votes and remove that candidate from every ballot (so if this candidate was the first preference on a particular ballot, then the second preference on that ballot now becomes the first preference, etc.) 3. Remove all ballots that are exhausted, i.e., no longer have any candidates. 4. Return to step 1 and repeat the process. Here's an example. Suppose Alice, Bob, and Carl are candidates and 105 voters submit a ballot, broken down as follows. (Step 1) The vote tally is Alice 35 , Bob 40 , and Carl 30 votes. (Step 2) 53 votes are needed to win and no candidate has earned a majority (Step 3) Carl has the fewest votes. Carl is eliminated from all of the ballots (Step 4) 5 ballots are exhausted (the 5 with Carl only) leaving 100 ballots. The vote breakdown after the steps above is as follows. (Step 1) The vote tally is Alice 55 , Bob 45 (Step 2) Alice wins, since 51 votes is a majority and Alice earned 55. You might be wondering: why do this? This system allows for numerous candidates to run with an "instantaneous" runoff election in the event of no majority winner. This system could also be used in, for example, a U.S. Presidential election to allow an independent or small-party candidate to run without fear of "splitting up" the vote for a candidate from a main-party (Democratic or Republican) candidate. Changing the voting approach might aid in allowing additional parties to grow in the U.S. Methods 1. Method name: convert 0 a. Goal: Convert data into a format that will make the rest of the tasks easier. b. Input: A String array c. Output: An ArrayList of Ballot objects d. Extra info: The input String array should be "fileContents", a String array provided with the starter code. Each String in the array represents one ballot in the election. A ballot with 2 or more candidates separates each name with a comma. Ballots with one candidate have just the name of the candidate. Candidates are listed in preferred order (EX: "Alice,Bob" would indicate that the voter ranked Alice first and Bob second). e. Tasks: i. Create an ArrayList to hold Ballot objects ii. Convert each String in the array to a Ballot object iii. Store each new Ballot object in the ArrayList you created iv. Return the ArrayList 2. Method name: tallies 0 a. Goal: Count how many votes each candidate received b. Input: An ArrayList of Ballot objects (representing all ballots cast) c. Output: A HashMap with candidate names as keys and a count of votes for the candidate as values d. Tasks: i. Create a HashMap and use techniques we have learned to insert candidates into the HashMap as they appear and tally the votes for each candidate. 3. Method name: countTotalVotes 0 a. Goal: Count how many total votes there are b. Input: A HashMap containing candidate names as keys and a count of votes for the candidate as values c. Output: An integer representing the total number of votes d. Task: Count the total number of votes in the HashMap 4. Method name: analyze 0 a. Goal: Determine if there is a winner or loser, and which candidate won or lost b. Input: A HashMap containing candidate names as keys and a count of votes for the candidate as values c. Output: A Result object storing the name of the winner/loser and whether they won or lost. d. Tasks: i. Determine how many votes are needed to win (HINT: use the countTotalVotes method to help) ii. Determine if any candidate earned enough votes to win. If so, return the candidate's name as the winner. If no candidates meet this criteria, then return the candidate with the fewest votes as the loser. 5. Method name: printCounts() a. Goal: Print out the tally of votes for each candidate (see examples below) b. Input: A HashMap containing candidate names as keys and a count of votes for the candidate as values c. Output: Nothing d. Tasks: i. Print the following information, one line per key in the HashMap given 1. "Vote Tallies" 2. >: // Examples below a. Alice: 2 b. Bob: 2 c. Bill: 1 6. Method name: remove( a. Goal: This method is called if no one wins a round of voting and the losing candidate needs to be removed from all votes cast. b. Inputs: i. A String with the name of the candidate to be removed ii. An ArrayList of Ballot objects representing all ballots cast c. Output: Nothing d. Tasks: i. For each Ballot in the given ArrayList, remove the given candidate name from the Ballot ii. If the Ballot is empty (no more candidates that were on the Ballot exist any more), then the Ballot should be removed from the ArrayList entirely. 1. HINT: ArrayList removals can be tricky! Be sure you review how to do this. 7. Method name: printPercentages ( a. Goal: When a winner has been found, print out the final percentages for each candidate b. Input: A HashMap containing candidate names as keys and a count of votes for the candidate as values c. Output: Nothing d. Tasks: i. Use the countTotalVotes method to count the total number of votes cast ii. Print the following information, one line per key in the HashMap given 1. "Vote Percentages" 2. >% I/ Examples below

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Ehs 2.0 Revolutionizing The Future Of Safety With Digital Technology

Authors: Tony Mudd

1st Edition

B0CN69B3HW, 979-8867463663

More Books

Students also viewed these Databases questions

Question

Distinguish between disidentification and discounting.

Answered: 1 week ago