Guest User

Untitled

a guest
May 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. package javaapplication8;
  2. import java.util.*;
  3.  
  4. public class JavaApplication8 {
  5. public JavaApplication8() {
  6. }
  7.  
  8. public static void main(String args[]) {
  9. System.out.println("Starting App");
  10. PFStack st = new PFStack();
  11. st.push(new Integer(10));
  12. st.push(new Integer(20));
  13. st.push(new Integer(30));
  14.  
  15. System.out.println("Top of stack is " + (Integer)st.peek());
  16. try {
  17. st.pop();
  18. System.out.println("pop 1");
  19. st.pop();
  20. System.out.println("pop 2");
  21. st.pop();
  22. System.out.println("pop 3");
  23. st.pop(); // This line causes an exception and throws the exception
  24. System.out.println("pop 4");
  25. st.pop();
  26. } catch(EmptyPFStackException e) {
  27. e.printStackTrace();
  28. System.out.println("successfully caught exception and finished executing");
  29. }
  30.  
  31. }
  32. }
  33.  
  34. class PFStack {
  35. private Vector m_theData;
  36.  
  37. public PFStack() {
  38. m_theData = new Vector();
  39. }
  40. public void push (Object item) {
  41. m_theData.addElement(item);
  42. }
  43. public Object peek() {
  44. return m_theData.lastElement();
  45. }
  46. public Object pop() throws EmptyPFStackException {
  47. try {
  48. Object result = m_theData.lastElement();
  49. m_theData.removeElementAt(m_theData.size()-1);
  50. return result;
  51. } catch(Exception e) {
  52. throw new EmptyPFStackException("Reached end of stack");
  53. }
  54.  
  55. }
  56. }
  57.  
  58. class EmptyPFStackException extends Exception {
  59. public EmptyPFStackException() {
  60. }
  61.  
  62. public EmptyPFStackException(String msg) {
  63. super(msg);
  64. }
  65. }
Add Comment
Please, Sign In to add comment