This is the code i have for my stack program:

public class MyStack {
    private int top;
    private int[] store;

    Stack(int capacity) {
        if (capacity <= 0)

        throw new IllegalArgumentException("Stack's capacity must be positive");

        store = new int[capacity];
        top = -1;
    }

    void push(int value) {

        if (top == store.length)
        throw new StackException("Stack's underlying storage is overflow");
            top++;
            store[top] = value;
    }

    int peek() {

        if (top == -1)
        throw new StackException("Stack is empty");
        return store[top];
    }
    void pop() {

        if (top == -1)
        throw new StackException("Stack is empty");
        top--;
    }

    boolean isEmpty() {

        return (top == -1);
    }

    public class StackException extends RuntimeException {

        public StackException(String message) {

        super(message);

    }

    }

    }

I now have to produce its main method, which demonstrates the axioms used such as pop,push etc
any ideas? thanks

The push method should be:

if (top == store.length-1)
throw new StackException("Stack's underlying storage is overflow");
top++;
store[top] = value;

Because initially top is -1. The you increase it and use it as index. Meaning that top will hold not the number of elements but the index of the last value entered. Meaning that the maximum value that it will take is store.length-1.
Imagine an array of size 3. You enter the last element at place 2: store[top] = value; Top is 2.

Then you call push again: if (top == store.length) . This will be false because size is 3. Then topp will be increased from 2 to 3 and then it will be used as index, which will give you an error.

The rest methods are OK. Also you should add an isFull method.

As for your question, just create a main method in this class or another. It doesn't matter where is created. When you call the class, only the main method will execute and everything that is inside it.
In that main method, create an instance of the object MyStack and call its methods with various arguments. For better demonstration, try to add more values than the Stack can handle in order to show how you can catch the exception.

This is the code i have for my stack program:

public class MyStack {
    private int top;
    private int[] store;

    void  Stack(int capacity) {
        if (capacity <= 0)

        throw new IllegalArgumentException("Stack's capacity must be positive");

        store = new int[capacity];
        top = -1;
    }

    void push(int value) {

        if (top == store.length)
        throw new StackException("Stack's underlying storage is overflow");
            top++;
            store[top] = value;
    }

    int peek() {

        if (top == -1)
        throw new StackException("Stack is empty");
        return store[top];
    }
    void pop() {

        if (top == -1)
        throw new StackException("Stack is empty");
        top--;
    }

    boolean isEmpty() {

        return (top == -1);
    }

    public class StackException extends RuntimeException {

        public StackException(String message) {

        super(message);

    }

    }

    }

I now have to produce its main method, which demonstrates the axioms used such as pop,push etc
any ideas? thanks

/* in this above static method need a return typ so u put the  void return tupe to your coding . U will put your desired return  type (or) u put  the main metod in entire  dao.java  then only  your problem will solve*/ 

I can alternate t above coding you will try and reply to me...

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.