The goal for this exercise is to get some practice creating a more specialized subclass, as well as to practice implementing a stack of simple types.
In this lesson, we're going to both implement a Stack, as well as examine one use of object-oriented programming (specifically, inheritance) : using inheritance to specialize an existing class.
For this exercise, you need
to create a subclass of the SmartArray class, named StackOfInts. Your StackOfInts
class will need to support the Push
, Pop
, Peek
,
IsEmpty
and getSize
methods.
In order to do that,
you will need to add a method to the SmartArray class – getSize
.
All of the methods for both the SmartArray, and StackOfInts classes, are described in more detail at the end of this document.
For this exercise, you are welcome to (and encouraged to) simply reuse your code for the SmartArray class from a prior exercise.
What you need to do for this exercise:
Test_Stack_SmartArray_Basic
class and the
Test_StackOfInts
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.Test_Stack_SmartArray_Basic
class serves to
make sure that your StackOfInts
still does everything
that a basic SmartArray
does. It is expected that your
StackOfInts
will be able to pass these tests simply by
virtue of inheriting methods from the SmartArray base class.StackOfInts
class MUST inherit from the SmartArray
class in
order for these tests to even compile!Test_StackOfInts
class serves to check new,
Stack-specific functionality that you’ve implemented in this
exercise.
SmartArray Data & Methods |
||
Data Field Name |
Type |
Description: |
|
Array of |
A reference to an array of integers. |
Note: all data fields should be marked private |
||
Unchanged Methods: |
||
These are unchanged from the prior implementation:
Default constructor,
|
||
New Method Name |
Returns |
Description/Parameters: |
|
An integer – the size, in number of elements, that the SmartArray currently has the capacity to hold. |
:) |
Note: all methods should be marked public |
StackOfInts Data & Methods |
||
Data Field Name |
Type |
Description: |
Note: This class must inherit from SmartArray |
||
|
|
You're free to make |
Note: all data fields should be marked private |
||
Method Name |
Returns |
Description/Parameters: |
<constructor> |
Nothing, by definition |
Allocates the array that the StackOfInts will use to store the integers |
|
True, if the stack currently contains NO elements. False otherwise |
Return type says it all |
|
Returns nothing
Will throw an |
Parameters: 1. An integer that is the value to be added to the top of the stack
This method will take the value given by the
parameter, and add it to the top of the stack. |
|
Returns the top-most item on the stack.
Will throw an |
Parameters: 1. None
Note that this method, unlike Pop, does NOT change the stack in any way – it only copies the topmost item into the parameter (if there is a top-most item to copy), and then returns.
|
|
Returns the top-most item on the stack.
Will throw an |
Parameters: 1. None
Note that this method, unlike Peek, DOES change the stack –it removes that top-most item from the stack and returns it.
Hint: In order
for this to happen, you'll need to adjust |
Note: all methods should be marked public |