Assignment 7
due Thursday, March 14 at 11:59pm on Canvas

Lab 8: Generics

due Monday, March 11 at 9:20am on Canvas

Pair Programming Required (Or No Credit)

  • Both partners fill in, sign, date, and submit the pair programming contract
  • Upload the document(s) along with your Lab to Canvas.
  • Only ONE partner submits the lab assignment on Canvas. Please make sure both your names are on the source file(s).

ListArray Lab: The Final Index (100 pts)

Part 1: Writing a Generic List
  • This lab will give you an even better peek under the hood of the ArrayList class, by asking you to update your ListArray class from Lab 5 to make it a generic class, that also throws exceptions for certain error conditions
  • Note that ArrayList is a generic class, that we have been using, even before studying generics.
  • Thus, we will no longer just be storing Strings in the ListArray, but should now be able to store any Object type of data (no primitives).
  • Although our class is generic, arrays of generic types are not allowed in Java.
  • Therefore, we will need to store an array of Objects and cast to the generic type E when needed (such as when returning an element stored in the ArrayList).
  • The logic of your methods should remain essentially unchanged.
  • For example, here should be your default constructor:
      public ListArray() {
        array = new Object[10]; //update to be type Object rather than String
        numElements = 0;
       
    }

  • The tricky part of this lab will be to make updates based on the generic type and to know when to cast from type Object to type E.
  • For example:
    @SuppressWarnings("unchecked")
    public E get(int index){
        if (index >= numElements) {
            System.out.println(
                    "Error: Cannot get element at index " + index + "."
                    + "\nOutside bounds of list array.\nIndex: "
                            + index + ", Size: " + numElements);
            return null; //end the method
        }
        return (E) array[index]; //must cast from Object to E to return type E
    }

  • Note: Generic code can generate warnings when casting to a generic type (as in the above method).
    • There is no way for the compiler to verify that the types are compatible, when the type is unknown
    • We will suppress the warnings, using @SuppressWarnings("unchecked"), as we are confident that no runtime errors will occur
    • I have added the tags for you in the starter code below in any case that they are required
  • Copy and paste the following starter code into a new class file called ListArray.java:
/**
 * ListArray.java
 * @author
 * @author
 * CIS 36B
 */

public class ListArray<E> {
    private Object[] array;
    private int numElements;
   
    /**Constructors*/
   
    /**
     * Default constructor for ListArray
     * Creates an empty array of length 10
     * Sets numElements to 0
     */
    public ListArray() {
       
    }
   
    /**
     * Constructor for ListArray
     * Creates an empty array of length size
     * Sets numElements to 0
     * @param size the initial size of the
     * ListArray
     */
    public ListArray(int size) {
       
    }
   
    /**
     * Copy constructor for ListArray
     * Creates a new list array of the
     * same size as the ListArray passed
     * in as a parameter, and copies the
     * parameter's data into the new
     * list array using a for loop
     * Also sets the numElements to be
     * the same as the parameter's
     * numElements
     * @param la the ListArray to copy
     */
    @SuppressWarnings("unchecked")
    public ListArray(ListArray<E> la) {
       
    }
   
    /**Accessor Methods*/
   
    /**
     * Returns whether the list array is
     * currently empty
     * @return whether the list array is
     * empty
     */
    public boolean isEmpty() {
        return false;
    }
   
    /**
     * Returns the current number of
     * elements stored in the list
     * array
     * @return the number of elements
     */
    public int size() {
        return -1;
    }
   
    /**
     * Returns the element at the specified
     * index. Prints an error message and
     * returns null if index is
     * larger than or equal to numElements
     * @param index the index of the element
     * to access
     * @return the element at index
     */
    @SuppressWarnings("unchecked")
    public E get(int index) {
        return null;
    }
   
    /**
     * Uses the linearSearch algorithm to
     * locate an element in the list array
     * @param element the element to locate
     * @return whether or not the element
     * is in the list array
     */
    public boolean contains(E element) {
       
        return false;
    }
   
    /**
     * Uses the linearSearch algorithm to
     * locate an element in the list array
     * @param element the element to locate
     * @return the location of the element
     * or -1 if the element is not in the
     * list array
     */
    public int indexOf(E element) {
       
        return -1;
    }
   
    /**
     * Determines whether the list array
     * is currently full
     * @return whether the list array
     * is at maximum capacity
     */
    private boolean atCapacity() {
        return false;
    }
   
    /**
     * Creates a String of all elements,
     * with [] around the elements,
     * each element separated from the next
     * with a comma
     */
    @Override public String toString() {
        String result = "[";
        return result + "]";
    }
   
   
    /**Mutator Methods*/
   
    /**
     * Resizes the list array by making a new
     * array that has a length (capacity)
     * 10 larger than the current array's
     * length (capacity)
     */
    private void reSize() {
       
    }
   
    /**
     * Inserts a new element to the end
     * of the list array.
     * Resizes the list array if it is
     * at capacity before inserting
     * @param element the element to insert
     */
    public void add(E element) {
       
    }
   
    /**
     * Inserts a new element at the specified
     * index in the list array.
     * Resizes the list array if it is
     * at capacity before inserting
     * @param index the index at which to insert
     * @param element the element to insert
     */
    public void add(int index, E element) {
       
    }
   
