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:
https://docs.oracle.com/javase/7/docs/api/java/lang/IndexOutOfBoundsException.html
http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.html

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.
Parameters:
     initialCapacity - the initial capacity of the list
Throws:
     IllegalArgumentException - if the specified initial capacity is negative


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


set(index,value)
Parameters:
   index - index of the element to replace
element - element to be stored at the specified position
Returns:
   the element previously at the specified position
Throws:
   IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())


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


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