Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Help with Java Stack. How to fix my mistakes on me methods?

The things I need help with are:

1. What is the correct code to make the isEmpty() and isFull() methods return an answer to tell me if the stack is empty or if the stack is full

2. I need to make my stack I am using be a set size of five(5)(stack can hold 5 ints)

3. I need to add exceptions in to the push() and pop() methods, that does not let the push be used when the stack is full, or the pop be used when the stack is empty

Here is my code so far, If you can help with all or any of these three things thank you

import java.util.ArrayList;

import java.util.List;

public class IntegerStack {

private List<Integer> stack;

public IntegerStack(int SIZE)

{

stack = new ArrayList<Integer>(SIZE);

}

/*method to push the stack*/

public void push(int i)

{

stack.add(0,i);

}

/*method to pop the stack*/

public int pop()

{

if(!stack.isEmpty()){

int i= stack.get(0);

stack.remove(0);

return i;

} else{

return -1;// Or any invalid value

}

}

/*method to peek at the stack*/

public int peek()

{

if(!stack.isEmpty()){

return stack.get(0);

} else{

return -1;// Or any invalid value

}

}

/*determine if the stack is empty*/

public boolean isEmpty()

{

stack.isEmpty();

}

/*determine if the stack is full*/

public boolean isFull()

{

stack.isFull();

}

/*determine the size of the stack*/

public int size()

{

if(stack.isEmpty())

return 0;

else

return stack.size();

}

}

1 Answer

Relevance
Still have questions? Get your answers by asking now.