Generic stack with type constraint
Java
Medium
generics
Task «Generic stack with type constraint»
Implement a parameterized class GenericStack<T extends Number>, which represents a stack of numbers (Integer, Double, etc.). The class must contain methods push(T element), T pop(), T peek(), and boolean isEmpty(). Use an array or ArrayList internally. In the main method, read a sequence of lines from standard input: the first line is the type of numbers (Integer or Double), the second line is the number of operations n, followed by n lines with operations: push <value>, pop, peek. For each operation, output the result (for pop and peek — the value, for push — 'ok'). If the stack is empty during pop or peek, output 'error'.
Input Format
First line — number type (Integer or Double). Second line — an integer n (1 ≤ n ≤ 100). Then n lines, each containing one of the commands: push <number>, pop, peek.
Output Format
For each command, output the result (ok, number, or error) on a separate line.
Examples
Example 1
INPUT
Double
4
push 3.14
peek
pop
pop
OUTPUT
ok
3.14
3.14
error
main.java