Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

* The Map class is used to create and manipulate voting maps. The value of a * cell on the map denotes the party for

* The Map class is used to create and manipulate voting maps. The value of a * cell on the map denotes the party for which the majority of the population * of that cell votes for. For instance, in the following map, PARTY_X is the * choice of voters in three cells, while PARTY_O is preferred in the rest of * the map: * O X O * X O X * O O O * A map is always a square, i.e. the total width and length are always equal. * The size of the map above is 3. public class Map {

private static final char PARTY_X = 'X'; // the symbol for PARTY_X on the map private static final char PARTY_O = 'O'; // the symbol for PARTY_O on the map private int size; // the map size, which is the size of one dimension private char[][] vote; // the array holding the value of each map cell

/** * TODO: Accessor for the vote instance variable * The accessor should return a copy of the vote instance variable instead of * a reference to the memory location of the vote array, so that privacy * leaks are avoided. * @return a reference to a char[][] array */ public char[][] getVote() { return null; }

private double probability; // the probability with which a cell on the map votes for PARTY_X

/** * TODO: Accessor for the probability instance variable * @return the double value of the probability */ public double getProbability() { return 0.0; }

/** * TODO: Initializes a Map object by creating and filling in the vote array. * @param size The size of the map to be produced. For instance, if a 5x5 map * should be generated, then size should have the value 5. * @param probability The probability with which PARTY_X wins a cell on the * map. */ public Map(int size, double probability) { }

/** * TODO: Fills the vote array with appropriate values depending on the value * of the probability instance variable. * The constructor should call this method. */ private void fillVote() {

}

/** * TODO: Initializes a Map object by creating and filling in the vote * array. * The constructor creates the vote array and checks whether the sequence is * valid (see the description of the isValidSequence method). If it is, then * it populates the vote array with values guided by a sequence (see the * description in the fillVote(int[]) method.) * @param size The size of the map to be produced. For instance, if a 5x5 map * should be generated, then size should have the value 5. * @param sequence The list of integers denoting the presence of each party in the * voting map. */ public Map(int size, int[] sequence) {

}

/** * TODO: Fills the vote array with values based on the sequence of * integers provided as input. For example, if the input list for an array of * size 5 contains the numbers {1, 7, 8, 3, 4, 2}, then the vote array should * be constructed as follows: * - the first cell should vote for PARTY_X * - the next 7 cells should vote for PARTY_O * - the following 8 cells should vote for PARTY_X * - the next 3 cells should vote for PARTY_O * - the following 4 cells should vote for PARTY_X * - the final 2 cells should vote for PARTY_O * The resulting array of this example, when printed, will look like this: * X O O O O * O O O X X * X X X X X * X O O O X * X X X O O * As illustrated by the example, the numbers in the sequence denote how many * cells vote for each of the two parties. When considering the next cell of * a cell at the end of a row, we move to the first cell of the next row. * @param sequence The list of integers denoting the presence of each party * in the voting map. */ private void fillVote(int[] sequence) {

}

/** * TODO: Verifies that the sequence of integers which is supposed to * be used for the construction of the vote array is valid. A sequence is * valid, if the sum of its elements is equal to size * size. * @param sequence The list of integers denoting the presence of each party * in the voting map. * @return a boolean value denoting whether the argument is a valid sequence */ private boolean isValidSequence(int[] sequence) { return false; } /** * TODO: Prints the map row after row. * The output formatting for the printing of each cell consists of * System.out.printf("%3c", vote[i][j]); */ public void printMap() {

}

/** * TODO: Calculates the number of cells voting for PARTY_X over the total * number of cells on the map. * @return a double value between 0.0 and 1.0 */ public double countPercentageX() { return 0.0; }

/** * TODO: Determines whether two maps are equal. * Two Map objects are considered equal, if their vote arrays contain the * same values at the same array positions. * @param m The Map to which this object will be compared to. * @return a boolean value denoting whether the two Map objects are equal */ public boolean equals(Map m) { return false; }

/** * TODO: The method is only supposed to be called on Map objects of * size 3. Given a 3x3 vote array, the method determines whether there exists * at least one way to draw voting districts so that PARTY_X can win 2 out of * 3 districts or more. Each district in a 3x3 array consists of 3 cells, so * there can only be 3 districts in total. Recall that district cells need to * be adjacent to each other: neighboring cells in a row or column, but not * on a diagonal. * The method does not need to construct the drawing of the districts. It * only needs to determine whether there exists a drawing that results in * PARTY_X being the winner. * Hint: Start designing your solution by examining the number of cells * PARTY_X may have in the vote array and whether there are cases that this * number makes it so that PARTY_X can never win or PARTY_X will definitely * win. * @return a boolean value denoting whether there exists at least one drawing * of the districts that will proclaim PARTY_X as the election winner. */ public boolean canXWin() { return false; } }

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

Statistical And Scientific Database Management International Working Conference Ssdbm Rome Italy June 21 23 1988 Proceedings Lncs 339

Authors: Maurizio Rafanelli ,John C. Klensin ,Per Svensson

1st Edition

354050575X, 978-3540505754

More Books

Students also viewed these Databases questions

Question

What is human nature?

Answered: 1 week ago