    /**
     * Assigns a new value to the list array
     * at a specified index
     * @param index the index at which to update
     * @param element the new element
     */
    public void set(int index, E element) {
       
    }
   
    /**
     * Removes an element at a specified index in
     * the list array
     * @param index the index at which to remove
     * @return the element that was removed
     */
    @SuppressWarnings("unchecked")
    public E remove(int index) {
       
        return null;
       
    }
   
    /**
     * Removes the first instance of the specified
     * element in the list array
     * @param element the element to remove
     * @return whether the element was successfully
     * removed
     */
    public boolean remove(E element) {
       
        return false;   
    }
   

/**
* Properly overrides Object equals
* Returns whether two ListArrays and
* the same length and store
* the same data in the same order
*/
@SuppressWarnings("unchecked")
@Override public boolean equals(Object o) {
    return false;
        }
}
  • Please note the changes in the test file below. We need to use <> any time the constuctor is called in order to define the type of data to be stored in the ListArray.
  • For example:
ListArray<String> test1 = new ListArray<String>(); //could also use the diamond <>
//= new ListArray<>();

ListArray<Integer> test4 = new ListArray<Integer>();



ListArrayTest.java File


**
 * ListArrayTest.java
 * @author
 * @author
 * CIS 36B
 */

public class ListArrayTest {
    public static void main(String[] args) {
        System.out.println("***Testing Default Constructor and One Parameter Constructor***\n");
        ListArray<String> test1 = new ListArray<String>();
       
        System.out.println("After default constructor called:");
        System.out.println("Should print size of 0: "
                + test1.size());
        System.out.println("Should print the list array is empty - true: "
                + test1.isEmpty());
        System.out.println("Should print []: " + test1);
       
        ListArray<String> test2 = new ListArray<String>(15);
        System.out.println("\nAfter one parameter constructor called:");
        System.out.println("\nShould print size of 0: "
                + test2.size());
        System.out.println("Should print the list array is empty - true: "
                + test2.isEmpty());
        System.out.println("Should print []: " + test2);
       
        System.out.println("\n***Testing Add Methods***\n");
        test1.add("B");
        System.out.println("Adding B: ");
        System.out.println("Should print size of 1: "
                + test1.size());
        System.out.println("Should print the list array is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print B at index 0: "
                + test1.get(0));
        System.out.println("Should print [B]: " + test1);
       
        test1.add("D");
        System.out.println("\nAdding D: ");
        System.out.println("Should print size of 2: "
                + test1.size());
        System.out.println("Should print the list array is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print B at index 0: "
                + test1.get(0));
        System.out.println("Should print D at index 1: "
                + test1.get(1));
        System.out.println("Should print [B, D]: "
                + test1);
       
        System.out.println("\nShould print error message. Cannot add at index 10:");
        test1.add(10, "A");
   
        test1.add(0, "A");
        System.out.println("\nAdding A at index 0: ");
        System.out.println("Should print size of 3: "
                + test1.size());
        System.out.println("Should print the list array is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print A at index 0: "
                + test1.get(0));
        System.out.println("Should print B at index 1: "
                + test1.get(1));
        System.out.println("Should print [A, B, D]: " + test1);
       
        test1.add(2, "C");
        System.out.println("\nAdding C at index 2: ");
        System.out.println("Should print size of 4: "
                + test1.size());
        System.out.println("Should print the list array is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print A at index 0: "
                + test1.get(0));
        System.out.println("Should print C at index 2: "
                + test1.get(2));
        System.out.println("Should print [A, B, C, D]: " + test1);
       
        System.out.println("\nAdding 1-20 in ListArray of size 15. Should resize to 20:");
        for(int i = 1; i <= 20; i++) {
            test2.add("" + i);
        }
        System.out.println("Should print size of 20: "
                + test2.size());
        System.out.println("Should print the list array is empty - false: "
                + test2.isEmpty());
        System.out.println("Should print 1 at index 0: "
                + test2.get(0));
        System.out.println("Should print 20 at index 19: "
                + test2.get(19));
       
        System.out.println("\n***Testing Copy Constructor***\n");
        ListArray<String> test3 = new ListArray<String>(test1);
        System.out.println("Should print size of 4: "
                + test1.size());
        System.out.println("Should print the list array is empty - false: "
                + test3.isEmpty());
        System.out.println("Should print A at index 0: "
                + test3.get(0));
        System.out.println("Should print C at index 2: "
                + test3.get(2));
        System.out.println("Should print [A, B, C, D]: " + test3);
       
        System.out.println("\nTesting for deep copy: ");
        test3.add("A");
        test3.add("B");
        test3.add("C");
        System.out.println("Original list array size should be 4: "
                + test1.size());
        System.out.println("New list array size should be 7: "
                + test3.size());
        System.out.println("Printing original [A, B, C, D]: " + test1);
        System.out.println("Printing new [A, B, C, D, A, B, C]: " + test3);
       
        System.out.println("\n***Testing Set Method***\n");
        System.out.println("Should print error message when setting index 6: ");
        test1.set(6, "Z");
       
        test1.set(2, "Z");
        System.out.println("\nShould print size of 4: "
                + test1.size());
        System.out.println("Should print the list array is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print A at index 0: "
                + test1.get(0));
        System.out.println("Should print Z at index 2: "
                + test1.get(2));
        System.out.println("Should print [A, B, Z, D]: " + test1);
       
        test1.set(0, "Z");
        System.out.println("\nShould print size of 4: "
                + test1.size());
        System.out.println("Should print the list array is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print Z at index 0: "
                + test1.get(0));
        System.out.println("Should print Z at index 2: "
                + test1.get(2));
        System.out.println("Should print [Z, B, Z, D]: " + test1);
       
        System.out.println("\n***Testing Remove Methods***\n");
       
        System.out.println("Remove index 3, element D: "
                + test3.remove(3));
        System.out.println("Should print size of 6: "
                + test3.size());
        System.out.println("Should print the list array is empty - false: "
                + test3.isEmpty());
        System.out.println("Should print A at index 0: "
                + test3.get(0));
        System.out.println("Should print A at index 3: "
                + test3.get(3));
        System.out.println("Should print [A, B, C, A, B, C]: " + test3);
       
        System.out.println("\nRemove index 0, element A: "
                + test3.remove(0));
        System.out.println("Should print size of 5: "
                + test3.size());
        System.out.println("Should print the list array is empty - false: "
                + test3.isEmpty());
        System.out.println("Should print B at index 0: "
                + test3.get(0));
        System.out.println("Should print B at index 3: "
                + test3.get(3));
        System.out.println("Should print [B, C, A, B, C]: " + test3);
       
        System.out.println("\nRemove index 10, should print error message: ");
        test3.remove(10);
        System.out.println("Should print size of 5: "
                + test3.size());
        System.out.println("Should print the list array is empty - false: "
                + test3.isEmpty());
        System.out.println("Should print B at index 0: "
                + test3.get(0));
        System.out.println("Should print B at index 3: "
                + test3.get(3));
        System.out.println("Should print [B, C, A, B, C]: " + test3);
       
        System.out.println("\nRemove index of size - 1 (last element) C: "
                + test3.remove(test3.size() - 1));
        System.out.println("Should print size of 4: "
                + test3.size());
        System.out.println("Should print the list array is empty - false: "
                + test3.isEmpty());
        System.out.println("Should print B at index 0: "
                + test3.get(0));
        System.out.println("Should print B at index size - 1: "
                + test3.get(test3.size() - 1));
        System.out.println("Should print [B, C, A, B]: " + test3);
       
        System.out.println("\nCan remove B (first instance): "
                + test3.remove("B"));
        System.out.println("Should print size of 3: "
                + test3.size());
        System.out.println("Should print the list array is empty - false: "
                + test3.isEmpty());
        System.out.println("Should print C at index 0: "
                + test3.get(0));
        System.out.println("Should print B at index size - 1: "
                + test3.get(test3.size() - 1));
        System.out.println("Should print [C, A, B]: " + test3);
       
        System.out.println("\nCan remove B (only instance): "
                + test3.remove("B"));
        System.out.println("Should print size of 2: "
                + test3.size());
        System.out.println("Should print the list array is empty - false: "
                + test3.isEmpty());
        System.out.println("Should print C at index 0: "
                + test3.get(0));
        System.out.println("Should print A at index size - 1: "
                + test3.get(test3.size() - 1));
        System.out.println("Should print [C, A]: " + test3);
       
        System.out.println("\nShould print error message: ");
        System.out.println("Cannot remove Z (not stored): "
                + test3.remove("Z"));
        System.out.println("Should print size of 2: "
                + test3.size());
        System.out.println("Should print the list array is empty - false: "
                + test3.isEmpty());
        System.out.println("Should print C at index 0: "
                + test3.get(0));
        System.out.println("Should print A at index size - 1: "
                + test3.get(test3.size() - 1));
        System.out.println("Should print [C, A]: " + test3);
       
        test3.remove("C");
        System.out.println("\nRemove C:");
        System.out.println("Should print size of 1: "
                + test3.size());
        System.out.println("Should print the list array is empty - false: "
                    + test3.isEmpty());
        System.out.println("Should print A at index 1: "
                        + test3.get(0));
        System.out.println("Should print A at index size - 1: "
                        + test3.get(test3.size() - 1));
        System.out.println("Should print [A]: " + test3);
       
        test3.remove("A");
        System.out.println("\nRemove A:");
        System.out.println("Should print size of 0: "
                + test3.size());
        System.out.println("Should print the list array is empty - true: "
                + test3.isEmpty());
        System.out.println("Should print []: " + test3);
       
        System.out.println("\n***Testing Size Method***\n");
        System.out.println("Should print size of 0: "
                + test3.size());
        System.out.println("Should print size of 20: "
                + test2.size());
        System.out.println("Should print size of 4: "
                + test1.size());
       
        System.out.println("\n***Testing IsEmpty Method***\n");
        System.out.println("Should print true: "
                + test3.isEmpty());
        System.out.println("Should print false: "
                + test2.isEmpty());
        System.out.println("Should print false: "
                + test1.isEmpty());
       
        System.out.println("\n***Testing Contains Method***\n");
        System.out.println("Contains Z, should print true: "
                + test1.contains(new String("Z")));
        System.out.println("Contains D, should print true: "
                + test1.contains("D"));
        System.out.println("Contains A, should print false: "
                + test1.contains("A"));
       
        System.out.println("\n***Testing IndexOf Method***\n");
        System.out.println("Index of Z, should print 0: "
                + test1.indexOf("Z"));
        System.out.println("Index of D, should print 3: "
                + test1.indexOf("D"));
        System.out.println("Index of A, should print -1: "
                + test1.indexOf("A"));
       
        System.out.println("\n***Testing Get Method***\n");
        System.out.println("Element at 0, should print Z: "
                + test1.get(0));
        System.out.println("Element at 3, should print D: "
                + test1.get(3));
        System.out.println("Element at 6, should print error message: ");
        test1.get(6);
       
        System.out.println("\n***Testing Equals Method***\n");
        ListArray<Integer> test4 = new ListArray<Integer>();
        test4.add(new Integer(1));
        test4.add(2);
        test4.add(3);
        ListArray<Integer> test5 = new ListArray<Integer>();
        test5.add(1);
        test5.add(2);
        test5.add(3);
        System.out.println("Should print ListArrays are equal (true): " + test4.equals(test5));
        test5.set(2, 4);
        System.out.println("Should print ListArrays are not equal (false): " + test4.equals(test5));
        test5.remove(2);
        System.out.println("Should print ListArrays are not equal (false): " + test4.equals(test5));
       
       
    }
}


