Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help. Create a new class ExpandableArray, based on the ExpandableIntArray

ID: 3565704 • Letter: P

Question

Please help. Create a new class ExpandableArray, based on the ExpandableIntArray class below, that uses a generic type to determine what is stored in the array.

public class ExpandableIntArray {
private int[] a;
private int size;
private final int INITIAL_SIZE = 5;
private int count = 0;

ExpandableIntArray() {
a = new int[INITIAL_SIZE];
size = INITIAL_SIZE;
}

public void addToEnd(int i) {
if (count == a.length)
extend();
a[count] = i;
++count;
}

public int size() {
return count;
}

public boolean isEmpty() {
return count == 0;
}

public int get(int i) {
return a[i];
}

public int set(int i, int k) {
int t = a[i];
a[i] = k;
return t;
}
  
private void extend() {
int[] b = new int[10 + a.length];
for (int i = 0; i < a.length; ++i)
b[i] = a[i];
a = b;
}

public String toString() {
String s = "[";
for (int i = 0; i < count; i++) {
if (i > 0)
s += ", "; // separate entries by commas
s += a[i];
}
return s + "]";
}

public void add(int i, int k)
{

if(count == a.length)
{
extend();
}
for(int j = a.length - 1; j > i; j--) {
a[j] = a[j - 1];
}
a[i] = k;

++count;
}

public void remove(int i)
{
for(int j = i + 1; j < a.length; j++)
{
a[j-1] = a[j];
}

--count;
}

}

Explanation / Answer

So, rather than having an array that just stores ints, we want it to be able to store data of any type. You can use java's generic types, which are fully explained here: http://docs.oracle.com/javase/tutorial/java/generics/types.html. Basically, use a type parameter "T" (public class ExpandableArray<T>....) and then replace anywhere we see "int" being used to manipulate the data of the array by a cast to T (ints are still used to index the array). I used unchecked casts from Object to the type of the array (weak typing). I've also put the code here: http://pastebin.com/1T6KbCdg

public class ExpandableArray<T> {
    private Object[] a;
    private int size;
    private final int INITIAL_SIZE = 5;
    private int count = 0;

    ExpandableArray() {
        a = new Object[INITIAL_SIZE];
        size = INITIAL_SIZE;
    }

    public void addToEnd(T i) {
        if (count == a.length)
            extend();
        a[count] = i;
        ++count;
    }

    public int size() {
        return count;
    }

    public boolean isEmpty() {
        return count == 0;
    }

    public T get(int i) {
        return (T)a[i];
    }

    public T set(int i, T k) {
        T t = (T)a[i];
        a[i] = k;
        return t;
    }

    private void extend() {
        Object[] b = new Object[10 + a.length];
        for (int i = 0; i < a.length; ++i)
            b[i] = a[i];
        a = b;
    }

    public String toString() {
        String s = "[";
        for (int i = 0; i < count; i++) {
            if (i > 0)
                s += ", "; // separate entries by commas
            s += ((T)a[i]).toString();
        }
        return s + "]";
    }

    public void add(int i, T k)
    {
        if(count == a.length)
        {
            extend();
        }
        for(int j = a.length - 1; j > i; j--) {
            a[j] = a[j - 1];
        }
        a[i] = k;

        ++count;
    }

    public void remove(int i)
    {
        for(int j = i + 1; j < a.length; j++)
        {
            a[j-1] = a[j];
        }

        --count;
    }

}

Additionally, here is a program you can use to test the ExpandableArray class:
/*
* Test the methods of ExpandableArray: addToEnd, size, isEmpty, get, set, toString, add, remove
*/
public class TestExpandableArray {
    public static void main(String[] args) {
        System.out.println("Testing ExpandableArray storing strings:");
        ExpandableArray<String> sArray = new ExpandableArray<String>();
        sArray.addToEnd("Hello");
        sArray.addToEnd("World");
        //ExpandableArray is 0-indexed so the nth element has index n-1
        sArray.add(1, "remove me");
        System.out.println("sArray = " + sArray.toString());
        sArray.remove(1);
        System.out.println("sArray = " + sArray.toString());
        System.out.println("Size of sArray = " + sArray.size());
        System.out.println("sArray is " + (sArray.isEmpty() ? "" : "not ") + "empty.");
      
        System.out.println(" Testing ExpandableArray storing integers:");
        ExpandableArray<Integer> iArray = new ExpandableArray<Integer>();
        // notice the array will automatically expand after 10 and 20 elements
        for (int k = 1; k <= 25; ++k) {
            iArray.addToEnd(k);
        }
        System.out.println("iArray has " + iArray.size() + " elements: " + iArray.toString());
        System.out.println("The 14th element of iArray is " + iArray.get(13));
        iArray.set(13, 40);
        System.out.println("After calling set(), the 14th element of iArray is " + iArray.get(13));
        System.out.println("iArray = " + iArray.toString());
    }
}