Question
import java.util.LinkedList; /** * Represent a JaggedArray of characters in n bins */ public class JaggedArray { // Unpacked representation private LinkedList [] unpackedValues; //
import java.util.LinkedList;
/**
* Represent a JaggedArray of characters in n bins
*/
public class JaggedArray
// Unpacked representation
private LinkedList
// Packed representation
private int[] offsets;
private T[] packedValues;
/**
* Create a unpacked JaggedArray with the given number of bins
*
* @param bins The number of bins to create
*/
public JaggedArray(int bins) {
}
/**
* Return the unpacked array of linked lists.
* Don't change this, the tests use it.
*/
public final LinkedList
return unpackedValues;
}
/**
* Return the packed values.
* Don't change this, the tests use it.
*/
public final T[] getPackedValues() {
return packedValues;
}
/**
* Return the offsets array.
* Don't change this, the tests use it.
*/
public final int[] getOffsets() {
return offsets;
}
/**
* Return the number of elements in the jagged array
*/
public int size() {
return 0;
}
/**
* Return the number of bins
*/
public int numberOfBins() {
return 0;
}
/**
* Return the number of slots in the given bin
*/
public int numberOfSlots(int bin) {
return 0;
}
/**
* Return the element at the given bin and slot number
*/
public T getElement(int bin, int slot) throws IndexOutOfBoundsException {
return null;
}
/**
* Add an element into the bin at the end
*/
public boolean addElement(int bin, T element) throws IndexOutOfBoundsException {
return false;
}
/**
* Remove an element at the given bin and slot number
*/
public boolean removeElement(int bin, int slot) throws IndexOutOfBoundsException {
return false;
}
/**
* Unpack the representation from packed.
*
* @return True if successful, or false if already unpacked
*/
public boolean unpack() {
return false;
}
/**
* Pack the values
*
* @return True if successful, or false if already packed
*/
public boolean pack() {
return false;
}
/**
* A helpful method to print out a jagged array. Useful for debugging.
*/
public void print() {
System.out.println("JaggedArray(Number of Bins=" + numberOfBins());
for (int i = 0; i
System.out.print("\tBin " + i + "(Slots=" + numberOfSlots(i) + ": ");
for (int j = 0; j
System.out.print(getElement(i, j));
if (j
System.out.print(", ");
}
}
System.out.println(")");
}
System.out.println(")");
}
}
Your Task Your task is to implement the JaggedArray class in JaggedArray.java. A skeleton is already given to you. Your implementation must use the member variables unpackedValues, packedValues and offsets. Your representation should use java.util.LinkedList. This is a Java class representing a linked list that you can use without having to write your own. These member variables are declared private, as they should be. However, the testing code uses public accessors to look at the values of your representation, so don't change those accessors. You have to implement these methods: - public JaggedArray(int bins) -- Create a jagged array with the given number of bins. - public int size() -- Return the number of elements stored in the jagged array - public int numberofBins() -- Return the number of bins in the jagged array - public int numberofslots(int bin) -- Return the number of slots stored in the given bin. This needs to work for both packed and unpacked representations. - public T getElement(int bin, int slot) -- Return the element stored at the given bin and slot number. Throw an IndexOutOfBoundsException if the bin or slot is invalid. This needs to work for both packed and unpacked representations. - public boolean addElement(int bin, T element) - Add the element to the given bin. Return true if successful, or false if the representation is packed. Throw an IndexOutOfBoundsException if the bin is invalid. - public boolean removeElement(int bin, int slot) - Remove the element from the given bin and slot. Return true if successful, or false if the representation is packed. Throw an IndexOutOfBoundsException if the bin or slot is invalid. - public boolean unpack() - Unpack the jagged array. Return true if successful, or false if the array is already unpacked. - public boolean pack() - Pack the jagged array. Return true if successful, or false if the array is already packed. Testing and Debugging The JaggedArray class has a member function called "print" that prints out the jagged array. It should help you in debugging your code. I suggest writing a main function and exercising your code with a few add and remove, pack and unpack operations, then printing out the array and seeing if it's what you expect. The test code is in the Test directory in TestJaggedArray.java. To test your code, type: make testStep 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