Required Output:

***Testing Default Constructor and One Parameter Constructor***

After default constructor called:
Should print size of 0: 0
Should print the list array is empty - true: true
Should print []: []

After one parameter constructor called:

Should print size of 0: 0
Should print the list array is empty - true: true
Should print []: []

***Testing Add Methods***

Adding B:
Should print size of 1: 1
Should print the list array is empty - false: false
Should print B at index 0: B
Should print [B]: [B]

Adding D:
Should print size of 2: 2
Should print the list array is empty - false: false
Should print B at index 0: B
Should print D at index 1: D
Should print [B, D]: [B, D]

Should print error message. Cannot add at index 10:
Error: Cannot add element A at index 10.
Index is outside the bounds of the list array.
Index: 10, Size: 2

Adding A at index 0:
Should print size of 3: 3
Should print the list array is empty - false: false
Should print A at index 0: A
Should print B at index 1: B
Should print [A, B, D]: [A, B, D]

Adding C at index 2:
Should print size of 4: 4
Should print the list array is empty - false: false
Should print A at index 0: A
Should print C at index 2: C
Should print [A, B, C, D]: [A, B, C, D]

Adding 1-20 in ListArray of size 15. Should resize to 20:
Should print size of 20: 20
Should print the list array is empty - false: false
Should print 1 at index 0: 1
Should print 20 at index 19: 20

***Testing Copy Constructor***

Should print size of 4: 4
Should print the list array is empty - false: false
Should print A at index 0: A
Should print C at index 2: C
Should print [A, B, C, D]: [A, B, C, D]

Testing for deep copy:
Original list array size should be 4: 4
New list array size should be 7: 7
Printing original [A, B, C, D]: [A, B, C, D]
Printing new [A, B, C, D, A, B, C]: [A, B, C, D, A, B, C]

***Testing Set Method***

Should print error message when setting index 6:
Error: Cannot set at index outside bounds of list array.
Index: 6 , Size: 4

Should print size of 4: 4
Should print the list array is empty - false: false
Should print A at index 0: A
Should print Z at index 2: Z
Should print [A, B, Z, D]: [A, B, Z, D]

Should print size of 4: 4
Should print the list array is empty - false: false
Should print Z at index 0: Z
Should print Z at index 2: Z
Should print [Z, B, Z, D]: [Z, B, Z, D]

***Testing Remove Methods***

Remove index 3, element D: D
Should print size of 6: 6
Should print the list array is empty - false: false
Should print A at index 0: A
Should print A at index 3: A
Should print [A, B, C, A, B, C]: [A, B, C, A, B, C]

Remove index 0, element A: A
Should print size of 5: 5
Should print the list array is empty - false: false
Should print B at index 0: B
Should print B at index 3: B
Should print [B, C, A, B, C]: [B, C, A, B, C]

Remove index 10, should print error message:
Error: Cannot remove element at index 10. Outside bounds of list array.
Should print size of 5: 5
Should print the list array is empty - false: false
Should print B at index 0: B
Should print B at index 3: B
Should print [B, C, A, B, C]: [B, C, A, B, C]

Remove index of size - 1 (last element) C: C
Should print size of 4: 4
Should print the list array is empty - false: false
Should print B at index 0: B
Should print B at index size - 1: B
Should print [B, C, A, B]: [B, C, A, B]

Can remove B (first instance): true
Should print size of 3: 3
Should print the list array is empty - false: false
Should print C at index 0: C
Should print B at index size - 1: B
Should print [C, A, B]: [C, A, B]

