Many multiple choice and free response questions on the AP Computer Science Exam require manipulation of arrays and ArrayList
objects. The MyArrayList
practice problem is intended primarily as practice with arrays, though it also exposes studnets to ArrayList
behavior.
The project requires insertion into and deletion from a partially filled array. It also requires traversal of the array to find and remove an element that may or may not be present. The project requires that new arrays be declared, and elements be copied, to change the capacity.
MyArrayList
instructions
The MyArrayList
project involves implementing a subset of the methods of the java.util.ArrayList
class in the Java library. Each method should behave exactly as corresponding method in the ArrayList
class.
Documentation for the ArrayList class and its method can be found at:
ArrayList documentation JDK 17
The skeleton code for MyArrayList
is available as both Java files and as text on this page. Documentation for each method has been omitted, since the methods are intended to behave exactly as in the documentation above.
Throwing exceptions is not on the AP CS A Exam. Understanding the meaning of some exceptions is on the AP CS A Exam. In MyArrayList
, the code to throw the required exceptions has been included in the skeleton code. Writing the condition under which each exception is thrown is part of the project.
Testing MyArrayList
Two automated tests have been provided below, each as a Java file. See Running JUnit 5 tests.
MyArrayListAutomatedTest
tests selected methods of MyArrayList
with prewritten tests. These tests can be quickly repeated as often as desired. To the extent possible, the each test tests only a single MyArrayList
method. The tests use package access methods of MyArrayList
to directly manipulate MyArrayList
instance variables. These package access methods are only in the Java file version (not the text version below).
MyArrayListAutomatedTestAgainstOracle
tests MyArrayList
by running a randomly selected sequence of methods on both a MyArrayList
object and a java.util.ArrayList
object. MyArrayListAutomatedTestAgainstOracle
compares the externally observable results of each method call. If the results differ, the test fails. The OUTPUT_METHOD_CALLS
constant can be set to true
to output the entire sequence of method calls made before the first detected failure. Since MyArrayListAutomatedTestAgainstOracle
calls a random sequence of methods each time it is run, past tests are not easily repeatable.
The instance variables in MyArrayList
are package access (no private
modifier) to enable clearer test code.
MyArrayList
skeleton code
MyArrayList.java
MyArrayListAutomatedTest.java
MyArrayListAutomatedTestAgainstOracle.java
public class MyArrayList<E>
{
private Object[] a;
private int size;
public MyArrayList()
{
// TODO: implement
}
public MyArrayList(int initialCapacity)
{
if (true) // TODO: replace this with the correct condition
throw new IllegalArgumentException();
// TODO: implement
}
public int size()
{
return -1; // TODO: implement
}
public E get(int index)
{
if (true) // TODO: replace this with the correct condition
throw new IndexOutOfBoundsException();
// Note: You must cast the reference from the array to type E.
// For example, to return element 6 from a: return (E) a[6];
return null; // TODO: implement
}
public E set(int index, E element)
{
if (true) // TODO: replace this with the correct condition
throw new IndexOutOfBoundsException();
return null; // TODO: implement
}
public boolean contains(Object elem)
{
return false; // TODO: implement
}
public void trimToSize()
{
// TODO: implement
}
public void add(int index, E element)
{
if (true) // TODO: replace this with the correct condition
throw new IndexOutOfBoundsException();
// TODO: implement
}
public boolean add(E elem)
{
return false; // TODO: implement
}
public E remove(int index)
{
if (true) // TODO: replace this with the correct condition
throw new IndexOutOfBoundsException();
return null; // TODO: implement
}
public boolean remove(Object elem)
{
return false; // TODO: implement
}
}
Solution & comments
See the MyArrayList solution or review it with AP CS Tutor Brandon Horn.