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.Random; import java.io.*; import java.util.*; import java.util.zip.CRC32; public class P2J7Test {
Please write in JAVA

| import static org.junit.Assert.*; |
| | import org.junit.After; |
| | import org.junit.Before; |
| | import org.junit.Test; |
| | import java.util.Random; |
| | |
| | import java.io.*; |
| | import java.util.*; |
| | import java.util.zip.CRC32; |
| | |
| | public class P2J7Test { |
| | |
| | private static final int SEED = 76543; |
| | |
| | @Test public void testHuntingtonHill() { |
| | // Explicit test cases |
| | int[] a1 = {42}; |
| | int[] b1 = {5}; |
| | assertArrayEquals(b1, P2J7.huntingtonHill(a1, 5)); |
| | |
| | int[] a2 = {3, 4}; |
| | int[] b2 = {2, 2}; |
| | assertArrayEquals(b2, P2J7.huntingtonHill(a2, 4)); |
| | |
| | int[] a3 = {18, 17}; |
| | int[] b3 = {4, 3}; |
| | assertArrayEquals(b3, P2J7.huntingtonHill(a3, 7)); |
| | |
| | int[] a4 = {17, 3, 4, 10, 11, 14}; |
| | int[] b4 = {2, 1, 1, 1, 1, 2}; |
| | assertArrayEquals(b4, P2J7.huntingtonHill(a4, 8)); |
| | |
| | int[] a5 = {13, 15, 20, 33, 45, 55, 60, 82}; |
| | int[] b5 = {1, 1, 2, 3, 3, 4, 5, 6}; |
| | assertArrayEquals(b5, P2J7.huntingtonHill(a5, 25)); |
| | |
| | int[] a6 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| | int[] b6 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| | assertArrayEquals(b6, P2J7.huntingtonHill(a6, 55)); |
| | |
| | // Pseudorandom fuzz tester |
| | Random rng = new Random(SEED); |
| | CRC32 check = new CRC32(); |
| | HashSet seen = new HashSet(); |
| | int scale = 1; |
| | for(int i = 1; i |
| | if(i % 50 == 0) { scale *= 10; } |
| | int[] pops = new int[i + 1]; |
| | seen.clear(); |
| | for(int j = 0; j |
| | int p; |
| | int count = 0; |
| | do { |
| | if(j > 0 && rng.nextDouble() |
| | p = pops[j-1] + 1; |
| | } |
| | else { |
| | p = (rng.nextInt(50) + 1) * scale; |
| | p += rng.nextInt(p); |
| | } |
| | } while(seen.contains(p)); |
| | assert p > 0; |
| | seen.add(p); |
| | pops[j] = p; |
| | } |
| | int seats = 2 * i + rng.nextInt(10 * i + 2); |
| | int[] result = P2J7.huntingtonHill(pops, seats); |
| | check.update(Arrays.toString(result).getBytes()); |
| | } |
| | assertEquals(811873173L, check.getValue()); |
| | } |
| } |
make sure the code can pass the test, will give up the vote. Thanks
public static int[] huntingtonHill(int[] population, int seats) that, given the population in each state and the number of seats to distribute across these states, creates and returns a new array that contains the counts of seats given to each state. To make results unambiguous for the JUnit fuzz test, whenever two states currently have the same priority quantity, the next seat is given to the state that is earlier in the population array. To write this method, first add the class Fraction from the class examples project into your labs Blue) project, to let you perform your calculations with exact integer fractions without any possibility of integer overflow or floating point rounding error. Not even one floating point number, let alone any calculation involving them, should appear inside your method! You should also note that even though the population of some state fits inside an int without an overflow, the square of that population will not equally friendly once this population is in the millions, which surely is not an unrealistic case! These population squaring operations absolutely have to be performed using exact Fraction objects of unlimited range, not as primitive int values. (Would your instructor really be so mean as to design the tester method testHuntingtonHill to fuzz populations that are too big to be accurately handled with floating point operations? Would that same instructor also ensure that some states end up having almost equal populations that differ by a measly little one? Come on. At this stage of the studies, and especially if you have already taken my earlier course CCPS 109, do you really even have to ask?) The best way to track of the relative priorities of the states (each state identified as an integer as its position in population table) is to keep them inside a PriorityQueue instance with a custom Comparator object whose compareTo(Integer a, Integer b) method renders its verdict based on the current priorities of the states a and b. These priorities, of course, are kept up to date inside another array Fraction[] priorities that is defined as a local variable inside this huntingtonHill method, outside the nested comparator class. To find out which state gets the next seat, simply poll the queue for the state whose priority is currently the highest. Update the priority for that state to the priorities array, and offer the state back into the priority queue. As historically important as this algorithm has been to the fate of the free world and our ability to keep rocking in it, it also has a more mundane application for our everyday lives in displaying rounded percentages from a list of numbers generated from some real-world data. You know how you sometimes see a disclaimer phrased something like Due to rounding, percentages may not add up to exactly 100" under some table of numbers and the percentages computed from those numbers? Use the correct algorithm to display rounded percentages to eliminate any need for such silly disclaimers! For example, to compute the percentages rounded to one decimal place, simply distribute 1,000 imaginary seats among your data items. Each seat corresponds to one tenth of a percentage point that will be displayed as the exact percentage share of that data item. public static int[] huntingtonHill(int[] population, int seats) that, given the population in each state and the number of seats to distribute across these states, creates and returns a new array that contains the counts of seats given to each state. To make results unambiguous for the JUnit fuzz test, whenever two states currently have the same priority quantity, the next seat is given to the state that is earlier in the population array. To write this method, first add the class Fraction from the class examples project into your labs Blue) project, to let you perform your calculations with exact integer fractions without any possibility of integer overflow or floating point rounding error. Not even one floating point number, let alone any calculation involving them, should appear inside your method! You should also note that even though the population of some state fits inside an int without an overflow, the square of that population will not equally friendly once this population is in the millions, which surely is not an unrealistic case! These population squaring operations absolutely have to be performed using exact Fraction objects of unlimited range, not as primitive int values. (Would your instructor really be so mean as to design the tester method testHuntingtonHill to fuzz populations that are too big to be accurately handled with floating point operations? Would that same instructor also ensure that some states end up having almost equal populations that differ by a measly little one? Come on. At this stage of the studies, and especially if you have already taken my earlier course CCPS 109, do you really even have to ask?) The best way to track of the relative priorities of the states (each state identified as an integer as its position in population table) is to keep them inside a PriorityQueue instance with a custom Comparator object whose compareTo(Integer a, Integer b) method renders its verdict based on the current priorities of the states a and b. These priorities, of course, are kept up to date inside another array Fraction[] priorities that is defined as a local variable inside this huntingtonHill method, outside the nested comparator class. To find out which state gets the next seat, simply poll the queue for the state whose priority is currently the highest. Update the priority for that state to the priorities array, and offer the state back into the priority queue. As historically important as this algorithm has been to the fate of the free world and our ability to keep rocking in it, it also has a more mundane application for our everyday lives in displaying rounded percentages from a list of numbers generated from some real-world data. You know how you sometimes see a disclaimer phrased something like Due to rounding, percentages may not add up to exactly 100" under some table of numbers and the percentages computed from those numbers? Use the correct algorithm to display rounded percentages to eliminate any need for such silly disclaimers! For example, to compute the percentages rounded to one decimal place, simply distribute 1,000 imaginary seats among your data items. Each seat corresponds to one tenth of a percentage point that will be displayed as the exact percentage share of that data item