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...

1 answer below »

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.

Answered Same DayOct 19, 2021

Answer To: The goal of this assignment is to improve a class that represents a dynamic array - an array that...

Shivani answered on Oct 21 2021
139 Votes
/**
* A DynamicArray2 behaves like an array of strings, except that it can grow and
* shrink. It is indexed beginning with zero. When a Dynami
cArray2 is created,
* it is empty. Methods are provided to report on the size, add, get, set, and
* remove elements.
*
* @author Prof. David E. Johnson
*
*/
public class DynamicArray2 {
    private String[] data; // the backing array
    private int virtualArrayLength; // the number of elements in the dynamic array
    /**
     * Creates an empty dynamic array with room to grow. (DO NOT modify this
     * method.)
     */
    public DynamicArray2() {
        // Start with a few available extra spaces. Do not change this.
        data = new String[8];
        // But the virtual array is empty.
        virtualArrayLength = 0;
    }
    /**
     * Returns the number of elements in the dynamic array.
     *
     * @return the number of elements
     */
    public int size() {
        // This is not the length of the backing array. It is the
        // number of elements used by the virtual array.
        return virtualArrayLength;
    }
    // Appends s to the end of the dynamic array at index this.size().
    public void add(String s)
    {
        add(virtualArrayLength,s);
    }
    // Throws an IndexOutOfBoundsException if index is not a valid index
    // for adding to the dynamic array, otherwise inserts s at index.
    // Elements can be added from index 0 up to and including this.size().
    //
    // @param index, s index is the int index at which the string s...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here