Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.LinkedList; /** * Represent a JaggedArray of characters in n bins */ public class JaggedArray { // Unpacked representation private LinkedList [] unpackedValues; //

image text in transcribed

import java.util.LinkedList;

/**

* Represent a JaggedArray of characters in n bins

*/

public class JaggedArray {

// Unpacked representation

private LinkedList[] unpackedValues;

// 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[] getUnpackedValues() {

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 test

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

Oracle RMAN For Absolute Beginners

Authors: Darl Kuhn

1st Edition

1484207637, 9781484207635

More Books

Students also viewed these Databases questions

Question

Simplify. - 4 ( y + 3 ) + 6 y

Answered: 1 week ago

Question

How does version history act as a control for data files?

Answered: 1 week ago