Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

code in java Grab an expression from the user and evaluate it. The expression can contain +-/*( ) The expression will not contain any spaces.

code in java

Grab an expression from the user and evaluate it. The expression can contain +-/*( )

The expression will not contain any spaces. All the numbers will be single digit (between 0 and 9). No error checking is necessary.

Example:

Give me an expression: 2*(6-3)

What will be displayed on the screen

Postfix expression:243-*

Answer: 6

Do not use any of the built-in stack functions. Convert to postfix and evaluate.

Postfix Assignment

1) Can I use any of the predefined classes in the library for stacks and queues?

A: No, you have to own code for stacks and queues and nodes

2) Do I take the input from a file or from the keyboard

A: You would take the input from the keyboard

3) What will the input look like

A: You will take an infix expression. All the numbers will have a single digit. The only operators that you will be using are +,-, /, *

4) How many stacks and queues should I use?

A: For the conversion from infix to postfix you need one stack and one queue. The stack will be used to store the operators. The queue will be used

to store what would usually be displayed on the screen which is the postfix expression. Both the stack and the queue will be storing chars.

For the evaluation, another stack will be used. This stack will be storing integers. As the evaluation is being done, it is possible for the numbers to

go beyond a single digit and so char datatype would no longer make any sense

5) What should be the output of this program

A: this program should output the postfix expression on one line and the result of the evaluation on another line.

6) How many classes should I have?

A: You will have 4 classes. Stack, Queue, Postfix and Driver. For your Stack and Queue class, you will not have any static methods or variables. For

your Postfix and Driver classes you will not have any instance methods or variables.

7) Why is everything in the Postfix class static?

A: Since there will be only one expression and it doesn't need to interact with any other expressions, we don't need to create any objects.

There will be a static method that takes an infix argument and converts it to postfix.

A static method that checks the precedence of the operators

A static method that evaluates the postfix expression

8) How does the precedence method in the Postfix class work?

A: It takes a character as an argument which will be an operator. The operators will be assigned a number based on their precedence. Here is a

possible sample

= 1

( 2

+,- 3

*,/ 4

The method can return the precedence of the operator that is being passed into it.

9) What conditions do I need to consider for converting infix to postfix

A: There are 4 conditions: open parentheses, close parentheses, numbers and operators

10) What does the main method in the Driver class look like

A:

Grab an infix expression from the user,

call the convert method in the Postfix class and grab its return value which is a postfix expression.

Display postfix expression

Call the evaluate method in the Postfix class with the converted postfix expression as its argument.and return the result

Display the result

Everything should be in a single file. There should be only one public class (Driver)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions