(5/5)

__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 usefor this exercise.__LinkedList__**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>**

: Input an integer*number*__Functionality__: putson the stack of the calculator*number*__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

(5/5)

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

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. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

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

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

(5/5)

Buy Now $35 USD
Get Free Quote!

309 Experts Online