Can remove B (only instance): true
Should print size of 2: 2
Should print the list array is empty - false: false
Should print C at index 0: C
Should print A at index size - 1: A
Should print [C, A]: [C, A]

Should print error message:
Error: Cannot remove element Z. No such element in the list array.
Cannot remove Z (not stored): false
Should print size of 2: 2
Should print the list array is empty - false: false
Should print C at index 0: C
Should print A at index size - 1: A
Should print [C, A]: [C, A]

Remove C:
Should print size of 1: 1
Should print the list array is empty - false: false
Should print A at index 1: A
Should print A at index size - 1: A
Should print [A]: [A]

Remove A:
Should print size of 0: 0
Should print the list array is empty - true: true
Should print []: []

***Testing Size Method***

Should print size of 0: 0
Should print size of 20: 20
Should print size of 4: 4

***Testing IsEmpty Method***

Should print true: true
Should print false: false
Should print false: false

***Testing Contains Method***

Contains Z, should print true: true
Contains D, should print true: true
Contains A, should print false: false

***Testing IndexOf Method***

Index of Z, should print 0: 0
Index of D, should print 3: 3
Index of A, should print -1: -1

***Testing Get Method***

Element at 0, should print Z: Z
Element at 3, should print D: D
Element at 6, should print error message:
Error: Cannot get element at index 6.
Outside bounds of list array.
Index: 6, Size: 4

***Testing Equals Method***

Should print ListArrays are equal (true): true
Should print ListArrays are not equal (false): false
Should print ListArrays are not equal (false): false


What to Submit:

  • Submit your completed ListArray.java file (make sure your name(s) are on both files) to Canvas.
  • Submit the pair programming worksheet to Canvas along with your Lab.
  • One partner submits ListArray.java and the pair programming contract.
    • The other other partner only submits the pair programming contract.

How You Will Be Graded:

  • You will receive 6.5 points for each correctly written method (15 methods)
  • Note: If you lost points on Lab 5 for any of your methods (including due to incorrect wording of error messages), please correct those methods now to avoid loss of points on this lab.
  • -5 for any package statements - please remove package statements before submitting
  • -5 for missing pair programming contract
  • -5 for incorrect indentation. Please go to Source->Format to format your code before submitting.
Pair Programming Extra Credit Opportunity (1 pt)
  • Both partners fill in, sign, date, and submit the pair programming contract
  • Upload the document(s) along with your source code files to Canvas.
  • Only ONE partner submits the assignment to Canvas. Please make sure both your names are on the files.

ListArray Try Catch (10 pts)
  • Open your ListArray class from your last lab.
  • We are going to make one final improvement to this class - adding in exception handling
  • In the last two versions of the program, you displayed error messages to the console and returned dummy values whenever there was an exceptional condition.
  • The dummy value is used to satisfy the compiler, which requires that a value of type E be returned
  • For example:
    @SuppressWarnings("unchecked")
    public E get(int index) {
        if (index >= numElements) { //exceptional condition
            //error message displayed to console:
            System.out.println("Error: Cannot get element at index " + index + "."
                    + "\nOutside bounds of list array.\nIndex: " + index + ", Size: " + numElements);
            return null; //returning a dummy value
        }
        return (E) array[index];
    }
  • We will throw exceptions to replace the error checking code from Lab 7
  • Note that when you throw an exception, you are not required to return a dummy value, as the execution of the program halts when the exception gets thrown.
  • You will need to update the following methods to throw an IndexOutOfBoundsException:
  1. public E get(int index)
  2. public void add(int index, E element)
  3. public void set(int index, E element)
  4. public E remove(int index)
  • As an example to follow, here is an updated get method:
    @SuppressWarnings("unchecked")
    public E get(int index) throws IndexOutOfBoundsException{
        if (index >= numElements) {
            throw new IndexOutOfBoundsException(
                    "Error: Cannot get element at index " + index + "."
                    + "\nOutside bounds of list array.\nIndex: "
                            + index + ", Size: " + numElements);
        }
        return (E) array[index];
    }
  • Note that the previous error message should go inside the () of the exception when it is thrown.
  • Once you have updated the four methods, you will also need to update the test file.
    • Anywhere that an error message is tested, you will need to place the code inside of a try-catch block.
  • For example:
      try {
      System.out.println("Element at 6, should print error message: ");
      test1.get(6);
  } catch (IndexOutOfBoundsException e) {
      System.out.println(e.getMessage());
  }

  • Since we are just testing that the code works properly (the exception is thrown as expected in this case), and not interacting with a user, we can place these tests inside of a try-catch block with no need for a loop.
    • We used an incorrect index on purpose to test our code!
    • No need to redo with a new index.
  • Notice that we made use of the getMessage() method to print the message thrown by the exception.
  • Here are the parts of the test file that will need to be updated (noted in bold below):
/**
 * ListArrayTest.java
 * @author
 * @author
 * CIS 36B
 */

