Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

ADT Stack

Objectives

- To gain understanding of how an implementation of an ADT is used by an application program.
- To learn how expressions can be evaluated at run-time.
- To become familiar with how infix expressions can be converted to postfix expressions.

Task

Write a Java program (a collection of Java classes) including a class named ExpressionCalculator that contains a static main method. This main method will prompt the user to enter a constant expression (i.e., a mathematical expression without variables) at the keyboard. The expression must be in infix notation and must use spaces to separate the tokens (operands and operators) of the expression. (This restriction is imposed to simplify the parsing of the input.) The only exception to the space-separation rule is for the unary minus prefix operator, which will be assumed to be part of the specification of a negative constant operand. The program will then evaluate the expression and display the result on the console. It will then wait for another expression to be entered for evaluation, terminating on an empty input.

The following operators should be supported by your expression calculator:

Operator

Type

Precedence

Description

+

binary

4

addition

-

binary

4

subtraction

*

binary

5

multiplication

/

binary

5

division

mod

binary

5

remainder

Operator

Type

Precedence

Description

^

binary

6

exponentiation

!

right unary

6

factorial

%

right unary

6

percentage

abs

unary

6

absolute value

trunc

unary

6

truncation

round

unary

6

rounding

sin

unary

6

sine

cos

unary

6

cosine

ln

unary

6

natural logarithm

log

unary

6

common (base-10) logarithm

lg

unary

6

base-2 logarithm

exp

unary

6

natural exponential (ek)

sqr

unary

6

square (k2)

sqrt

unary

6

square root

The algorithm for the infix-to-postfix converter is as follows:

while there are more tokens in the infix string get the next token
if the next token is an operand append it to the postfix expression
else if the next token is an operator process the operator (see below)
else
indicate a syntax error
pop the remaining operators off the operator stack and append them to the postfix expression, but indicate a syntax error if a '(' is found

The algorithm for processing an operator is:

if the operator stack is empty or
the item on top of the stack is '('

push the current operator onto the stack
else
fetch the top operator on the stack
if the precedence of the current operator is greater than the precedence of the top operator from the stack
push the current operator onto the stack
else
while the stack is not empty and
the operator on top of the stack is not '(' and
the precedence of the current operator is less than or equal to the precedence of the top operator on the stack
pop the top operator off the stack and append it to the postfix

expression

if the operator stack is not empty
fetch the top operator from the stack

if the operator on top of the stack is '(' pop the top element of the stack
if the current operator is not ')'
push the current operator onto the stack

You can also use the algorithms in the text-book if you like. You can also adjust them as needed to allow for the correct processing of right-associative unary operators. If some of those operations cannot be complemented, that is fine.

You can use the algorithm for postfix expression evaluator in the textbook.

Hints

Use a StringBuffer or the new StringBuilder class of Java 5 for the postfix expression as it is being constructed. It is more efficient to make many modifications to a StringBuffer or to append() strings to a StringBuilder than to continually create new Strings by appending to existing ones.

Use Java's StringTokenizer or the new Scanner class of Java 5 to parse expressions. The latter offers the methods hasNextType and nextType (where Type is replaced by Integer, Double, etc.), which would be convenient for checking if the next token in an expression can be parsed as a Double and is thus an operand.

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M92034121
  • Price:- $60

Priced at Now at $60, Verified Solution

Have any Question?


Related Questions in Computer Engineering

The of the steering wheel is used to create a parallel

The _____ of the steering wheel is used to create a parallel plane in the Synchronous Part environment. The _____ option is used to apply the crown by defining its radius and take-off angle. 1-In the Ordered Part environ ...

Question what sequence of numbers would be printed by the

Question : What sequence of numbers would be printed by the following recursive function if we started it with N assigned the value 1? def Exercise (N): print(N) if (N Exercise(N + 1) print(N)

Please pick a health care phenomenaproblem and use philip

Please pick a health care phenomena/problem and use Philip Jacobs's descriptive, explanatory and evaluative model (in studying Economics) to analyze it. (Please use the real statistics that you search online and cite the ...

Question you need to research the topic and discuss the

Question: You need to research the topic and discuss the topic in at 400 words with references. Then, . reference will not count as a discussion. What would be the impact of predictive modeling on healthcare and medicine ...

In a large university 6 of students live in dormitories a

In a large university, 6% of students live in dormitories. A random sample of 14 students is selected. What is the probability that the sample contains more than five students who do not live in the dormitories?

Calculate the energy of one photon of blue light that has a

Calculate the energy of one photon of blue light that has a wavelenght of 425 nm and red light that has a wavelenght of 740 nm. Use E = hv and C = frequency x wavelenght(v). And determine which photon has highest energy.

Can someone please show me how to do this step by step

Can someone please show me how to do this step by step please. I dont understand any of it 1.) Add a new virtual disk a) Edit VMware player settings to add a new virtual hard disk. b) Start or reboot the virtual machine ...

Question find an article on the www that discusses a recent

Question: Find an article on the WWW that discusses a recent (within the last 2 years) security breach or Cyberattack. Provide a link to the article and suggest 2 Best practices for a user Domain Policy that would mitiga ...

Reminder all files must be closed when you are done with

Reminder: All files must be closed when you are done with them, even if it stops early due to an IOError. If you're using with, this will happen automatically. If you're trying to close things manually using .close(), th ...

Explain that the biggest problems with adware is that it

Explain that the biggest problems with adware is that it slows down the computers its running on.

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As