Objectives

The goal of this lab period is to give you an opportunity to implement a stack.

Tasks

  1. Make a new folder for this week's project.
  2. Implement an IntStack class, which uses an array of integers to store the elements. It should have a field (of type int) to keep track of the index of the top of the stack and a field to store the elements (int[ ]). It should implement the following methods:
    • public IntStack( ) a constructor that makes an array of 10 ints and initializes the top index to be 0.
    • public void push( int new_item ) pushes a new item onto the stack. If there isn't enough room in the array, then create a new array and copy the data over. The size of the new array is equal to twice the size of the old array.
    • public int pop() pops an item off the stack and returns it.
    • public int elementAt( int index ) returns the element at the given position (do not modify the stack).
    • public int size() returns the number of items in the stack.
    • public boolean isEmpty() returns true if the stack is empty.
    • Test every method in your class using this main() as a starting point. Copy and paste it into your IntStack code. Feel free to add your own tests!
    • Debug until your stack is perfect. :)

When you are done with the lab exercises, you may start on the rest of the project.


© 2017 Caitrin Eaton.