public class ListArrayTest {
    public static void main(String[] args) {
        System.out.println("***Testing Default Constructor and One Parameter Constructor***\n");
        ListArray<String> test1 = new ListArray<String>();
       
        System.out.println("After default constructor called:");
        System.out.println("Should print size of 0: "
                + test1.size());
        System.out.println("Should print the list array is empty - true: "
                + test1.isEmpty());
        System.out.println("Should print []: " + test1);
       
        ListArray<String> test2 = new ListArray<String>(15);
        System.out.println("\nAfter one parameter constructor called:");
        System.out.println("\nShould print size of 0: "
                + test2.size());
        System.out.println("Should print the list array is empty - true: "
                + test2.isEmpty());
        System.out.println("Should print []: " + test2);
       
        System.out.println("\n***Testing Add Methods***\n");
        test1.add("B");
        System.out.println("Adding B: ");
        System.out.println("Should print size of 1: "
                + test1.size());
        System.out.println("Should print the list array is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print B at index 0: "
                + test1.get(0));
        System.out.println("Should print [B]: " + test1);
       
        test1.add("D");
        System.out.println("\nAdding D: ");
        System.out.println("Should print size of 2: "
                + test1.size());
        System.out.println("Should print the list array is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print B at index 0: "
                + test1.get(0));
        System.out.println("Should print D at index 1: "
                + test1.get(1));
        System.out.println("Should print [B, D]: "
                + test1);
       
        System.out.println("\nShould print error message. Cannot add at index 10:");
        test1.add(10, "A");

   
        test1.add(0, "A");
        System.out.println("\nAdding A at index 0: ");
        System.out.println("Should print size of 3: "
                + test1.size());
        System.out.println("Should print the list array is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print A at index 0: "
                + test1.get(0));
        System.out.println("Should print B at index 1: "
                + test1.get(1));
        System.out.println("Should print [A, B, D]: " + test1);
       
        test1.add(2, "C");
        System.out.println("\nAdding C at index 2: ");
        System.out.println("Should print size of 4: "
                + test1.size());
        System.out.println("Should print the list array is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print A at index 0: "
                + test1.get(0));
        System.out.println("Should print C at index 2: "
                + test1.get(2));
        System.out.println("Should print [A, B, C, D]: " + test1);
       
        System.out.println("\nAdding 1-20 in ListArray of size 15. Should resize to 20:");
        for(int i = 1; i <= 20; i++) {
            test2.add("" + i);
        }
        System.out.println("Should print size of 20: "
                + test2.size());
        System.out.println("Should print the list array is empty - false: "
                + test2.isEmpty());
        System.out.println("Should print 1 at index 0: "
                + test2.get(0));
        System.out.println("Should print 20 at index 19: "
                + test2.get(19));
       
        System.out.println("\n***Testing Copy Constructor***\n");
        ListArray<String> test3 = new ListArray<String>(test1);
        System.out.println("Should print size of 4: "
                + test1.size());
        System.out.println("Should print the list array is empty - false: "
                + test3.isEmpty());
        System.out.println("Should print A at index 0: "
                + test3.get(0));
        System.out.println("Should print C at index 2: "
                + test3.get(2));
        System.out.println("Should print [A, B, C, D]: " + test3);
       
        System.out.println("\nTesting for deep copy: ");
        test3.add("A");
        test3.add("B");
        test3.add("C");
        System.out.println("Original list array size should be 4: "
                + test1.size());
        System.out.println("New list array size should be 7: "
                + test3.size());
        System.out.println("Printing original [A, B, C, D]: " + test1);
        System.out.println("Printing new [A, B, C, D, A, B, C]: " + test3);
       
        System.out.println("\n***Testing Set Method***\n");
        System.out.println("Should print error message when setting index 6: ");
        test1.set(6, "Z");

       
        test1.set(2, "Z");
        System.out.println("\nShould print size of 4: "
                + test1.size());
        System.out.println("Should print the list array is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print A at index 0: "
                + test1.get(0));
        System.out.println("Should print Z at index 2: "
                + test1.get(2));
        System.out.println("Should print [A, B, Z, D]: " + test1);
       
        test1.set(0, "Z");
        System.out.println("\nShould print size of 4: "
                + test1.size());
        System.out.println("Should print the list array is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print Z at index 0: "
                + test1.get(0));
        System.out.println("Should print Z at index 2: "
                + test1.get(2));
        System.out.println("Should print [Z, B, Z, D]: " + test1);
       
        System.out.println("\n***Testing Remove Methods***\n");
       
        System.out.println("Remove index 3, element D: "
                + test3.remove(3));
        System.out.println("Should print size of 6: "
                + test3.size());
        System.out.println("Should print the list array is empty - false: "
                + test3.isEmpty());
        System.out.println("Should print A at index 0: "
                + test3.get(0));
        System.out.println("Should print A at index 3: "
                + test3.get(3));
        System.out.println("Should print [A, B, C, A, B, C]: " + test3);
       
        System.out.println("\nRemove index 0, element A: "
                + test3.remove(0));
        System.out.println("Should print size of 5: "
                + test3.size());
        System.out.println("Should print the list array is empty - false: "
                + test3.isEmpty());
        System.out.println("Should print B at index 0: "
                + test3.get(0));
        System.out.println("Should print B at index 3: "
                + test3.get(3));
        System.out.println("Should print [B, C, A, B, C]: " + test3);
       
        System.out.println("\nRemove index 10, should print error message: ");
        test3.remove(10);

        System.out.println("Should print size of 5: "
                + test3.size());
        System.out.println("Should print the list array is empty - false: "
                + test3.isEmpty());
        System.out.println("Should print B at index 0: "
                + test3.get(0));
        System.out.println("Should print B at index 3: "
                + test3.get(3));
        System.out.println("Should print [B, C, A, B, C]: " + test3);
       
        System.out.println("\nRemove index of size - 1 (last element) C: "
                + test3.remove(test3.size() - 1));
        System.out.println("Should print size of 4: "
                + test3.size());
        System.out.println("Should print the list array is empty - false: "
                + test3.isEmpty());
        System.out.println("Should print B at index 0: "
                + test3.get(0));
        System.out.println("Should print B at index size - 1: "
                + test3.get(test3.size() - 1));
        System.out.println("Should print [B, C, A, B]: " + test3);
       
        System.out.println("\nCan remove B (first instance): "
                + test3.remove("B"));
        System.out.println("Should print size of 3: "
                + test3.size());
        System.out.println("Should print the list array is empty - false: "
                + test3.isEmpty());
        System.out.println("Should print C at index 0: "
                + test3.get(0));
        System.out.println("Should print B at index size - 1: "
                + test3.get(test3.size() - 1));
        System.out.println("Should print [C, A, B]: " + test3);
       
        System.out.println("\nCan remove B (only instance): "
                + test3.remove("B"));
        System.out.println("Should print size of 2: "
                + test3.size());
        System.out.println("Should print the list array is empty - false: "
                + test3.isEmpty());
        System.out.println("Should print C at index 0: "
                + test3.get(0));
        System.out.println("Should print A at index size - 1: "
                + test3.get(test3.size() - 1));
        System.out.println("Should print [C, A]: " + test3);
       
        System.out.println("\nShould print error message: ");
        System.out.println("Cannot remove Z (not stored): "
                + test3.remove("Z"));
        System.out.println("Should print size of 2: "
                + test3.size());
        System.out.println("Should print the list array is empty - false: "
                + test3.isEmpty());
        System.out.println("Should print C at index 0: "
                + test3.get(0));
        System.out.println("Should print A at index size - 1: "
                + test3.get(test3.size() - 1));
        System.out.println("Should print [C, A]: " + test3);
       
        test3.remove("C");
        System.out.println("\nRemove C:");
        System.out.println("Should print size of 1: "
                + test3.size());
        System.out.println("Should print the list array is empty - false: "
                    + test3.isEmpty());
        System.out.println("Should print A at index 1: "
                        + test3.get(0));
        System.out.println("Should print A at index size - 1: "
                        + test3.get(test3.size() - 1));
        System.out.println("Should print [A]: " + test3);
       
        test3.remove("A");
        System.out.println("\nRemove A:");
        System.out.println("Should print size of 0: "
                + test3.size());
        System.out.println("Should print the list array is empty - true: "
                + test3.isEmpty());
        System.out.println("Should print []: " + test3);
       
        System.out.println("\n***Testing Size Method***\n");
        System.out.println("Should print size of 0: "
                + test3.size());
        System.out.println("Should print size of 20: "
                + test2.size());
        System.out.println("Should print size of 4: "
                + test1.size());
       
        System.out.println("\n***Testing IsEmpty Method***\n");
        System.out.println("Should print true: "
                + test3.isEmpty());
        System.out.println("Should print false: "
                + test2.isEmpty());
        System.out.println("Should print false: "
                + test1.isEmpty());
       
        System.out.println("\n***Testing Contains Method***\n");
        System.out.println("Contains Z, should print true: "
                + test1.contains(new String("Z")));
        System.out.println("Contains D, should print true: "
                + test1.contains("D"));
        System.out.println("Contains A, should print false: "
                + test1.contains("A"));
       
        System.out.println("\n***Testing IndexOf Method***\n");
        System.out.println("Index of Z, should print 0: "
                + test1.indexOf("Z"));
        System.out.println("Index of D, should print 3: "
                + test1.indexOf("D"));
        System.out.println("Index of A, should print -1: "
                + test1.indexOf("A"));
       
        System.out.println("\n***Testing Get Method***\n");
        System.out.println("Element at 0, should print Z: "
                + test1.get(0));
        System.out.println("Element at 3, should print D: "
                + test1.get(3));
        System.out.println("Element at 6, should print error message: ");
        test1.get(6);

       
        System.out.println("\n***Testing Equal Method***\n");
        ListArray<Integer> test4 = new ListArray<Integer>();
        test4.add(1);
        test4.add(2);
        test4.add(3);
        ListArray<Integer> test5 = new ListArray<Integer>();
        test5.add(new Integer(1));
        test5.add(2);
        test5.add(3);
        System.out.println("Should print ListArrays are equal (true): " + test4.equals(test5));
  
        test5.set(2, 4);
        System.out.println("Should print ListArrays are not equal (false): " + test4.equals(test5));
        test5.remove(2);
        System.out.println("Should print ListArrays are not equal (false): " + test4.equals(test5));
       
       
       
    }
}
  • When your program is giving the same output as shown below, submit your updated ListArray.java and ListArrayTest.java to Canvas.
