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

  Write a program that draws a picture of a house

Write a program that draws a picture of a house. It could be as simple as the accompanying figure, or if you like, make it more elaborate

  Modeling the caves

Assignment 2 Description:  You are a mighty warrior, and armed with your trusty bow and 3 arrows, you enter The Caves in search of the mighty Wumpus. If you shoot the Wumpus, you are victorious and the masses will praise you, but if you stumble upon ..

  Write java program prints strings given at command line

Write down the program Average.java which just prints strings which it is given at command line, one per line. If nothing is given at command line, print "No arguments".

  Create an application that provides a solution

Create an application that provides a solution for problem 20.8 In addition to requirements specified in the description.

  Creates an array named odds

Write code that creates an array named odds and stores all odd numbers between -6 and 38 into it using a loop. Make the array's size exactly large enough to store the numbers.

  Create javascript program to find gross pay for employees

Create JavaScript program which will find out the gross pay for each of three employees. Company pays "straight time" for first 40 hours worked by each employee and pays "time and a half".

  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 (..

  Java program that computes a ride you took in taxi

Write a java program that computes a ride you took in taxi. The bill computed based on an initial cost plus the taxi charges 0.5 dirham every 0.3 km.

  1 gqueuea queue is an ordered collection of items in which

1 gqueuea queue is an ordered collection of items in which the removal of items is restricted to the fifo rst in rst

  Write a program comprising classes

Write a program comprising three classes: MonteCarlo (with the main() method), Simulation, and Metrics.

  Wite a java application that allows the user to read

write a java application that allows the user to read validate store display sort and search the account

  Create a class called scorelist according to the uml diagram

Create a class called ScoreList according to the UML diagram. This class will allow a user to enter up to 100 scores into an array.

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