Repository navigation
Expand file tree
/
Copy pathQ10828.java
More file actions
69 lines (55 loc) · 1.48 KB
/
Copy pathQ10828.java
File metadata and controls
69 lines (55 loc) · 1.48 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.Stack;
public class Q10828 {
public static Stack<Integer> stack;
public static void main(String[] args) {
stack = new Stack<>();
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
// Reflection
Class classMy = Q10828.class;
Object obj = classMy.newInstance();
Method method = null;
int count = Integer.parseInt(br.readLine());
for (int i = 0; i < count; i++) {
String[] arr = br.readLine().split(" ");
if (arr[0].equals("push")) {
// method has parameters
method = classMy.getMethod(arr[0], new Class[] {Integer.class });
method.invoke(obj, Integer.parseInt(arr[1]));
} else {
// method has no parameters
method = classMy.getMethod(arr[0]);
method.invoke(obj);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void push(Integer num) {
stack.push(num);
}
public void pop() {
if (stack.isEmpty())
System.out.println("-1");
else
System.out.println(stack.pop());
}
public void size() {
System.out.println(stack.size());
}
public void empty() {
if (stack.isEmpty())
System.out.println("1");
else
System.out.println("0");
}
public void top() {
if (stack.isEmpty())
System.out.println("-1");
else
System.out.println(stack.get(stack.size() - 1));
}
}