Required Output:

***Testing Default Constructor and One Parameter Constructor***

After default constructor called:
Should print size of 0: 0
Should print the list array is empty - true: true
Should print []: []

After one parameter constructor called:

Should print size of 0: 0
Should print the list array is empty - true: true
Should print []: []

***Testing Add Methods***

Adding B:
Should print size of 1: 1
Should print the list array is empty - false: false
Should print B at index 0: B
Should print [B]: [B]

Adding D:
Should print size of 2: 2
Should print the list array is empty - false: false
Should print B at index 0: B
Should print D at index 1: D
Should print [B, D]: [B, D]

Should print error message. Cannot add at index 10:
Error: Cannot add element A at index 10.
Index is outside the bounds of the list array.
Index: 10, Size: 2

Adding A at index 0:
Should print size of 3: 3
Should print the list array is empty - false: false
Should print A at index 0: A
Should print B at index 1: B
Should print [A, B, D]: [A, B, D]

Adding C at index 2:
Should print size of 4: 4
Should print the list array is empty - false: false
Should print A at index 0: A
Should print C at index 2: C
Should print [A, B, C, D]: [A, B, C, D]

Adding 1-20 in ListArray of size 15. Should resize to 20:
Should print size of 20: 20
Should print the list array is empty - false: false
Should print 1 at index 0: 1
Should print 20 at index 19: 20

