stack calculator or cellar memory is an abstract data structure that is structured like a list. In the case of a stack, it is only possible to add a new element at the first position
INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS
JAVA ASSIGNMENT 3
Do not use any elements of the Java libraries, except elements of java.lang and java.io packages.
Exercise A and B need to be submitted separately.
- STACK BASED CALCULATOR (Note: Only use LinkedList for this exercise. Do not use ArrayList)
A stack or cellar memory is an abstract data structure that is structured like a list. In the case of a stack, it is only possible to add a new element at the first position, to remove the last added element or to return the value of the last added value. The internal structure of the stack is hidden from the user - they can only add and remove elements. However, the user should have access to further information, such as a textual representation and the size of the stack.
A pocket calculator uses a stack as a storage model, i.e. numbers can be added one after the other and mathematical operations can be carried out. The top two numbers are always taken from the stack and the result of the operation is placed on the stack. The goal of the task is to develop an interactive user interface that enables interaction with the calculator with integers. Make sure there is a clear separation between the user interface, the logic of the calculator and the stack. Implement the following commands. Your program should be able to process the specified entries and output an error message (starting with Error, ␣) in the (specified possible) error cases. If the entry is valid, the specified output should be output. If an unknown command is entered, an error message is issued.
Note: You can assume that transferred numbers can always be converted into integers without errors.
- push <number>
- number: Input an integer
- Functionality: puts number on the stack of the calculator
- Output: OK
- Possible errors:
- Invalid number of arguments
- Symbol in parameter number, which is not a number
- pop
- Functionality: removes the first element of the stack. If the stack is empty, the stack does not change.
- Output: OK
- Possible error case: invalid number of arguments
- peek
- Functionality: Outputs the top element of the stack and does not change the stack.
- Output: The string representation of the topmost element of the stack
- possible errors:
- invalid number of arguments
- the stack is empty
- add
- Functionality: Adds the top two elements of the stack and places the result on the stack.
- Output: OK
- possible errors:
- invalid number of arguments
- the stack has less than 2 elements
- sub
- Functionality: Subtracts the second top element from the topmost element and sets the result on the stack.
- Output: OK
- possible errors:
- invalid number of arguments
- the stack has less than 2 elements
- multiply
- Functionality: Multiplies the top two elements of the stack and places the result on the stack.
- Output: OK
- possible errors:
- invalid number of arguments
- the stack has less than 2 elements
- divide
- Functionality: divides the top element by the second top element and puts the result (as an integer) on the stack.
- Output: OK
- possible errors:
- invalid number of arguments
- the stack has less than 2 elements
- the second top element is a 0
- if-else
- Functionality: takes the top element from the stack; if this is a 0, the second top element is removed from the stack. Otherwise, the third top element is removed from the stack. The top element remains on the stack.
- Output: OK
- possible errors:
- invalid number of arguments
- the stack has less than 3 elements
- print
- Functionality: Outputs a textual representation of the stack. The individual elements are separated by commas (without spaces). Output begins at the top of the stack. If the stack is empty, an empty line is simply output.
- Output: <top element>,<second top element>,...,<bottom element>
- Possible error case: invalid number of arguments
- revert
- Functionality: Inverts the stack so that, for example, the top element is the bottom element. i.e the stack 1,2,3 becomes 3,2,1. If the stack is empty, the stack does not change.
- Output: OK
- Possible error case: invalid number of arguments
- reset
- Functionality: Completely empties the calculator's memory
- Output: OK
- Possible error case: invalid number of arguments
12.quit
- Functionality: exits the program
- Output: no output
- Possible error case: invalid number of arguments
Attachments:
Expert's Answer
675 Times Downloaded
Related Questions
. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C
DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma
. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an
Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t
. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual
Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th
. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:
1
Project 1
Introduction - the SeaPort Project series
For this set of projects for the course, we wish to simulate some of the aspects of a number of
. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:
1
Project 2
Introduction - the SeaPort Project series
For this set of projects for the course, we wish to simulate some of the aspects of a number of