Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Need help on Java @Test public void testHuntingtonHill() { // Explicit test cases int[] a1 = {42}; int[] b1 = {5}; assertArrayEquals(b1, P2J7.huntingtonHill(a1, 5)); int[]
Need help on Java
@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 | |
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()); | |
} | |
Please make sure to pass the tester. will vote up
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 PriorityQueueStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started