Lab 16: Exception Handling (100 pts)

due Monday, November 25 at 9:20am on Canvas


Pair Programming Extra Credit (+5pts E.C.)
  • Both partners fill in, sign, date, and submit the pair programming contract
  • Upload the document(s) along with your Lab code to Canvas.
  • Only ONE partner submits the Lab code on Canvas.
  • BOTH partners submit the contract.
  • Please make sure both your names are on all the files.
  • If you need help finding a partner, please contact me as soon as possible.

ArrayList: The Final Index
  • For this lab, we will updated our ArrayList class to include exception handling.
  • Anywhere that we had previously written the method to print an error message to the console, we will now throw an exception instead.
  • For example, below is an updated version of the get method.
      /**
     * Returns the element at the specified
     * index. Prints an error message and
     * returns an empty String if index is
     * larger than or equal to numElements
     * or index is less than 0
     * @param index the index of the element
     * to access
     * @return the element at index
     * @throws IndexOutOfBoundsException
     * when index > numItems

     */
    @SuppressWarnings("unchecked")
    public E get(int index) throws IndexOutOfBoundsException{
        if (index >= numElements || index < 0) {
            throw new IndexOutOfBoundsException(
                    "Error: Cannot get element at index " + index + "."
                    + "\nOutside bounds of the ArrayList.\nIndex: "
                    + index + ", Size: " + numElements);

        }
        return (E) array[index];
    }

  • Note the updates that have been made to this method (bolded above):
    • Javadoc comment rewritten to include the @throws tag
    • Method signature revised to indicate the method throws an exception
    • If statement now throws an exception rather than printing the error message to the console
  • You will need to follow a similar model for four additional methods.
  • Please see the bolded changes below to the starter code provided for the prior lab:

/**
 * ArrayList.java
 *
 * @author
 * @author
*  CIS 36B, Lab 16
 */

public class ArrayList<E> {
    private Object[] array;
    private int numElements;

    /** Constructors */

    /**
     * Default constructor for ArrayList Creates an empty array of length 10 Sets
     * numElements to 0
     */
    @SuppressWarnings("unchecked")
    public ArrayList() {
        array = (E[]) new Object[10]; //declare a new array of Objects, but cast to type E
        numElements = 0;
    }

    /**
     * Constructor for ArrayList Creates an empty array of length size Sets
     * numElements to 0
     *
     * @param size the initial size of the ArrayList
     */
    public ArrayList(int size) {

    }

    /**
     * Copy constructor for ArrayList
     * Creates a new ArrayList of the same size as
     * the ArrayList passed in as a parameter,
     * and copies the parameter's data into
     * the new array list using a for loop
     * Also sets the numElements to be the same
     * as the parameter's numElements
     * Hint: Make a deep copy!
     * @param la the ArrayList to copy
     */
    @SuppressWarnings("unchecked")
    public ArrayList(ArrayList<E> la) {

    }

    /** Accessor Methods */

    /**
     * Returns whether the array list is currently empty
     *
     * @return whether the array list is empty
     */
    public boolean isEmpty() {
        return false;
    }

    /**
     * Returns the current number of elements stored in the array list
     *
     * @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
     * or index is < 0
     * @param index the index of the element to access
     * @return the element at index
     * @throws IndexOutOfBoundsException
     * when index > numItems or index < 0
     */
    public E get(int index) { //update method signature
        return null;
    }

    /**
     * Uses the linearSearch algorithm to locate an element in the array list
     *
     * @param element the element to locate
     * @return whether or not the element is in the array list
     */
    public boolean contains(E element) {

        return false;
    }

    /**
     * Uses the linearSearch algorithm to locate an element in the array list
     *
     * @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 array list is currently full
     *
     * @return whether the array list 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 array list 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 array list. Resizes the array list 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 array list. Resizes the
     * array list if it is at capacity before inserting
     *
     * @param index   the index at which to insert
     * @param element the element to insert
     * @throws IndexOutOfBoundsException
     * when index > numItems or index < 0

     */
    public void add(int index, E element) { //update method signature

    }

    /**
     * Assigns a new value to the array list at a specified index
     *
     * @param index   the index at which to update
     * @param element the new element
     * @throws IndexOutOfBoundsException
     * when index > numItems or index < 0

     */
    public void set(int index, E element) {
//update method signature
    }

    /**
     * Removes an element at a specified index in the array list
     *
     * @param index the index at which to remove
     * @return the element that was removed
     * @throws IndexOutOfBoundsException
     * when index > numItems or index < 0

     */
    public E remove(int index) { //update method signature

        return null;

    }

    /**
     * Removes the first instance of the specified element in the array list
     *
     * @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 ArrayLists and the same
     * length and store the same data in the same order
     */
    @SuppressWarnings("unchecked")
    @Override
    public boolean equals(Object o) {
        return false;
    }
}


  • Please update your code from Lab 14 to incorporate the changes to the Javadoc comments and the method signatures.
  • Then, you will want to update the indicated methods to throw an exception rather than displaying an error message to the console.
  • Next, update your ArrayListTest.java file to handle the exception by placing the appropriate method calls inside of a try-catch block.
  • Here is an example for the set method:
    System.out.println("\n***Testing Set Method***\n");
          try {       
        System.out.println("Should print error message when setting index 6: ");
        test1.set(6, "Z");

    } catch(IndexOutOfBoundsException e) {
        System.out.println(e.getMessage());
    }
  • The above should (still) produce the following output to the console:

***Testing Set Method***

Should print error message when setting index 6:
Error: Cannot set at index outside bounds of
ArrayList.
Index: 6 , Size: 4
  • Open ArrayListTest.java and update the tests anywhere an exception might be thrown by placing that method call inside of a try-catch, as shown in the example above.
  • Note that you should add a total of 4 try-catch blocks to ArrayListTest.java.
    • See the highlighted output below
  • If you have updated your code properly, all tests should run, and the program should not end prematurely when an exception is thrown.
  • Make sure that you obtain the correct output as shown below:

Required Output:

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

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

After one parameter constructor called:

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

***Testing Add Methods***

Adding B:
Should print size of 1: 1
Should print the
ArrayList 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
ArrayList 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
ArrayList.
Index: 10, Size: 2


Adding A at index 0:
Should print size of 3: 3
Should print the
ArrayList 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
ArrayList 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
ArrayList 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
ArrayList 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
ArrayList size should be 4: 4
New
ArrayList 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
ArrayList.
Index: 6 , Size: 4


Should print size of 4: 4
Should print the
ArrayList 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
ArrayList 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
ArrayList 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
ArrayList 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
ArrayList.
Should print size of 5: 5
Should print the
ArrayList 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
ArrayList 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
ArrayList 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
ArrayList 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
ArrayList.
Cannot remove Z (not stored): false
Should print size of 2: 2
Should print the
ArrayList 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
ArrayList 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
ArrayList 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
ArrayList.
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 updated ArrayList.java and ArrayListTest.java to Canvas
  • If pair programming: Only one partner needs to submit the two files, but both partners should upload the pair programming contract.

How You Will Be Graded:

  • Each updated method and its associated test in ContainerTest.java is worth 25 points
  • 2.5 point deduction for missing pair programming contract (if pair programming)
  • 5 point deduction for not removing package statements from the top of your files
  • 5 point deduction for code that has not been properly formatted - please go to Source->Format in Eclipse to format your code before submitting.
  • 5 point deduction for not writing your name(s) in a Javadoc comment on top of both files
  • 10-100 point deduction for any added import statements.