2016-10-31 Lab + HW

posted Oct 30, 2016, 9:28 PM by Samuel Konstantinovich   [ updated Oct 30, 2016, 9:31 PM ]
Easy methods:
void grow():
   Increases the capacity of this SuperArray instance to ???.

void trimToSize() 
   Trims the capacity of this SuperArray instance to be the list's current size.

If you have N elements in your SuperArray, how many operations does it take with respect to N to do each of these:
//Ignore small constants.
1. get(i)   
    // answer -> 
    //Since you just look at the array directly, it takes k operations where k is some small constant. This means it takes 1 operation since we ignore constants!
2. grow() 
    // answer -> 
    //After you new the array, you must copy over the N elements, so it takes N operations!

3. set(i,v)
4. add(v)
5. add(i,v)
6. remove(i)
7. trimToSize() 


If we know the starting capacity is 10.  Let us analyze two grow methods:
a) add 1 to the capacity.
b) double the capacity.
In both cases, we add 160 elements to the SuperArray. 
How would the behavior differ?
How many times would both 'a' and 'b' versions resize the array?

By the way:

Finally your lab + HW:
Discuss how you would test your completed SuperArray to a level that would make you confident that it would not crash on any tests that I performed on them.
Assume that we perform operations on valid indices and operations that make sense:
-No invalid index operations
-No removal of elements when empty.

You must test all kinds of situations, because any single command could possibly break the rest. 
  adding to all locations 0, ... , size()
  removing from any location (not just 0 or size() -1
  removing all the elements
  test all operations after grow() has been called
  removal of a set of elements, and replacing them
  Use loops to run multiple commands, do not test manually!
  Use print statements that include the value you would expect, along with the toString() of the SuperArray.

Create a battery of tests, post yours if you think you have problems OR if you think yours is very thorough.