Question: Objectives This assignment involves implementing some methods of ArrayList data structure in Java. Create a package named lab2 (Note: pay attention to the name, it
Objectives This assignment involves implementing some methods of ArrayList data structure in Java.
Create a package named lab2 (Note: pay attention to the name, it is case sensitive.)
Download MyArrayList.java and TestMyArrayList.java
Import the above two files into package lab2 Understand MyArrayList class Read MyArrayList class in MyArrayList.java. Some methods are already implemented with Javadoc comments provided. Please read code and comments of these methods, fully understand their implementation before starting the next step.
Finish the unimplemented methods The unimplemented methods have been highlighted and marked as TODO in MyArrayList.java. The detailed requirements of each function have been provided in the Javadoc comments. Please read these comments before your coding. Finish the unimplemented methods one by one. Make sure you implement the add(int index, E newValue) function and pass the two test cases testAddElements1() and testAddElements2() in TestMyArrayList.java before coding the next one. Check your Javadocs Click on the class name and the method names of all the elements in your class. Feel free to add/remove/change the comments if needed.
You need to implement and replace the lines that say "TODO Please fill your code here" within the code to do what the methods are calling for.
package lab2; import java.util.Arrays; import java.util.AbstractList; /** * This class implements some of the methods of the Java * ArrayList class. * * */ public class MyArrayListextends AbstractList { // Data Fields /** The default initial capacity */ private static final int INITIAL_CAPACITY = 10; /** The underlying data array */ private E[] data; /** The current size */ private int pos = 0; /** The current capacity */ private int capacity = 0; /** * Construct an empty ArrayList with the default * initial capacity */ public MyArrayList() { capacity = INITIAL_CAPACITY; data = (E[]) new Object[capacity]; } /** * Allocate a new array to hold the directory * */ private void reallocate() { capacity = 2 * capacity; data = Arrays.copyOf(data, capacity); } /** * Add an entry to the end of the list * @param anEntry - The anEntry to be inserted * @return true/false - if the entry is inserted successfully at the end */ public boolean add(E anEntry) { if (pos == capacity) { reallocate(); } data[pos] = anEntry; pos++; 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 >= pos) { throw new ArrayIndexOutOfBoundsException(index); } return data[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 * @throws NullPointerException - if newValue is null */ public E set(int index, E newValue) { if (index < 0 || index >= pos) { throw new ArrayIndexOutOfBoundsException(index); } if(newValue == null) throw new NullPointerException(); E oldValue = data[index]; data[index] = newValue; return oldValue; } /** * Get the current size of the array * @return The current size of the array */ public int size() { return pos; } /** * 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) { for (int i = 0; i < pos; i++) { if (data[i] == null && item == null) { return i; } if (data[i].equals(item)) { return i; } } return -1; } /* * ================================ The following functions need to be filled =================================================== * */ /** * Add an entry to the data inserting it at the specified index. * @param index - The index of the time that the new * value it to be inserted * @param newValue - The value to be inserted * @throws ArrayIndexOUtOfBoundsException if index is * less than zero or greater than size * @throws NullPointerException if newValue is null * */ public void add(int index, E newValue) { //TODO: Please fill your code } /** * Construct an empty ArrayList with a specified initial capacity * @param capacity - The initial capacity * @throws IllegalArgumentException - if the capacity is less 0 */ public MyArrayList(int capacity) { //TODO: Please fill your code } /** * Remove an entry based on its index * @param index - The index of the entry to be removed * @return The Item 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) { //TODO: Please fill your code return null; // please remove this line after filling your code } /** * Count the total number of elements equals to elem * @param theValue - the compared element * @return the total number of replicas or -1 if not found in the list */ public int countApperance(E theValue) { // TODO: please fill your code here. return -1; } /** * Remove all the duplicated elements equals to theValue * @param theValue - the duplicated element to be removed */ public void removeDuplicate(E theValue) { // TODO: please fill your code here. // After calling removeDuplicate() method, only one copy of theValue stored in the list. } /* * ================================ The end of functions need to be filled =================================================== * */ }
HERE IS THE TEST CODE:
/** * This is the Junit Test file for MyArrayList class */ package lab2; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; /** * @author Zheng Li * */ public class TestMyArrayList { private MyArrayList list; @Before public void setUp(){ list = new MyArrayList(); } @Test public void testListInit(){ assertTrue(list.isEmpty()); assertTrue(list.size() == 0); } @Test public void testAddElements1(){ list.add(0, "Karol"); list.add(1, "Vanessa"); list.add(0, "Amanda"); assertTrue(list.size()==3); assertEquals("Amanda", list.get(0)); assertEquals("Karol", list.get(1)); assertEquals("Vanessa", list.get(2)); } @Test public void testAddElements2(){ list.add(0, "Karol"); list.add(1, "Vanessa"); list.add(2, "Amanda"); assertEquals("Karol", list.get(0)); assertEquals("Vanessa", list.get(1)); assertEquals("Amanda", list.get(2)); list.add(1, "Mariana"); assertEquals("Karol", list.get(0)); assertEquals("Mariana", list.get(1)); assertEquals("Vanessa", list.get(2)); assertEquals("Amanda", list.get(3)); assertTrue(list.size()==4); } @Test (expected = IllegalArgumentException.class) public void testInvalidCapacity(){ list = new MyArrayList(-1); } @Test public void testMyArrayListCapacity1(){ list = new MyArrayList(1); list.add(0, "Karol"); list.add(1, "Vanessa"); assertTrue(list.size() == 2); assertEquals("Karol", list.get(0)); } @Test public void testMyArrayListCapacity2(){ list = new MyArrayList(1); list.add(0, "Karol"); list.add(1, "Vanessa"); list.add(0, "jack"); assertTrue(list.size() == 3); assertEquals("Karol", list.get(1)); } @Test (expected = NullPointerException.class) public void testAddElementNull(){ list.add(0, null); } @Test public void testRemoveElement1(){ list.add(0, "Karol"); list.add(1, "Vanessa"); list.add(2, "Amanda"); assertEquals("Amanda", list.remove(2)); assertTrue(list.size() == 2); } @Test public void testRemoveElement2(){ list.add(0, "Karol"); list.add(1, "Vanessa"); list.add(2, "Amanda"); list.remove(0); assertEquals("Vanessa", list.get(0)); } @Test (expected = IndexOutOfBoundsException.class) public void testRemoveWithEmptyList(){ list.remove(0); } @Test public void testIndexof1(){ list.add(0, "Karol"); list.add(1, "Vanessa"); list.add(2, "Amanda"); assertEquals(2, list.indexOf("Amanda")); assertEquals(0, list.indexOf("Karol")); assertEquals(-1, list.indexOf("jack")); } @Test public void testIndexof2(){ list.add(0, "Karol"); list.add(1, "Vanessa"); list.add(2, "Amanda"); list.add(3, "Karol"); assertEquals(0, list.indexOf("Karol")); } @Test public void testcountApperance(){ list.add(0, "Karol"); list.add(1, "Vanessa"); list.add(2, "Karol"); list.add(3, "Karol"); assertEquals(3, list.countApperance("Karol")); } @Test public void testremoveDuplicate(){ list.add(0, "Karol"); list.add(1, "Karol"); list.add(2, "Amanda"); list.add(3, "Karol"); list.removeDuplicate("Karol"); assertEquals(1, list.countApperance("Karol")); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
