Classes, References

The three goals for this exercise are:

1.    to continue to improve upon the SmartArray class

2.    to practice concepts that related to dealing with references – specifically checking for null, and allocating a block of memory when you need it.

For this exercise, will implement the SmartArrayAOD class, where AOD stands for AllocOnDemand.  The key difference between this version & the original SmartArray is that the original allocated memory in the class's constructor – this class will defer allocating any memory until you attempt to store something in the SmartArrayAOD.  Additionally, the SmartArrayAOD will create a new array (and then copy the old values into the new array) whenever someone attempts to SetAtIndex that’s higher than the current bounds of the array.

The key thing to learn here is how to deal with a reference that might be null.  In other words, for each method, you'll first need to check if rgNums is null, and if so, you'll need to decide what to do if the array hasn't yet been allocated.  The exact behavior is described below.  Requirements that haven’t changed from the prior exercise of this type have been grayed out, requirements that are new or changed have been marked in red.  Note that you may have to change how you implement methods that are grayed out, in order to fulfill the new, red requirements in a different method (depending on how you’ve implemented things)

            Once you've done that, you should put (in comments) the running time, in Big "Oh" notation, for each method that you implemented.  Make sure to specify the running time for the case wherein you need to allocate the array (remember that the .Net runtime will have to initialize each element of the array to "0" – even though the runtime is doing that for you, it still counts as time! J), as opposed to the situation wherein the array was previously allocated in a different method call.

What you need to do for this exercise:  

  1. For this exercise, you need to implement the SmartArrayAOD class.  This class should be found in the Student_Answers.cs file in a project named something like 03_PCE_StudentCode.  You need to implement the methods on the class, as described below.
    1. Stuff that hasn't changed from the SmartArray has been grayed out, stuff that has changed has been marked in red.
  2.  You should make sure to annotate (put in a comment that documents) the running time of the SetAtIndex, GetAtIndex, and Find methods.
  3. Once you’ve completed the above task, you should run the tests in the Test_SmartArrayAOD  class.  They should all pass at this point, and if not, then you should fix your program so that those tests do pass.  You can run the tests using the NUnit support in the provided starter project.  You should feel free to supplement that code with your own test cases if you wish, but you are neither required nor expected to.

SmartArrayAOD Data & Methods

Data Field Name

Type

Description:

rgNums

Array of ints

A reference to an array of integers. Until the first time that SetAtIndex is called, this will be null (unless an optional, alternate constructor is used)

Note: all data fields should be marked private

Method Name

Returns

Description/Parameters:

Default constructor

<nothing>

Initializes rgNums to be null, so that other methods may check if rgNums is null or has a valid memory address.

PrintAllElements

Nothing (void)

Parameters: None

 

Print all elements of the array, one per line, onto the console.

 

If rgNums hasn't been allocated yet, this method will print the message
ARRAY NOT YET ALLOCATED

on it’s own line

Find

true if at least one element in the array is the same value as the parameter, false if the given value isn't present in the array.

Parameter:

1.      An integer that may or may not be in the array.

 

This method takes it's parameter, and sees if that value is located anywhere within the array.  If it finds even a single slot of the array with the same value as the parameter, it will return true.  If the value isn't found anywhere within the array, it will return false.

 

If the array hasn't been allocated yet, this will return false.

GetSize

An integer – the size, in number of elements, that the SmartArray currently has the capacity to hold.

Parameters: None

 

If the array hasn't been allocated yet, this will return zero ( 0 ).  Otherwise, the following logic should be executed:

 

     return rgNums.Length;

 


SetAtIndex

Nothing

Parameters:

1.      An integer that is the index of the element to set

2.      An integer that is the value to set that element to

 

Conceptually,

SmartArrayObject.SetAtIndex(10, 20)

this will accomplish the same sort of thing as saying

Array[10] = 20;

would accomplish with a normal array


Each time this method is called, if the index parameter is outside the current bounds of the array (or if there is no array (because the default constructor was used)), this method should resize the rgNums array, in order to ensure that the SmartArray has enough space to hold the new value.

            For example, if a SmartArrayAOD object is created using the default constructor, the very first time that SetAtIndex is called, the method will allocate an array of that contains enough space to put the value in the requested spot, and is at least 5 elements long.  If the second time this method is called with, say, a target index of 3, no new allocations will happen.  If the third time the method is called with, say, a target index of 20, this method will resize the array to be 21 elements long, while preserving the values placed in the array in the first and second method calls.

GetAtIndex

The integer value at the array slot.

Parameters:

1.      An integer that is the index of the element to get


This will throw an UnderflowException if the index is too low, and will throw an OverflowException if the index is too high (or if the array hasn't been allocated yet).  These exceptions are defined in the Program.cs file for this project.

Note: all methods should be marked public