Due: Monday, February 27, 2017, 11:59 pm

Stacks

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 usses an array of ints 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 initialize 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 equals to 2 * 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.
    • Write a main method testing every method in your class. One of your tests should be to push 20 items onto your stack. This test is to check whether your stack grows appropriately. Test your stack and debug it until it is perfect. : )

Once you have completed the lab and feel comfortable with stacks, go on to Project 3.

© 2017 Ying Li. Page last modified: .