Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribed

@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());
}

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 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

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

Mastering Influxdb Database A Comprehensive Guide To Learn Influxdb Database

Authors: Cybellium Ltd ,Kris Hermans

1st Edition

B0CNGGWL7B, 979-8867766450

More Books

Students also viewed these Databases questions

Question

Define marketing myopia; give three examples in the sport industry.

Answered: 1 week ago

Question

1. Identify three approaches to culture.

Answered: 1 week ago

Question

3. Identify and describe nine cultural value orientations.

Answered: 1 week ago

Question

4. Describe how cultural values influence communication.

Answered: 1 week ago