***Testing Copy Constructor***

Should print size of 4: 4
Should print the list array is empty - false: false
Should print A at index 0: A
Should print C at index 2: C
Should print [A, B, C, D]: [A, B, C, D]

Testing for deep copy:
Original list array size should be 4: 4
New list array size should be 7: 7
Printing original [A, B, C, D]: [A, B, C, D]
Printing new [A, B, C, D, A, B, C]: [A, B, C, D, A, B, C]

***Testing Set Method***

Should print error message when setting index 6:
Error: Cannot set at index outside bounds of list array.
Index: 6 , Size: 4

Should print size of 4: 4
Should print the list array is empty - false: false
Should print A at index 0: A
Should print Z at index 2: Z
Should print [A, B, Z, D]: [A, B, Z, D]

Should print size of 4: 4
Should print the list array is empty - false: false
Should print Z at index 0: Z
Should print Z at index 2: Z
Should print [Z, B, Z, D]: [Z, B, Z, D]

***Testing Remove Methods***

Remove index 3, element D: D
Should print size of 6: 6
Should print the list array is empty - false: false
Should print A at index 0: A
Should print A at index 3: A
Should print [A, B, C, A, B, C]: [A, B, C, A, B, C]

Remove index 0, element A: A
Should print size of 5: 5
Should print the list array is empty - false: false
Should print B at index 0: B
Should print B at index 3: B
Should print [B, C, A, B, C]: [B, C, A, B, C]

Remove index 10, should print error message:
Error: Cannot remove element at index 10. Outside bounds of list array.
Should print size of 5: 5
Should print the list array is empty - false: false
Should print B at index 0: B
Should print B at index 3: B
Should print [B, C, A, B, C]: [B, C, A, B, C]

Remove index of size - 1 (last element) C: C
Should print size of 4: 4
Should print the list array is empty - false: false
Should print B at index 0: B
Should print B at index size - 1: B
Should print [B, C, A, B]: [B, C, A, B]

Can remove B (first instance): true
Should print size of 3: 3
Should print the list array is empty - false: false
Should print C at index 0: C
Should print B at index size - 1: B
Should print [C, A, B]: [C, A, B]

Can remove B (only instance): true
Should print size of 2: 2
Should print the list array is empty - false: false
Should print C at index 0: C
Should print A at index size - 1: A
Should print [C, A]: [C, A]

Should print error message:
Error: Cannot remove element Z. No such element in the list array.
Cannot remove Z (not stored): false
Should print size of 2: 2
Should print the list array is empty - false: false
Should print C at index 0: C
Should print A at index size - 1: A
Should print [C, A]: [C, A]

Remove C:
Should print size of 1: 1
Should print the list array is empty - false: false
Should print A at index 1: A
Should print A at index size - 1: A
Should print [A]: [A]

Remove A:
Should print size of 0: 0
Should print the list array is empty - true: true
Should print []: []

***Testing Size Method***

Should print size of 0: 0
Should print size of 20: 20
Should print size of 4: 4

***Testing IsEmpty Method***

Should print true: true
Should print false: false
Should print false: false

***Testing Contains Method***

Contains Z, should print true: true
Contains D, should print true: true
Contains A, should print false: false

***Testing IndexOf Method***

Index of Z, should print 0: 0
Index of D, should print 3: 3
Index of A, should print -1: -1

***Testing Get Method***

Element at 0, should print Z: Z
Element at 3, should print D: D
Element at 6, should print error message:
Error: Cannot get element at index 6.
Outside bounds of list array.
Index: 6, Size: 4

***Testing Equals Method***

Should print ListArrays are equal (true): true
Should print ListArrays are not equal (false): false
Should print ListArrays are not equal (false): false