Question
I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think about how to implement KWArrayList class. Please implement the
I hope someone can explain this exercise to me. Thanks
+++++++++++++
Programming Exercise
Try to think about how to implement KWArrayList class. Please implement the following constructor and methods:
public KWArrayList()
public boolean add(E anEntry)
public E get(int index) {
public E set(int index, E newValue)
public E remove(int index)
private void reallocate()
public int size()
public int indexOf(Object item)
Study the code for ArrayList implementation (enclosed in the folder) and work on the following exercise
Provide a constructor for class KWArrayList
2. Implement the indexOf method of the KWArrayList
3.Write statements to remove the middle object from a KWArrayList and place it at the end.
/**
Removes middle object and re-inserts it at the end
@return E object removed and re-inserted
*/
public E removeMiddleInsertEnd()
{
}
4. Please define a tester class to verify your code.
import java.util.Arrays; import java.util.AbstractList; /** * This class implements some of the methods of the Java * ArrayList class. * @author Your Name */ public class KWArrayList extends AbstractList { // Data Fields /** The default initial capacity */ private static final int INITIAL_CAPACITY = 10; /** The underlying data array */ private E[] theData; /** The current size */ private int size = 0; /** The current capacity */ private int capacity = 0; /** * Construct an empty KWArrayList with the default * initial capacity */ public KWArrayList() { capacity = INITIAL_CAPACITY; theData = (E[]) new Object[capacity]; } /*Your programming exercise 1 goes here*/ /** * Construct an empty KWArrayList with a specified initial capacity * @param capacity The initial capacity */ /**/ /** * Appends the specified element to the end of this list. * * @param theEntry - The value to be inserted * @throws ArrayIndexOUtOfBoundsException if index is * less than zero or greater than size */ public boolean add(E anEntry) { if (size == capacity) { reallocate(); } theData[size] = anEntry; size++; return true; } /** * Get a value in the array based on its index. * @param index - The index of the item desired * @return The contents of the array at that index * @throws ArrayIndexOutOfBoundsException - if the index * is negative or if it is greater than or equal to the * current size */ public E get(int index) { if (index < 0 || index >= size) { throw new ArrayIndexOutOfBoundsException(index); } return theData[index]; } /** * Set the value in the array based on its index. * @param index - The index of the item desired * @param newValue - The new value to store at this position * @return The old value at this position * @throws ArrayIndexOutOfBoundsException - if the index * is negative or if it is greater than or equal to the * current size */ public E set(int index, E newValue) { if (index < 0 || index >= size) { throw new ArrayIndexOutOfBoundsException(index); } E oldValue = theData[index]; theData[index] = newValue; return oldValue; } /** * Remove an entry based on its index * @param index - The index of the entry to be removed * @return The value removed * @throws ArrayIndexOutOfBoundsException - if the index * is negative or if it is greater than or equal to the * current size */ public E remove(int index) { if (index < 0 || index >= size) { throw new ArrayIndexOutOfBoundsException(index); } E returnValue = theData[index]; for (int i = index + 1; i < size; i++) { theData[i - 1] = theData[i]; } size--; return returnValue; } /*Your programming exercise 3 goes here*/ /** Removes middle object and re-inserts it at the end @return E object removed and re-inserted */ public E removeMiddleInsertEnd() { } /** * Allocate a new array to hold the directory */ private void reallocate() { capacity = 2 * capacity; theData = Arrays.copyOf(theData, capacity); } /** * Get the current size of the array * @return The current size of the array */ public int size() { return size; } /*Your programming exercise 2 goes here*/ /** * Returns the index of the first occurence of the specified element * in this list, or -1 if this list does not contain the element * @param item The object to search for * @returns The index of the first occurence of the specified item * or -1 if this list does not contain the element */ public int indexOf(Object item) { } /**/ } /**/
Step 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