Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication8;
- import java.util.*;
- public class JavaApplication8 {
- public JavaApplication8() {
- }
- public static void main(String args[]) {
- System.out.println("Starting App");
- PFStack st = new PFStack();
- st.push(new Integer(10));
- st.push(new Integer(20));
- st.push(new Integer(30));
- System.out.println("Top of stack is " + (Integer)st.peek());
- try {
- st.pop();
- System.out.println("pop 1");
- st.pop();
- System.out.println("pop 2");
- st.pop();
- System.out.println("pop 3");
- st.pop(); // This line causes an exception and throws the exception
- System.out.println("pop 4");
- st.pop();
- } catch(EmptyPFStackException e) {
- e.printStackTrace();
- System.out.println("successfully caught exception and finished executing");
- }
- }
- }
- class PFStack {
- private Vector m_theData;
- public PFStack() {
- m_theData = new Vector();
- }
- public void push (Object item) {
- m_theData.addElement(item);
- }
- public Object peek() {
- return m_theData.lastElement();
- }
- public Object pop() throws EmptyPFStackException {
- try {
- Object result = m_theData.lastElement();
- m_theData.removeElementAt(m_theData.size()-1);
- return result;
- } catch(Exception e) {
- throw new EmptyPFStackException("Reached end of stack");
- }
- }
- }
- class EmptyPFStackException extends Exception {
- public EmptyPFStackException() {
- }
- public EmptyPFStackException(String msg) {
- super(msg);
- }
- }
Add Comment
Please, Sign In to add comment