# Project #61162 - Java Progrmaiing

## Background

In this assignment you will implement a small calculator that can process simple mathematical expressions that involve only (as input): positive integers, parentheses, and the four operators: addition, subtraction, multiplication, and (floating-point) division. For example, the following expressions are supported:

```(12-2)*2/(3+2)
1+1*4
(1)
(12)-(45-5)
1/3
```

but the following are not supported:

```-1-2
2.5*4
```

For this assignment, the graphical portion of the code is implemented for you. Your task is to supply the code responsible for processing the expression and evaluating it, and for detecting errors in invalid expressions.

Evaluating mathematical expressions may seem like a daunting task at first, but it is relatively simple if you choose the right data structures (which we have seen in class) and are familiar with two key concepts: tokenization and reverse Polish notation.

### Tokenization

Tokenization is the process of breaking up a sequence of characters into units that are meaningful for a certain task (in our case, computing mathematical expressions). For instance, in the expression `123+456`, it is not convenient to consider the digits individually. Instead, we would prefer to work with a sequence of three units: `[123][+][456]` (here square brackets are used as token delimiters). As part of your task, you will write a tokenizer that does three things:

• Ignore ("swallow") all white spaces;
• Flag an error if any invalid character is discovered (an invalid character is any character except a white space, digit, parenthesis, or operator).
• Package the string into a stream of tokens.

Example of tokenization:

```(123+34)/(3*4*5) -> [(][123][+][34][)][/][(][3][*][4][*][5][)]
```

### Reverse Polish Notation

Mathematical expressions such as `(1+2)/3` are in infix notation, that is, the operator is placed between operands. It turns out that evaluating operations directly in infix notation is unnecessarily complex. A better way is to convert infix expressions into Reverse Polish Notation (RPN), where operators follow their operands. In the following examples, the first line is an expression in infix notation and the line below is the same expression in RPN. Note that in RPN parentheses are superfluous and not used.

```[1][+][2]
[1][2][+]

[1][+][1][*][4]
[1][1][4][*][+]

[(][12][-][2][)][*][2][/][(][3][+][2][)]
[12][2][-][2][*][3][2][+][/]
```

As part of the assignment, one of your task is to convert the original expression in infix notation into its RPN equivalent. To do this, you can pretty much directly implement the famous Shunting-yard algorithm. If you use the previous (Wikipedia) reference, simply omit the steps that involve function calls since function calls are not supported by our calculator. As usual, our four operators are left-associative and multiplication and division have precedence over addition and subtraction.

Once you have an expression in RPN, evaluating it is trivial using a Stack (figuring out how to do this is part of the assignment).

### Error Handling

As part of the assignment, we ask you to detect problems in the input string and to flag the first problematic character detected using method`flagError`. However, different parts of the computation are "better" at detecting different types of errors, so don't try to detect errors all at once (for example at the beginning), instead, consider the following tips:

• The Tokenizer is the best place to detect invalid characters.
• The misuse of parentheses (for example as in `(1)(2)`) is easy to detect once you have a fully tokenized expression NOT in RPN.
• Unbalanced parentheses are easy to detect as part of the Shunting-yard algorithm.
• Illegal combination of operators (such as `1++2` are easy to detect in the evaluator.

## Requirements

You may be able to make the project work on other environments, but we will support the following cross-platform configuration:

• Eclipse Luna
• Java 7

## Instructions

1. Download the assignment 3 package and import it into Eclipse (File -> Import -> General -> Existing Projects into Workspace). The code skeleton should build without errors. Run the main method to make sure everything works. You should see the calculator appear but (obviously) it's not going to work as it should.
3. Complete the code provided as part of the assignment package. All further instructions and hints are in the source code comments.
4. Submit your answer as a single file called Assignment3.java that contains the academic integrity statement and all your solution. Do not submit a zip file, class files, or any other kind of artifact besides that single file. The file you submit should at the very least compile without any errors.

## Recommended Sequence

There is a natural dependency order in the pieces you need to implement for this assignment and you are strongly encouraged to follow this order:

1. Implement the Token hierarchy and the Tokenizer. Test the tokenizer thoroughly.
2. Implement the `processExpression` method and test that it produces correct RPN expressions.
3. Implement the `evaluateExpression` method.

Paste this statement in the header of your code. We will not grade your work without it. If you have any doubt about the validity of the content of your submission, contact an instructor or TA before submitting it.

```/* ACADEMIC INTEGRITY STATEMENT
*
* By submitting this file, we state that all group members associated
* with the assignment understand the meaning and consequences of cheating,
* plagiarism and other academic offenses under the Code of Student Conduct
*
* By submitting this assignment, we state that the members of the group
* associated with this assignment claim exclusive credit as the authors of the
* content of the file (except for the solution skeleton provided).
*
* In particular, this means that no part of the solution originates from:
* - anyone not in the assignment group
* - Internet resources of any kind.
*
* This assignment is subject to inspection by plagiarism detection software.
*
* Evidence of plagiarism will be forwarded to the Faculty of Science's disciplinary
* officer.
*/```

 Subject Computer Due By (Pacific Time) 03/15/2015 12:00 am
TutorRating
pallavi

Chat Now!

out of 1971 reviews
amosmm

Chat Now!

out of 766 reviews
PhyzKyd

Chat Now!

out of 1164 reviews
rajdeep77

Chat Now!

out of 721 reviews
sctys

Chat Now!

out of 1600 reviews

Chat Now!

out of 770 reviews
topnotcher

Chat Now!

out of 766 reviews
XXXIAO

Chat Now!

out of 680 reviews