subject

A stack machine executes programs consisting of commands that manipulate an internal stack.
The stack is initially empty.
The commands are:
push N; push N onto the stack (N is any Double)
pop; removes top element from a stack
add; replace top two elements of the stack by their sum
mul; replace top two elements of the stack by their product
sub; replace top two elements of the stack by their difference:
[a, b, ... ] => [a-b, ... ]
div; replace top two elements of the stack by their quotient:
[a, b, ... ] => [a/b, ... ]

For example, the following program computes 23:
(push 1, push 2, mul, push 2, mul, push 2, mul)
The following types of exceptions are thrown by the stack machine:
stack empty
stack too short (not enough elements to complete the operation)
divide by 0
syntax error

(A) Implement a generic stack class for the stack machine:
class Stack { ??? }
Notes:
·You will need to implement the following methods: push, pop, top, clear, and toString.
·pop and top may throw EmptyStack exceptions
·How will you store the elements in the stack? List? Set?

(B) Implement your design using your solution for part 2.
Notes:
·Executing arithmetic commands may throw ShortStack exceptions if there aren't at least two elements on the stack.
·div may throw a DivByZero exception. Restore the stack before throwing this exception.
·Consider making the execute method and stack in your StackMachine class static. Then, commands can make static references to the stack without needing to have an association to the stack machine.
·The execute method of StackMachine should have separate catch clauses for each type of exception. For now it just prints the exception but in the future we could potentially have different strategies for each type of exception caught.
·After the last command is executed the stack machine should print the stack.
·Before the first command is executed the stack machine should clear the stack.

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 04:30
Dr. wisteria is a prominent chiropractor in the chicago area. she wants to provide a forum for her patients to discuss their health concerns and to seek and offer advice to other patients. which telecommunications tool is most appropriate for dr. wisteria's needs?
Answers: 3
question
Computers and Technology, 22.06.2019 08:30
Active listen eliminates barries to communication true or false
Answers: 1
question
Computers and Technology, 22.06.2019 17:00
Aisha has finished working on a word processing document that contains 15 pages. she has added some special elements in the first three pages, page 9 and 10, and page 15 from the document. she wants to print only these pages to see how they look. which option is the correct way to represent (in the print dialog box) the pages that aisha wants to print
Answers: 3
question
Computers and Technology, 23.06.2019 16:00
Helen is having a meeting with her colleagues in her company. they are working on the goals and objectives for the coming year. they want to ensure that these goals and objectives of the processes involved are properly evaluated. which system can helen and her colleagues apply to evaluate this? helen and her colleagues require a blank to evaluate the goals and objectives.
Answers: 2
You know the right answer?
A stack machine executes programs consisting of commands that manipulate an internal stack.
T...
Questions
question
Social Studies, 19.07.2019 16:30