I need help with implementing grow and shrink Here is the actually instructions--->

•grow(). This method, when called, will “grow” arrayStack by utilizing the strategy outlined in 2 above. This method will only be called by push(). Note that grow() should be private.
•shrink(). This method, when called, will shrink arrayStack by utilizing a strategy similar to that outline in 2 above. This method will only be called by pop() and will only be executed when the “occupied” part of the array is less than or equal to half the size of arrayStack. Note that after a call to shrink, the size of arrayStack should be equal to the number of “occupied” slots. Note also that shrink() should be private.

this is what I have done so far---> I have couple errors and I not sure what I am doing wrong..

public class ArrayStack {

    private int[] arrayStack = new int[50];
    private int index = -1;


    public ArrayStack() {}

    public boolean isEmpty() {
        return index == -1;
    }   

    public void push(int num) {
        arrayStack[++index] = num;
    }   
    public Boolean isFull() {
        if( isFull()) {
            return index =arrayStack.length – 1;
        }
        else
    public void push(int num) {
        if (isFull()) {
            throw new FullStackException("Trying to push onto a full stack!");
        }
    arrayStack[++index] = num;
    }

    private int grow (int num) {
        arrayStack++;
    }

    public int shrink(int num) {
        arrayStack--;

    public int pop() {

        if (isEmpty()) {

            throw new EmptyStackException("Trying to pop from an empty stack!");
    }   
        return arrayStack[index--];
    }   

    public void printStack() {
        for (int i = 0; i < arrayStack.length; i++) {
            System.out.println(arrayStack[i]);
        }   
    }       
}

Recommended Answers

All 5 Replies

what exactly are your errors?

what is "the strategy outlined in 2 above"?

When I run the program I am having a characters errors. In line 17 and yes the strategy is outlined in the above 2.

  1. We can utilize the same strategy used by ArrayList. In other words, when the stack becomes full, we declare a new array with a size that is twice the size of the original array, copy the elements from the old array into the new Array using System.arrayCopy(), and let the reference to the original array point to the new array.

lol.. MNSU IT 214, Prof Hart?

 public Boolean isFull() {
    if( isFull()) {
    ...

When you call isFull(), on its first line it calls isFull(), which, on its first line calls isFull(), which, on its first line calls isFull(), which, on its first line calls isFull(), which, on its first line calls isFull()...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.