* 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;
}
}
I have completed 8 methods in the given class, please post another question for the rest of the methods.
MAP Class
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() {
char[][] copy = new
char[this.size][this.size];
for (int i = 0; i < size; i++)
{
for (int j = 0;
j < size; j++) {
copy[i][j] = vote[i][j];
}
}
return copy;
}
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 this.probability;
}
/**
* 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) {
this.size = size;
this.probability =
probability;
char[][] vote = new
char[this.size][this.size];
}
/**
* 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) {
this.size = size;
this.vote = new
char[this.size][this.size];
if (isValidSequence(sequence))
{
fillVote(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) {
int c = 0;
int a = sequence[c];
char current = PARTY_X;
for (int i = 0; i < size;
i++) {
for (int j = 0;
j < size; j++) {
if (a != 0) {
vote[i][j] = current;
a--;
} else {
a = sequence[c++];
if (current == PARTY_X)
current =
PARTY_O;
else
current =
PARTY_X;
}
}
}
}
/**
* 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) {
int sum = 0;
for (int i = 0; i <
sequence.length; i++) {
sum +=
sequence[i];
}
if (sum == (this.size *
this.size))
return
true;
else
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() {
for (int i = 0; i < size;
i++) {
for (int j = 0;
j < size; j++) {
System.out.printf("%3c", vote[i][j]);
}
System.out.println();
}
}
/**
* 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() {
int sum = 0;
for (int i = 0; i < size; i++)
{
for (int j = 0;
j < size; j++) {
sum += vote[i][j];
}
}
double p = sum / (this.size *
this.size);
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) {
char[][] other = m.getVote();
for (int i = 0; i < size; i++)
{
for (int j = 0;
j < size; j++) {
if (vote[i][j] != other[i][j])
return false;
}
}
return true;
}
/**
* 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;
}
}
* The Map class is used to create and manipulate voting maps. The value of a...
Java Quadratic Probing Hash table HELP! Complete the Map and Entry class provided in Map.java. Create a tester/driver class to show the Map class is working as intended. Methods/Constructor correctly completed (2pt) This is the Map class methods Map(), put(key, value), get(key), isEmpty(), makeEmpty() Private class correctly completed (2pt) This is the Entry class methods Add the methods that are required for this to work correctly when stored in the QuadraticProbingHashTable Tester class (1pt) Show the methods of the Map...
Load to the IDEA the remaining classes from the provided Lab02.zip file. Repeat the previous project inside the ArraySetWithArray class. As shown in the UML diagram below ArraySetWithArray class does not utilize ResizableArrayBag object as its instance variable, it has setOfEntries defined as an array which should be dynamically resized if more room needed (double the size). displaySet method should check if the set is empty and display appropriate message; if the set is not empty should display the number...
Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course { /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...
Implement the missing methods in java! Thanks! import java.util.Iterator; import java.util.NoSuchElementException; public class HashTableOpenAddressing<K, V> implements DictionaryInterface<K, V> { private int numEntries; private static final int DEFAULT_CAPACITY = 5; private static final int MAX_CAPACITY = 10000; private TableEntry<K, V>[] table; private double loadFactor; private static final double DEFAULT_LOAD_FACTOR = 0.75; public HashTableOpenAddressing() { this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR); } public HashTableOpenAddressing(int initialCapacity, double loadFactorIn) { numEntries = 0; if (loadFactorIn <= 0 || initialCapacity <= 0) { throw new IllegalArgumentException("Initial capacity and load...
public class SimpleSoftmax { /** * This method calculates the softmax output of a non-normalized array. * Example: double[] nonNormArr = {-3.0, 0.2, 7.8} * gives the result {0.00002, 0.00050, 0.99948} * Details of how to compute the softmax output can be found at zyBook instructions. * Hint: use Math.exp(double a) to calculate the exponential values. * @param nonNormArr The non-normalized array to be softmaxed. * @return The softmax output of the input...
Objectives
Problem solving using arrays and ArrayLists. Abstraction.
Overview
The diagram below illustrates a banner constructed from
block-letters of size 7. Each block-letter is
composed of 7 horizontal line-segments of width 7 (spaces
included):
SOLID
as in block-letters F, I, U, M, A, S and the
blurb RThere are six distinct line-segment
types:
TRIPLE
as in block-letter M
DOUBLE
as in block-letters U, M, A
LEFT_DOT
as in block-letters F, S
CENTER_DOT as
in block-letter...
Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) { if(arr == null || arr.length == 0) { return -1; } for (int i = 0; i < arr.length; i++) { if(arr[i] == ch) { return i; } } return -1; ...
//implement the binomial heap please import java.util.LinkedList; import java.util.NoSuchElementException; /** * Binomial Heap Implementation * * @author First Last * @since ${date} */ public class BinomialHeap<T extends Comparable<? super T>> implements binomialHeapInterface<T> { private static final int DEFAULT = 5; // default size for the binomial heap public Node<T>[] forest; private int n; private boolean isMaxHeap; /** * Node Class for nodes in Binomial Heap */ protected class Node<T> { private T value; private int degree; private LinkedList<Node<T>> children; /**...
public class Fish {
private String species;
private int size;
private boolean hungry;
public Fish() {
}
public Fish(String species, int size) {
this.species = species;
this.size = size;
}
public String getSpecies() {
return species;
}
public int getSize() {
return size;
}
public boolean isHungry() {
return hungry;
}
public void setHungry(boolean hungry) {
this.hungry = hungry;
}
public String toString() {
return "A "+(hungry?"hungry":"full")+" "+size+"cm "+species;
}
}Define a class called Lake that defines the following private...
In java please: TotalCostForTicket interface Create interface that includes one variable taxRate which is .09. It also includes an abstract method calculateTotalPrice which has no parameters and returns a value of type double. public abstract class Ticket { //There is a public static instance variable of type double for the basePrice. public static double basePrice; // private int theaterNumber; private int seatNumber; private boolean isTicketSold; private boolean isTicketReserved; public Ticket(int theaterNumber, int seatNumber) { this.theaterNumber = theaterNumber; this.seatNumber = seatNumber;...