-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ5(Practical).java
More file actions
160 lines (156 loc) · 3.19 KB
/
Q5(Practical).java
File metadata and controls
160 lines (156 loc) · 3.19 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
5.Write a program to implement stack. Use exception handling to manage underflow and overflow conditions.
/**** Stack.java ****/
public class Stack {
// Top of the Stack
private int tos;
// Array of Elements
private int[] array;
// Size of the Stack
final private int size;
/**
* Public Constructor for Stack Objects
*
* @param size Size of the Stack
*/
public Stack(int size) {
this.tos = -1;
this.size = size;
this.array = new int[this.size];
}
/**
* Push an element to the top of the stack
*
* @param e Element to be pushed
* @throws StackException Stack Overflow
*/
public void push(int e) throws StackException {
if (tos == size - 1)
throw new StackException("Stack Overflow: could not push " + e);
else
this.array[++this.tos] = e;
}
/**
* Pop an element from the stack
*
* @return Object on top of the stack
* @throws StackException Stack Underflow
*/
public int pop() throws StackException {
if (this.tos < 0) {
throw new StackException("Stack Underflow:could not pop");
} else
return this.array[this.tos--];
}
/**
* Index of the element at the top of the stack
*
* @return Index of Generic Element
*/
public int getTOS() {
return this.tos;
}
/**
* Representation of Stack Object
*
* @return String Representation
*/
@Override
public String toString() {
return "Stack<size=" + this.size + ">";
}
}
/**** StackException.java ****/
public class StackException extends Exception {
final private String message;
public StackException(String message) {
this.message = message;
}
@Override
public String getMessage() {
return this.message;
}
}
/**** Main.java ****/
import java.util.Random;
public class Main {
public static void main(String[] args) {
int r;
Stack stack = new Stack(10);
Random random = new Random(1337);
System.out.println("Created stack of size 10...");
System.out.println("Pushing integers onto stack...");
while (true) {
r = random.nextInt(100);
System.out.println("Pushing " + r + "...");
try {
stack.push(r);
System.out.println(
"Elements in Stack = " + (stack.getTOS() +1)
);
} catch (StackException e) {
System.err.println(e.getMessage());
break;
}
}
System.out.println("Popping integers from stack...");
while (true) {
System.out.println(
"Elements in Stack = " + (stack.getTOS() +1)
);
try {
System.out.println("Popped " + stack.pop() + "...");
} catch (StackException e) {
System.err.println(e.getMessage());
break;
}
}
}
}
Output -
Created stack of size 10...
Pushing integers onto stack...
Pushing 25...
Elements in Stack = 1
Pushing 42...
Elements in Stack = 2
Pushing 54...
Elements in Stack = 3
Pushing 8...
Elements in Stack = 4
Pushing 46...
Elements in Stack = 5
Pushing 3...
Elements in Stack = 6
Pushing 6...
Elements in Stack = 7
Pushing 29...
Elements in Stack = 8
Pushing 67...
Elements in Stack = 9
Pushing 7...
Elements in Stack = 10
Pushing 4...
Stack Overflow: could not push 4
Popping integers from stack...
Elements in Stack = 10
Popped 7...
Elements in Stack = 9
Popped 67...
Elements in Stack = 8
Popped 29...
Elements in Stack = 7
Popped 6...
Elements in Stack = 6
Popped 3...
Elements in Stack = 5
Popped 46...
Elements in Stack = 4
Popped 8...
Elements in Stack = 3
Popped 54...
Elements in Stack = 2
Popped 42...
Elements in Stack = 1
Popped 25...
Elements in Stack = 0
Stack Underflow: could not pop