2

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


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();
    }

}
SDJ
  • 4,083
  • 1
  • 17
  • 35
user3058359
  • 21
  • 1
  • 2

2 Answers2

0

1. As Clad mentioned in the comments, it looks like you are just missing the return keyword on the isEmpty() function:

/* determine if the stack is empty */
public boolean isEmpty() {
    return stack.isEmpty();
}

However, for the isFull() function, there is no isFull() function on the List class, you will have to implement that yourself. You'll have to keep track of the integer that is passed in through your constructor so that you can compare it to the size of the list that is stored in your object

2. Are you supposed to allow the user of this object to specify any SIZE they want, or are you supposed to always have a 5 integer limit?

3. You might want to read this section of the Java documentation on How to Throw Exceptions

Paul Gray
  • 546
  • 1
  • 5
  • 10
0

Did you really look a bit on how to do before asking ? seems like you just copy paste a code you don't get.

import java.util.ArrayList;
import java.util.List;

public class IntegerStack {

private int max_size;
private List<Integer> stack;

public IntegerStack(int size) {
    max_size = 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() {
    return stack.isEmpty();
}

/* determine if the stack is full */
public boolean isFull() {
    //go through all your stack and see if there are things not set to get if it's full.
}

/* determine the size of the stack */
public int size() {
    if (stack.isEmpty())
        return 0;
    else
        return stack.size();
}
}

when you create your objet you put 5 as the parameter. You may need other change in code ;)

Try by yourself you will improve more :D

Clad Clad
  • 2,653
  • 1
  • 20
  • 32
  • Your push method is wrong. And the list interface doesn't provide an `isFull()` method. – Alexis C. Dec 02 '13 at 17:39
  • I didn't say it was the answer but a way to go to the answer ;) this guy didn't seems to look a bit for the answer that's why ;) – Clad Clad Dec 02 '13 at 17:40
  • Not a reason to provide wrong code (for the `push` method) even if you give an helpful hint by storing the `max_size` attribute. – Alexis C. Dec 02 '13 at 17:41
  • I didn't want to give something wrong my bad sorry for this ^^ I changed it back to original – Clad Clad Dec 02 '13 at 17:46