The goal of this assignment is to improve a class that represents a dynamic array - an array that can change size.
To get started, make an a6 package and add files fromthisfolder(Links to an external site.)
(Links to an external site.)to that package. This package should have three files:DynamicArray.java, DynamicArray2.java, and SpeedTest.java.
Warning - it is important for this assignment to carefully review the material from lectures 21 and 22.
Part 1 — DynamicArray2
Following the example from lecture 22 (the recitation), you are to implement a more efficient version of the dynamic array than we did in lecture 21. A more complete version of the lec21 class is provided for you in DynamicArray.java for your reference. (You are not expected to modify theDynamicArrayclass). You should understand how the DA class works before working on your own version.
DynamicArray2.javahas been started for you. Finish the implementation as indicated by the comments. The main difference is that this version of the dynamic array does NOTcreate a new array each time add is called nor copy the array elements during an add except as needed.
Instead, when theaddmethod is called, use the extra spaces in the backing store array to efficiently grow the array. If the backing store array is full, make a new backing store array twice the length of the old backing store array and copy the contents of the backing store array over to this new, longer array.
A new variable, virtualArrayLength, stores how much of this new array is actually used such that a valid index for the dynamic array is0 ≤index
Note that the creation of a new array and the copying of array contents will happen infrequently, andNOT every time the addmethod is called. Also, the backing store array should never decrease in length (although virtualArrayLength may decrease).A solution that does not use this representation strategy will receiveno credit
. You may not use a java Collection class to handle the internal representation of this class, only use a Java primitive array for the backing store, as is done in the example DynamicArray class.
Document theDyanmicArray2class and all of its methods with Javadoc comments. (I only provided // style comments - you should put them in Javadoc form and modify as needed). Overall, the finished class should have good programming style.
In main, write code that uses the DynamicArray2 class and tests its various methods. Read the comment there for more details. There will only be minimal autograder testing provided before submission. Make sure you write enough code in main and carefully examine the changes to the array to be confident that your code is behaving correctly.
Part 2 — Speed Test
The belief that the second DynamicArray yields better performance should be tested. The SpeedTest class has a small program to collect the time to add N elements to an empty array of each type. Perform an experiment with several values of N and collect the timing data. Write a short document which includes a table of results, a graph comparing the two dynamic array classes, and has a paragraph discussing trends you see. It is not enough to just say one is slower than the other. Save the report as Report.pdf.