In this assignment, you’ll be implementing the “infix-to-postfix” and “postfix evaluation” algorithms simultaneously. You will then improve upon this by implementing error detection to give error messages about inputs, rather than just crashing.


Starting point

I hope project 1 taught you to never write an entire program before testing it. Right? Right?????? 😁

Download and extract these materials. Contained are:

Do not modify anything other than InfixExpressionEvaluator.java. We will be testing it with unmodified versions of the other files.

InfixExpressionEvaluator contains a good bit of code already. You do not need to change any of the methods we wrote. Instead, you will be filling in the methods which implement the “meat” of the algorithm. Basically, search for TODO: and do those things.

There is a main in InfixExpressionEvaluator so you can run it like java InfixExpressionEvaluator. It does nothing when you first download it, but it does run.


Implementing the TODO methods [70 points]

If you don’t have the book… ask your classmates.

Check out sections 5.16 and 5.18 in the book (4th ed.). Those outline what you are doing. We already did the switch-case for you; the methods you implement are the code inside those cases.

You’re going to “squish” those two algorithms into one. Rather than having an “output”, you will push the operands as you encounter them and evaluate the operators as you pop them. We’ve already given you two stacks to hold these things.

Here are the methods you must fill in. They are all called by evaluate() as it finds tokens in the input.

And last, you must fill in the return value of evaluate(). Right now it returns null. That’s bad. You want it to return the result of the expression evaluation.

Example inputs/outputs

Here are examples.

Suggestions

Finish this first section before moving onto error checking. Programs are like houses: you need a good foundation. Don’t skip steps if you run into trouble. It’s better to get a solid understanding of the algorithm and get a 70 than try to attempt everything, do poorly, and get a 50.


Error checking [30 points]

Now, your program should be able to evaluate correctly-formatted expressions, but people make mistakes. Right now, your program might crash if you write something like 4 + . That’s no good!

Change your code to detect problems like this and throw ExpressionError with descriptive error messages. Try everything! Try giving your expression evaluator complete garbage and see what it does! It should never crash.

Hints


Extra credit [+10 points]

You can earn up to 10 points of extra credit. If you do any extra credit, please follow the submission instructions for it below.

Here are some suggestions:


Testing

We will be testing your code on the command line. You are welcome to use an IDE, but before submitting, please test that your Java files can be compiled and run on the command line without it.

DO NOT TURN IN CODE THAT DOES NOT COMPILE.

This is not acceptable in this class or in your following classes. Comment out the code that doesn’t compile and put comments at the top of your Java files for the grader. You may receive some partial credit for such code.

Code that crashes may also receive partial credit.


Submission

You will submit a ZIP file named username_proj2.zip where username is your Pitt username.

Do not put a folder in the zip file, just the following file(s):

Do not submit any IDE project files.

Submit to the Box folder at this link. Drag your zip file into your browser to upload. If you can see your file, you uploaded it correctly!

You can also re-upload if you made a mistake and need to fix it.