Question: use java Write a console program (StackCommand.java) for handling stack commands. Command Example push 10 push number Meaning Push a number onto the top of

 use java Write a console program (StackCommand.java) for handling stack commands.

Command Example push 10 push number Meaning Push a number onto the

top of the stack pop and display the number on the top

of the stack Return the number of elements in the stack pop

use java

Write a console program (StackCommand.java) for handling stack commands. Command Example push 10 push number Meaning Push a number onto the top of the stack pop and display the number on the top of the stack Return the number of elements in the stack pop depth quit pop depth quit End the program 1. Every command is entered on a single line. 2. The command is executed after the user press the Enter key. 3. A symbol > is shown at the beginning of the line when the program is ready for a user command. 4. The push command accepts one argument, which is a number to be added to the top of a stack. (Note the number is entered on the same line as the push command) 5. The pop command removes the top element from the stack, and shows it on the next line. 6. The depth command shows the number of elements in the stack. Hint: read the API document of the Stack class. Is there a method that returns the stack's size?" 7. The quit command ends the program. 8. For this part, you can assume the user would not enter any invalid command. (e.g, unknown commands, pop from an empty stack, or invalid arguments). Handle the case of attempting to pop from an empty stack. An error message should be printed out and the program should continue to accept the next command. For Example: >push 10 >pop 10 >pop Invalid command or argument. > Implement the peek command, which return the top element of a stack without removing it from the stack itself. For example: >push 10 >push 20 Speek 20 >peek 20 >pop 20 >pop 10 WWWWWWWWW Modify the StackCommand.java program to handle the following invalid commands and arguments. Invalid command keywords: any command other than push, pop, depth, peek and quit should result in an error message. Invalid number of arguments: there should be at least one argument for the push commands, and zero argument for all other commands Invalid argument type: push command should be followed by numbers only. For each case, an error message should be shown on the next line. For examples: >push THREE >what? Invalid Command or argument. what? is not a valid command. >pop 99 >how do I quit? Invalid Command or argument. how is not a valid command. >quit >push Invalid Command or argument

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!