Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Given code: import java.lang.AssertionError; public class MyArrayList { / / FIXME add member variables / * * * Construct an MyArrayList with a given initial
Given code:
import java.lang.AssertionError;
public class MyArrayList
FIXME add member variables
Construct an MyArrayList with a given initial length.
@param initialLength The initial length of the array.
public MyArrayList int initialLength we do not need to redeclare the type in the constructor
FIXME
Return the number of elements in the MyArrayList.
@return The number of elements in the MyArrayList.
public int size
return ; FIXME
Add an element to the end of the MyArrayList.
@param element The element to add.
public void addT element
FIXME
Get the element at the specified index.
This function assumes that the index argument is within range of the MyArrayList.
@param index The index to get.
@return The element at the specified index.
public T getint index
return null; FIXME
Remove the element at the specified index.
This function assumes that the index argument is within range of the MyArrayList.
@param index The index to remove.
public void removeint index
FIXME
Double the size of the internal array.
private void resize
FIXME
Create a String representation of the MyArrayList.
@return A String representation of the MyArrayList.
public String toString
String result ;
if thissize
result this.get;
for int i ; i this.size; i
result this.geti;
result ;
return result;
Check that an MyArrayList contains the same elements as an int array.
If the list and the array are not the same, throw an AssertionError.
@param list The MyArrayList to check.
@param answer The expected answer, in the form of an int array.
public static void assertArraysEqualMyArrayList list, int answer
if listsize answer.length
throw new AssertionErrorExpected list of length answer.length but got list.size;
for int i ; i answer.length; i
if Integerlistgeti answeri
throw new AssertionErrorExpected answeri but got list.geti at index i;
Test that the empty arraylist has size
public static void test
MyArrayList list new MyArrayList;
int answer new int;
assertArraysEquallist answer;
Test insertion into an arraylist without resizing
public static void test
MyArrayList list new MyArrayList;
for int i ; i ; i
list.addi i;
int answer ;
assertArraysEquallist answer;
Test deletion from an arraylist without emptying it
public static void test
MyArrayList list new MyArrayList;
for int i ; i ; i
list.addi i;
list.remove;
list.remove;
int answer ;
MyArrayList.assertArraysEquallist answer;
Test deletion from an arraylist and emptying it
public static void test
MyArrayList list new MyArrayList;
for int i ; i ; i
list.addi i;
list.remove;
list.remove;
delete the final remaining numbers
list.remove;
list.remove;
list.remove;
int answer;
MyArrayList.assertArraysEquallist answer;
check that there are no lastelement issues
for int i ; i ; i
list.addi i;
list.remove;
list.add;
int answer;
MyArrayList.assertArraysEquallist answer;
Test insertion into an arraylist with resizing
public static void test
MyArrayList list new MyArrayList;
for int i ; i ; i
list.addi i;
int answer ;
MyArrayList.assertArraysEquallist answer;
Put the MyArrayList through some simple tests.
@param args Ignored command line arguments.
public static void mainString args
test;
test;
test;
test;
test;
System.out.printlnpass;
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