Cash-back offer from April 23rd to 27th, 2024: Get a flat 10% cash-back credited to your account for a minimum transaction of $50.Post Your Questions Today!

Question DetailsNormal
$ 40.00

ECEC301 Assignment - Infix Calculator | Complete Solution

Question posted by
Online Tutor Profile
request

Assignment - Infix Calculator


Introduction
This assignment will give you practice with Java, interfaces (not Java interfaces, but the more general
notion), and build tools (ant, jar). Write a Java program that will evaluate arithmetic expresions in postfix notation called Calc.java.


Expressions in postfix notation contain the operands on which the operation is performed followed by
an operator. For example, 3 4 + is equal to 3 + 4 in the infix notation. For this assignment your
program should support integer numbers and the following operators: + - * / % (all integer
operators).
Your interpreter should accept strings of operators and operands seperated by some whitespace from
standard input, and print the final result or output an error if the input is invalid. Each line will be a
separate expression. EOF is the signal to quit.


The Assignment
You will write a program that parses infix expressions (described below) into appropriate Tokens
(operator or operand), stored in some linear container (ArrayList), passes the infix expression to a
function that returns the expression to postfix form, then passes it to a function which evaluates the
postfix expression, returns an integer.
We'll be doing int arithmetic. Though the inputs are positive, the intermediate results might not be.
The operators you will encounter are +, -, *, /, % (modulus). You must also be prepared to handle
parenthesis.
Your code will be documented properly.
Your code
I expect to see, at a minimum, 2 methods:
infix2postfix
evalPostfix
I've written Token classes for you (the files are in the class Assignments/Calc directory). You do need
to fill in the Operator.getPrec() method, assign appropriate (relative) precedences to the operators.
The 2 types, Operand and Operator share a base class, Token, so that you can store an entire
expression, in a generic ArrayList which holds Tokens. We need to get used to generics. Sample.java, in the directory, shows the creation and storage of various Tokens in a generic collection.
opType is an example of how we did "enums" pre- Java 1.5. Note that you can make these types
smarter by assigning precedence right there. You're welcome to make this modification.
Yes, you need to use my Token classes. Input Your program will read a file called input.infix that contains a number of expressions, one per line. Each line has, at most, 80 characters. Tokens will be separated by white space. Operands will be strictly non-negative integers. Operators are: { + - * / % } for addition, subtraction, multiplication, division, and modulus, respectively.
Here is a sample input:
13 + 23 - 42 * 2
3 * ( 5 - 2 ) % 5
A sample input file can be found here.
For my part, I promise that all expressions are valid.
Output
Your program will output, for each input expression, on one line:
postfix expression = result , where result is the value of expression.
There will be one expression per line (same as the input). Single-space only, please. So, given the input, above, I'd expect the output to be:
13 23 + 42 2 * - = -48
3 5 2 - * 5 % = 4


Documentation
I am NOT looking for a lot of comments here. Remember, your code should read like a book. But,
each class should have a description, and each method should have a description and a discussion of
inputs, side-effects, etc. You will use Javadoc-style comments. HTML documentation will be created from your code, using ant (and javadoc, of course).


Algorithms
Infix to Postfix
Append a right paren ')' to the end of the input expression.
Push a left paren '(' onto the stack.
Start at the first token. For each token:
If it is a left paren, push it onto the stack.
If it is a right paren, pop operators from the stack and append to the postfix expression,
until a left paren is encountered on the stack. Remove and discard the left paren.
If it is an operand, append it to the postfix expression.
If it is an operator, then pop operators from the stack and append to the postfix expression
while the operators have equal or higher precedence than the current token. Push current
token (operator) onto the stack.
Remember, we're not treating the parentheses as operators, they're being handled
separately.


Continue until you've reached the end of the expression. If the input expression was valid, then
evey pop() should've been fine, and the stack should be empty.
Evaluating Postfix Expressions
Start at the first token. For each token:
If it is an operand, push it on the stack.
Else if it is an operator, then
y ← pop top value
x ← pop top value
result ← x (oper) y
push result onto stack
fi
Continue until you've reached the end of the expression. There should be exactly one element in the
stack; the result of the expression.
The main function will parse the input file and hand the expressions to this function to evaluate. See
the StreamTokenizer examples. Or, since the Java community is moving away from this, you might
look at the String.split methods.
Files for Download
Operator.java
Operand.java
opType.java
Token.java
Sample.java
Submission
Calc.java - Your driver, container for static methods, post2infix, etc.
Operator.java - You need to fill the in the getPrec method of my file, submit it Any other source files you need. .class files will be deleted before I start, so, must be built from source.


README optional - Anything you want to tell me before I grade
If you supply input.infix it will be overwritten.
Please note You will use the filenames as listed here, and submit them all to the proper assignment. If
this is not clear, then see me in class. If I get programs submitted to the wrong directories, I will be
irate, which may, in some subconscious way, affect your grades. Adversely.
ECEC301 Assignment - Infix Calculator https://www.cs.drexel.edu/~kschmidt/ECE301/Assignments
 

Available Answer
$ 40.00

[Solved] ECEC301 Assignment - Infix Calculator | Complete Solution

  • This Solution has been Purchased 1 time
  • Submitted On 13 Mar, 2015 11:56:12
Answer posted by
Online Tutor Profile
solution

This Tutorial is rated A+ p...

Buy now to view the complete solution
Other Similar Questions
User Profile
Exper...

ECEC301 Assignment - Infix Calculator | Complete Solution

This Tutorial is rated A+ previously,if you have any questions regarding this tutorial then you can contact me.

...

The benefits of buying study notes from CourseMerits

homeworkhelptime
Assurance Of Timely Delivery
We value your patience, and to ensure you always receive your homework help within the promised time, our dedicated team of tutors begins their work as soon as the request arrives.
tutoring
Best Price In The Market
All the services that are available on our page cost only a nominal amount of money. In fact, the prices are lower than the industry standards. You can always expect value for money from us.
tutorsupport
Uninterrupted 24/7 Support
Our customer support wing remains online 24x7 to provide you seamless assistance. Also, when you post a query or a request here, you can expect an immediate response from our side.
closebutton

$ 629.35