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 20:30
In this lab, you complete a prewritten c program that calculates an employee’s productivity bonus and prints the employee’s name and bonus. bonuses are calculated based on an employee’s productivity score as shown below. a productivity score is calculated by first dividing an employee’s transactions dollar value by the number of transactions and then dividing the result by the number of shifts worked.
Answers: 3
question
Computers and Technology, 23.06.2019 02:00
Consider the following function main: int main() { int alpha[20]; int beta[20]; int matrix[10][4]; . . } a. write the definition of the function inputarray that prompts the user to input 20 numbers and stores the numbers into alpha. b. write the definition of the function doublearray that initializes the elements of beta to two times the corresponding elements in alpha. make sure that you prevent the function from modifying the elements of alpha. c. write the definition of the function copyalphabeta that stores alpha into the first five rows of matrix and beta into the last five rows of matrix. make sure that you prevent the function from modifying the elements of alpha and beta. d. write the definition of the function printarray that prints any onedimensional array of type int. print 15 elements per line. e. write a c11 program that tests the function main and the functions discussed in parts a through d. (add additional functions, such as printing a two-dimensional array, as needed.)
Answers: 3
question
Computers and Technology, 23.06.2019 06:30
Which option correctly describes a dbms application? a. software used to manage databases b. software used to organize files and folders c. software used to develop specialized images d. software used to create effective presentations
Answers: 1
question
Computers and Technology, 23.06.2019 09:00
Design a class tictactoe that: holds the following information about the game: two-dimensional array (3 by 3), and winner. add additional variables as needed. includes the functions to perform the various operations on objects. for example, function to print the board, getting the move, checking if move is valid, determining if there is a winner after each move. add additional operations as needed. includes constructor(s). write the functions of the class, and write a program that uses the class. the program should declare an object of type tictactoe. the program will create the board and store it in the array. the program will allow two players to play the tic-tac-toe game. after every valid move update the array, check if there is a winner. if there is no winner and no tie, then print the board again to continue.
Answers: 2
You know the right answer?
A stack machine executes programs consisting of commands that manipulate an internal stack.
T...
Questions
Questions on the website: 13722362