-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackExcercise.java
More file actions
64 lines (45 loc) · 1.62 KB
/
StackExcercise.java
File metadata and controls
64 lines (45 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Author: Joshua Greene
Date: 9/8/2020
Class: COSC2203:Data Structures: Section 1
Operating System: Windows 10
Java version: 13.0.2
Description: This lab had us create our own "Stack" using an ArrayList.
We also made our own interface and Exception class.
*/
package stackexcercise;
//Tester class for GreeneStack:
public class StackExcercise {
public static void main(String[] args) throws GreeneEmptyStackException {
GreeneStack<String> myStack = new GreeneStack<>();
//try-catch block to catch the exception that will be thrown by pop():
try {
myStack.pop();
}
catch(GreeneEmptyStackException ex) {
System.out.println(ex.getMessage());
}
//returns "true":
System.out.println(myStack.isEmpty());
//adding A - E to myStack:
myStack.push("A");
myStack.push("B");
myStack.push("C");
myStack.push("D");
myStack.push("E");
//"E" is erased and returned:
System.out.println(myStack.pop());
//prints myStack:
System.out.println(myStack);
//returns "D":
System.out.println(myStack.peek());
//prints myStack:
System.out.println(myStack);
//"D" is erased and returned:
System.out.println(myStack.pop());
//prints myStack:
System.out.println(myStack);
//prints "false":
System.out.println(myStack.isEmpty());
}
}