2015-11-04 SuperArrayII

posted Nov 4, 2015, 6:28 AM by Samuel Konstantinovich   [ updated Nov 4, 2015, 2:20 PM ]
HW:  How do you test your SuperArray ?
Design a set of tests that allows you to check if your SuperArray works properly.
You must check for all possible errors, including:
  -All methods
  -All exceptions
  -If the quantity of the elements is correct
  -If the order/values of the elements is correct (hard code a few tests for this)
After you write your test cases, discuss on piazza, how many tests you ran, and why.

Goal Exceptions!!!!!

Checked Exceptions: are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword.

For example, a Java program that opens file throws a checked exception FileNotFoundException, which must be caught inside the program. 

Unchecked Exceptions: are the exceptions that are not checked at compiled time. It is up to the programmers to specify or catch the exceptions. In Java exceptions that are subclasses of Error and RuntimeException are unchecked exceptions.

IndexOutOfBound and IllegalArgumentException are a subclasses of RuntimeException. You don't have to memorize which are checked and unchecked. But you should understand the difference, and know what to do if you come across a checked exception when writing code. (Like with scanning a file)

Links to some unchecked exceptions:

Generally you check for some condition before throwing an exception

//inside the attack method of an actor (or it's subclass) you might have something like this:
    if( hp < 0 ){
       throw new DeadActorsCannotAttackException();

//Inside a StackOfPancakes class:
public Pancake eat() {
    Pancake next;
    if (size == 0) {
        throw new EmptyStackException();
    next = getNextPancake();
    return next;

Add/Modify the following Methods to your SuperArray

public SuperArray(int initialCapacity)
     Constructs an empty list with the specified initial capacity.
     initialCapacity - the initial capacity of the list
     IllegalArgumentException - if the specified initial capacity is negative

  index - index of the element to return
  the element at the specified position in this list
  IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

   index - index of the element to replace
element - element to be stored at the specified position
   the element previously at the specified position
   IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

    index - index at which the specified element is to be inserted
    element - element to be inserted
    IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())

   index - the index of the element to be removed
    the element that was removed from the list
    IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())