Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

public static int[] huntingtonHill(int[] population, int seats)

 

First add the class Fraction [ Linked here: https://raw.githubusercontent.com/ikokkari/JavaExamples/master/Fraction.java ] from the class examples project into your labs 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 comfortably inside an int without any overflow, the square of that population will not be equally friendly once this population is in the millions, which surely is not an unrealistic case for electoral math! For this reason, these population squaring operations absolutely have to be performed using exact Fraction objects of unlimited range, not as primitive int values. Just like it is too late to close the barn door after the horse has escaped, it is too late to convert an int value into an unlimited BigInteger once the overflow has already taken place! For example, instead of trying to be concise, normally a virtue in this place, by writing a statement

num = BigInteger.valueOf(population[i] * population[i]);

where the int multiplication can overflow before conversion to BigInteger, split this into

num = BigInteger.valueOf(population[i]);

num = num.multiply(population[i]);

to force the integer multiplication take place inside the BigInteger mechanism where there is enough room for all digits. On that note, everybody stand up in attention, eyes front to receive the following extremely important instruction!

Just because an integer value fits into an int, its square might not do so. Just because the intermediate results of your computational formulas are not given explicit symbolic names in your code, it doesn't mean that those intermediate results somehow magically wouldn't exist, nor be subject to the exact same laws of computations as your named data items!

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Heres the implementation of the huntingtonHill method based on the provided instructions java public static int huntingtonHillint population int seats Array to store seat counts for each state int sea... 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

A Pathway To Introductory Statistics

Authors: Jay Lehmann

1st Edition

0134107179, 978-0134107172

More Books

Students also viewed these Programming questions

Question

For all numbers a and b such that a Answered: 1 week ago

Answered: 1 week ago

Question

Y = 1/3x 2

Answered: 1 week ago