Computers and Technology

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.

answer
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 12:00, anamatiascamaja
If you embed a word table into powerpoint, what happens when you make edits to the embedded data? a. edits made to embedded data change the data in the source file; however, edits made to the source file will not be reflected in the embedded data. b. edits made to embedded data will change the data in the source file, and edits made to the source file will be reflected in the embedded data. c. edits made to embedded data don't change the data in the source file, nor will edits made to the source file be reflected in the embedded data. d. edits made to embedded data don't change the data in the source file; however, edits made to the source file will be reflected in the embedded data.
Answers: 1
image
Computers and Technology, 24.06.2019 16:50, Laylahlettiere
Develop the program incrementally: a) start by reading and displaying each line of the input file to make sure you are reading the data set correctly. b) use the split string method to extract information from each line into a list. print the list to prove that this step is working correctly. d) convert the exam scores to type int and calculate the student’s average. display those items to prove this step is working correctly. e) create a tuple containing the six items for each student (name, exam scores, exam mean). display the tuples to prove this step is working correctly. (optionally, you may want to have the exam scores in a list so your tuple is (name, scores_list, f) append each tuple to a list. display the list to prove this step is working correctly. g) use the sort list method to re-order the tuples in the list. display the list to prove this step is working correctly. h) use a for statement to display the contents of the list as a table (with appropriate formatting). i) use a for statement to calculate the average of all scores on exam #1, then display the results. note that you could have calculated this average within the first loop, but we are explicitly requiring you to do this calculation by looping though your list of tuples. j) add the logic to calculate the average of all scores on exam #2, then display the results.
Answers: 2
image
Computers and Technology, 24.06.2019 21:40, imamador6396
Which of these is not a type of socket? aga (alternating grid array) pga (pin grid array) spga (staggered pin grid array) lga (land grid array)
Answers: 1
image
Computers and Technology, 24.06.2019 22:00, ivethzurita0425
What is a number system, and what is the total number of digits used in this system called? a number system is a system that uses different (options: a) numbers b) symbols c) codes d) digits e) alphabets) to represent different numbers. the total number of digits used in a number system is known as its (options: 1) processor 2) converter 3) radix 4) least significant digit 5) most significant digit)
Answers: 1
Do you know the correct answer?
A stack machine executes programs consisting of commands that manipulate an internal stack.
T...

Questions in other subjects:

Konu
Mathematics, 24.04.2020 19:21