Integer expression evaluator

Assignment Help JAVA Programming
Reference no: EM13552222

Program must use stacks in order to evaluate integer expressions. Your program need only implement the following binary operators:

+ ... addition
- ... subtraction
* ... multiplication
/ ... integer division
< ... minimum (e.g. 3 < 2 yields 2)
> ... maximum (e.g. 3 > 2 yields 3)

To keep the assignment somewhat more manageable you may assume that all input integers are unsigned single decimal digits (0-9). However, your evaluation may result in larger and/or negative integers.

The evaluation of expressions must recognize the standard precedence:

< and > have the highest precedence
* and / have the middle precedence
+ and - have lowest precedence

Expressions must also permit the usual use of parentheses (...) to override precedence.

Below are several examples of proper expressions:

1+2*3>4

(24-7)/2+6<3*(31-299)

2*3*4*5*6

10>9*8+7+2

Your program must read input expressions from a text file and allow the user to select which text file to process using a JFileChooser. You may assume that each line of the text file is a separate integer expression and that there are no blanks or other invalid characters in the file.

For each input expression your program should output (using System.out.println) the input line followed by "==" then followed by the numeric value of the resulting expressions evaluation.

You must implement a bounded generic Stack class. Represent your Stack using an array to hold the stack content. (An array with a maximum of one hundred cells will be more than adequate.)

The standard algorithm for evaluating expressions of this style is to use two stacks - one stack for operators and the other for operands/subexpression values. An expression is processed left to right as follows:

• Each operand encountered is pushed upon the operand stack.

• Each left paren encountered is pushed upon the operator stack.

• Each right paren causes operators to be popped from the operator stack and the associated subexpressions evaluated until a left paren is found in the operator stack. The left paren is then popped.

• Each operator has its precedence compared to the precedence of the operator atop the operator stack. Operators are popped off the operator stack and their corresponding subexpressions evaluated until the top of the operator stack is either a left paren or has a precedence less than the new operator. After evaluating subexpressions, the new operator is pushed onto the operator stack.

• At the end of the expression all operators on the operator stack must be popped and their corresponding subexpressions evaluated. The value of the expression should be the single remaining value atop the operand stack.

When the prior explanations refer to "popping an operator and evaluating the corresponding subexpression" this means that the operand stack should be popped twice, and the two popped operands are evaluated using the popped operator with the resulting value pushed on the operand stack. For example, if the top of the operand stack is a 7 and the second operand in the operand stack is a 3 and the operator is a "-", then all three of these are popped and the result of the subexpression 3-7 (i.e., the value -4) is pushed on the operand stack.

Reference no: EM13552222

Questions Cloud

Estimate the density of the solvent : A cylindrical storage tank has a radius of 1.20 m. When filled to a height of 3.99 m, it holds 13800 kg of a liquid industrial solvent. What is the density of the solvent
Identify a global organization with a multinational presence : Identify a global organization with a multinational presence.
Estimate what is the pressure of the steam : Steam moves into the cylinder of a steam engine at a constant pressure and does 9.650 J of work on a piston. What is the pressure of the steam
Evaluate the percent relative error for the the solutions : A 50-ml transfer pipet and a 200-ml volumetric flask (To Contain) is used to make 0.500 M and 0.125 M solutions from a stock solution of 2.000±0.005 M. Calculate the percent relative error for the three solutions if perfect technique is used. Repo..
Integer expression evaluator : You must implement a bounded generic Stack class. Represent your Stack using an array to hold the stack content - Integer Expression Evaluator
Personal selling and sales objectives : Personal Selling and Sales Objectives
Electron beam on a photographic plate placed perpendicular : A beam of electrons traveling with speed 0.8 x10^(8) m/s passes through a slit of width 10^(-5) m. Because of the uncertainty in the lateral position of the beam, there will be an uncertainty in the transverse momentum as well.
Obtain the width of the first-order bright fringe : Light of wavelength 5.30 102 nm passes through a slit of width 0.230 mm. Determine the width of the first-order bright fringe
Explain small amount of unreacted starting material : Suppose a reaction has the equilibrium constant k= 1.7 x 10^-8 at a particular temperature. Will there be a large or small amount of unreacted starting material present when this reaction reaches equilibrium

Reviews

Write a Review

JAVA Programming Questions & Answers

  One-dimensional array to solve the

In C#, Use a one-dimensional array to solve the following problem. A company pays its salespeople on a commission basis. The salespeople each receive $200 per week plus 9 percent of their gross sales for that week. For example, a salesperson who gros..

  Add a button that will read the text fields

Create a GUI with two text fields for inputting the dimensions of a rectangle. Identify these two text fields as Length and Width with labels. Add a button that will read the text fields and cause the GUI to display the area and perimeter of the r..

  Generates a random number between 1 and 100

Write a program that generates a random number between 1 and 100 (inclusive) and then prompts the user to guess the number. The user should be given up to 10 chances to guess the number.

  Inheritancedemo

Create a New Project named InheritanceDemo . 2. Create a new class called Person and cut and paste the Person.java file from Blackboard into the class. Create a new class called Student and cut and paste the Student.java file from Blackboard into the..

  Write a program displaying a text file that is in a text are

Write a program displaying a text file that is in a text area. The user enteres a file name in a text field and clicks the view button; the file is then displayed in the text area. Do not use BufferedInputStream.

  The letters of the alphabet a through z

The letters of the alphabet A through Z can be represented in Morse code. Each letter is represented by a combination of up to four dots and/or dashes, as shown below. Use Java to write and run a console-window program that can convert an English ..

  Small computer system interface-standard computer disk

How many primary drive partitions are supported on computers that still conform to the standard established by MS-DOS? How many total partitions can a standard computer disk [Intergrated Drive Electronics (IDE)/Serial Advanced Technology Attachment (..

  What is relationship between hashing and digital signatures

The security advantages of using two-factor authentication, such as the integrated fingerprint reader in the more recent iPhone models, particularly to support internet services (such as online banking)

  Allow the user to display a work history report

The 'report screen' shall:Allow the user to display a work history report for an individual or for all employees for the two weeks prior to the report request.

  Write a scanner that translates logical expressions

Write a scanner that translates logical expressions. There are many different notations for logical expressions.

  Write java program which will permit user to make selection

Write the Java Program which will permit the user to make selection. You will present user with two options to perform, then you will perform action selected by user.

  Design an adt for a two-color

Design an ADT for a two-color, double-stack ADT that consists of two stacks one "red" and one "blue" and has as its operations color-coded versions of the regular stack ADT operations.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd