-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegerStack.java
More file actions
46 lines (37 loc) · 1.05 KB
/
IntegerStack.java
File metadata and controls
46 lines (37 loc) · 1.05 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
package unit10.mySolutions;
import unit10.solutions.StackEmptyException;
import unit10.solutions.StackFullException;
// Push method throws exception if stack is full. Print
// method throws exception if the stack is empty.
public class IntegerStack {
private int entries;
private int [] stack;
public IntegerStack(int size) {
entries = 0;
stack = new int[size];
}
public IntegerStack() {
this(10);
}
// Throws exception
public void push(int num) throws StackFullException {
if(stackFull())
throw new StackFullException("Stack is full");
else
stack[entries++] = num;
}
// Throws exception
public void print() throws StackEmptyException {
if(stackEmpty())
throw new StackEmptyException("Stack is empty");
else
for(int i = 0; i < entries; i++)
System.out.println(stack[i]);
}
private boolean stackFull() {
return (entries == stack.length) ? true : false;
}
private boolean stackEmpty() {
return (entries == 0) ? true : false;
}
}