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:
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 |