Question: Any assistance would be appreciated Task For this assignment, you will be adding a couple of methods to the DynamicList class we developed. Start by
Any assistance would be appreciated
Task
For this assignment, you will be adding a couple of methods to the DynamicList class we developed.
- Start by downloading DynamicList.java which the code is below:
-
// DynamicList.java // a partial implementation of ArrayList public class DynamicList { private Type [] array; private int size; private void resize() { @SuppressWarnings("unchecked") Type [] temp = (Type[]) new Object[size * 2]; for (int i = 0; i = index; i--) { array[i + 1] = array[i]; } array[index] = item; size++; } /** * removes an item from the list by position * @param index the index of the item to be removed */ public void remove(int index) { for (int i = index + 1; i = size) { throw new IndexOutOfBoundsException(); } return array[index]; } /** * resets the list back to an empty one */ public void clear() { size = 0; } /** * gets the number of elements stored in the list * @return the size of the list */ public int size() { return size; } }
- Next add the following ArrayList methods which we omitted:
- void set(int index, Type item) - this method should change an item in the list to the value passed in. It should also throw IndexOutOfBoundsException if the index is not valid.
- int lastIndexOf(Type item) - this method should search the array, but starting at the end of the array and working towards the front. If its not found it should return -1.
- void trimToSize() - this method should resize the array to be exactly large enough to hold the items stored in it. For instance if the capacity is 10, but there are only 6 items, calling trimToSize should resize it to size 6. For testing, you should print the size and capacity after doing the resize.
Testing
You can test your code with the following main class:
public class DynamicListTest { public static void main(String args[]) { DynamicList names = new DynamicList(30); names.add("Alice"); names.add("Bob"); names.add("Claire"); names.add("Dominic"); names.add("Estelle"); names.add("Frank"); names.add("Gwen"); names.add("Hugo"); names.add("Irene"); names.add("Claire"); names.add("Jack");
// test the set method names.set(1, "Billy"); System.out.println(names.get(1)); try { names.set(100, "Slartibartfast"); } catch (IndexOutOfBoundsException e) { System.out.println("Exception thrown correctly"); }
// test the last index method int index = names.lastIndexOf("Claire"); System.out.println("Last index of Claire is " + index);
// call the trimToSize method -- should ensure that works in method names.trimToSize();
// print the final list System.out.println("List contents:"); for (int i = 0; i
It should produce the following:

Billy Exception thrown correctly Last index of Claire is 9 Size = 11 Capacity = 11 List contents: Alice Billy Claire Dominic Estelle Frank Gwen Hugo Irene Claire Jack
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
