Classes, References, Memory Management

 

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, for copying an array by hand.

3.    to start working towards creating your own tests for your code.

 

For this exercise, you will implement the SmartArrayResizable class.  The key difference between this version & the last version is that the last version only allocated memory once, and if you used it all up, you were out of luck – with this class, you can ask it to allocate a new array if it’s out of memory.

The key thing to learn here is how to make a larger copy of a given array yourself (i.e., without using any built-in .Net methods) – in a nutshell, you'll need a variable to hold a temporary reference to the new, larger array, then you'll need to copy everything from the existing array into the new array, and finally you'll need to set the rNums field to refer to the new array.  Since this is C#, and C# has garbage-collection, you won't need to worry about releasing the memory occupied by the old array –the language will eventually notice, and get rid of it for you (well, technically the .Net Common Language Runtime will do this, but who's quibbling? J ).  The exact behavior is described below.

            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 SmartArrayResizable 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.  

a.    Stuff that hasn't changed from the SmartArray has been grayed out, stuff that has changed has been marked in red.  As you can see, the only change is the addition of the Resize() method.

2.    You should make sure to annotate (put in a comment that documents) the running time of the Resize method.

The following was removed on Sat, Jan 21st, at about 10pm

3.    Once you’ve completed the above task, you should run the tests in the Test_SmartArrayResizable_Basic class and the Test_SmartArrayResizable 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.

a.       The Test_SmartArrayResizable_Basic class serves to make sure that your SmartArrayResizable still does everything that a basic SmartArray does.  It is expected that your SmartArrayResizable will be able to pass these tests simply by virtue of inheriting methods from the SmartArray base class.

                                                  i.      Note that this means that your SmartArrayResizable class MUST inherit from the SmartArray class in order for these tests to even compile!

b.      The Test_SmartArrayResizable class serves to check new, SmartArrayResizable -specific functionality that you’ve implemented in this exercise.

SmartArrayResizable Data & Methods

Data Field Name

Type

Description:

rNums

Array of ints

A reference to an array of integers.
This is declared in the base class (SmartArray), NOT on the SmartArrayResizable.

This data field must be marked protected (in the base class - SmartArray), so that you can access it in this class (SmartArrayResizable)

Note: all data fields should be marked private, unless otherwise specified

Method Name

Returns

Description/Parameters:

Default constructor

<nothing>

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

Resize

<nothing>

If the memory isn’t allocated, then the C# runtime will throw an exception, so we don’t have to do anything special to handle the case where we ask for more memory than C# can provide.

Parameters:

1.      An integer that is desired new size of the array.

 

By calling this method, the caller is indicating that the SmartArray should be resized.  If the value of the parameter is larger than the current size of rNums, then you should allocate a new array, copy all the values from rNums into the new array, and then set rNums so that it refers to the new array.  The old array is discarded.

 

If the value of the parameter is smaller than rNums, then the new array will be allocated with this new, smaller, length.  Copy as much of rNums into the new array as is possible, and discard any extra values.  When you're done, rNums[0] should be the same as the newArray[0], and the 'highest' values in rNums will have been lost.

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

 

This will throw an UnderflowException if the index is too low, and will throw an OverflowException if the index is too high.  These exceptions are defined in the Program.cs file for this project.

GetAtIndex

The integer value at the array slot.

Parameters:

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

 

Conceptually,

SmartArrayObject.GetAtIndex(10);

this will accomplish the same sort of thing as saying

Array[10];

would accomplish with a normal array

This will throw an UnderflowException if the index is too low, and will throw an OverflowException if the index is too high.  These exceptions are defined in the Program.cs file for this project.

PrintAllElements

Nothing (void)

Parameters: None

 

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

 

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.

 

getSize

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

If the array hasn't been allocated yet, this will return Int32.MinValue.  Otherwise, the following logic should be executed:

 

      return rNums.Length;

 

 

Note: all methods should be marked public