Question: Please write in JAVA import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.*; import java.util.zip.CRC32; public class MultipleWinnerElectionTest { @Test public void testDHondtHundred()
Please write in JAVA
| import static org.junit.Assert.*; | |
| import org.junit.After; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| import java.util.*; | |
| import java.util.zip.CRC32; | |
| public class MultipleWinnerElectionTest { | |
| @Test public void testDHondtHundred() { | |
| test(100, 3816530178L, 0); | |
| } | |
| @Test public void testDHondtTenThousand() { | |
| test(10000, 572303332L, 0); | |
| } | |
| @Test public void testWebsterHundred() { | |
| test(100, 2959731999L, 1); | |
| } | |
| @Test public void testWebsterTenThousand() { | |
| test(10000, 2360920111L, 1); | |
| } | |
| @Test public void testImperialiHundred() { | |
| test(100, 2278224169L, 2); | |
| } | |
| @Test public void testImperialiTenThousand() { | |
| test(10000, 2082193870L, 2); | |
| } | |
| @Test public void testAll() { | |
| test(1000, 156607936L, 3); | |
| } | |
| private boolean isSorted(int[] a) { | |
| int prev = a[0] - 1; | |
| for(int e : a) { | |
| if(e | |
| prev = e; | |
| } | |
| return true; | |
| } | |
| private void test(int n, long expected, int mode) { | |
| CRC32 check = new CRC32(); | |
| Random rng = new Random(12345); | |
| int diffCount = 0; | |
| for(int i = 0; i | |
| int C = 2 + Math.min(i / 20, 20) + Math.min(i / 1000, 20); | |
| int[] votes = new int[C]; | |
| int seats = 10 + rng.nextInt(4 * C); | |
| for(int j = 0; j | |
| votes[j] += rng.nextInt(i + 10); | |
| int c = rng.nextInt(C); | |
| votes[c] += rng.nextInt(2 * i + 1); | |
| } | |
| Arrays.sort(votes); | |
| //System.out.print(Arrays.toString(votes) + " for " + seats + " seats: "); | |
| int[] result0 = null, result1 = null, result2 = null; | |
| if(mode == 0 || mode == 3) { // DHondt | |
| result0 = MultipleWinnerElection.DHondt(votes, seats); | |
| assertTrue(isSorted(result0)); | |
| check.update(Arrays.toString(result0).getBytes()); | |
| } | |
| if(mode == 1 || mode == 3) { // Webster | |
| result1 = MultipleWinnerElection.webster(votes, seats); | |
| assertTrue(isSorted(result1)); | |
| check.update(Arrays.toString(result1).getBytes()); | |
| } | |
| if(mode == 2 || mode == 3) { // Imperiali | |
| result2 = MultipleWinnerElection.imperiali(votes, seats); | |
| assertTrue(isSorted(result2)); | |
| check.update(Arrays.toString(result2).getBytes()); | |
| } | |
| if(mode == 3) { // For examining how the methods produce different results. | |
| if(!(Arrays.equals(result0, result1) && Arrays.equals(result1, result2))) { | |
| diffCount++; | |
| // System.out.print(Arrays.toString(votes) + " " + seats + ": "); | |
| // System.out.print(Arrays.toString(result0)); | |
| // System.out.print(Arrays.toString(result1)); | |
| // System.out.println(Arrays.toString(result2)); | |
| } | |
| } | |
| } | |
| if(mode == 3) { | |
| assertEquals(981, diffCount); | |
| } | |
| assertEquals(expected, check.getValue()); | |
| } | |
| } |
make sure the code can pass the test, will give up the vote. Thanks
The Anglosphere countries tend to use voting systems where each district elects exactly one representative, typically done with the crude first-past-the-post method to determine the sole winner of each district. Some other countries use some kind of party list proportional voting system where each electoral district elects multiple winners. Each voter casts a vote for a particular party, either directly (closed list) or indirectly (open list). The seats that are up for grabs in each district are dealt out to the parties in proportion to their votes in that district. In this lab, you implement three methods to compute the distribution of seats to the parties according to the votes that these parties received. The methods of D'Hondt, Webster and Imperiali, each one a special case of the highest averages method, are otherwise identical, but use a different formula to compute the priority quantity for each party, as described below. You might therefore want to implement this entire voting algorithm as one private method that the following three public methods pass the buck to. public static int[] DHondt(int[] votes, int seats) public static int[] webster(int[] votes, int seats) public static int[] imperiali(int[] votes, int seats) The parameter array votes gives the number of votes received by each party. The seats are given out one at the time same as in the Huntington-Hill method back in the Lab O(G) so that each seat goes to the party that currently has the highest priority. To make the results sufficiently unambiguous for the JUnit fuzz tests, all ties should be resolved in favour of the party whose number is higher, same as in the previous Runoff Voting lab. The priority quantity of the party that received v votes and has so far been given s seats is v/(s+1) in the D'Hondt method, v/(25+1) in the Webster method, and v/(1+s/2) in the Imperiali method. You should again perform all these calculations with exact integer arithmetic using our Fraction example class, of course! The returned result should be an int[] of the same length as the parameter array votes, each element indicating how many seats were given to that party. For example, in an election between four parties where seats=21 and votes={23,26,115,128}, the D'Hondt method would return {1,2,8,10}, the Webster method would return {2,2,8,9}, and the Imperiali method would return {1,1,9,10}. These results illustrate how adjusting the divisor sequence in the priority quantity calculation can cause this process to favour either larger or smaller parties, the Webster method being most favourable and the Imperiali method the least favourable for small parties. (As a final humorous aside on that theme, what should we think of a method where the priority quantity is computed with the formula v/2s+1 dealing the seats out logarithmically so that doubling your number of votes gives you one additional seat? Let the wonks at FiveThirty Eight wrap their pointy eggheads around that one!) The Anglosphere countries tend to use voting systems where each district elects exactly one representative, typically done with the crude first-past-the-post method to determine the sole winner of each district. Some other countries use some kind of party list proportional voting system where each electoral district elects multiple winners. Each voter casts a vote for a particular party, either directly (closed list) or indirectly (open list). The seats that are up for grabs in each district are dealt out to the parties in proportion to their votes in that district. In this lab, you implement three methods to compute the distribution of seats to the parties according to the votes that these parties received. The methods of D'Hondt, Webster and Imperiali, each one a special case of the highest averages method, are otherwise identical, but use a different formula to compute the priority quantity for each party, as described below. You might therefore want to implement this entire voting algorithm as one private method that the following three public methods pass the buck to. public static int[] DHondt(int[] votes, int seats) public static int[] webster(int[] votes, int seats) public static int[] imperiali(int[] votes, int seats) The parameter array votes gives the number of votes received by each party. The seats are given out one at the time same as in the Huntington-Hill method back in the Lab O(G) so that each seat goes to the party that currently has the highest priority. To make the results sufficiently unambiguous for the JUnit fuzz tests, all ties should be resolved in favour of the party whose number is higher, same as in the previous Runoff Voting lab. The priority quantity of the party that received v votes and has so far been given s seats is v/(s+1) in the D'Hondt method, v/(25+1) in the Webster method, and v/(1+s/2) in the Imperiali method. You should again perform all these calculations with exact integer arithmetic using our Fraction example class, of course! The returned result should be an int[] of the same length as the parameter array votes, each element indicating how many seats were given to that party. For example, in an election between four parties where seats=21 and votes={23,26,115,128}, the D'Hondt method would return {1,2,8,10}, the Webster method would return {2,2,8,9}, and the Imperiali method would return {1,1,9,10}. These results illustrate how adjusting the divisor sequence in the priority quantity calculation can cause this process to favour either larger or smaller parties, the Webster method being most favourable and the Imperiali method the least favourable for small parties. (As a final humorous aside on that theme, what should we think of a method where the priority quantity is computed with the formula v/2s+1 dealing the seats out logarithmically so that doubling your number of votes gives you one additional seat? Let the wonks at FiveThirty Eight wrap their pointy eggheads around that one!)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
