The Stack class, depicted in Listing 1, is a fixed stack of integers and its object’s capacity is set at instantiation time. It provides push and pop operations as well as sum, which calculates the existing items in the stack.
The main method in Listing 2 provides the interactive commands to work with an instances of object. Once you run it, it asks you to enter the max cap for stack. ‘push n’ command pushes the integer ‘n’. ‘pop’ removes the top of stack and ‘print’ it on the screen. ‘sum’ calculates the item in the stack, and ‘print’ shows the items in the stack.