Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

CSCI162 - Lab 09 1 Lab 09: Postx Evaluator 1 Objective In this assignment you will read and gure out how to use several classes that I provide, write your own class from scratch, and implement a...

1 answer below »

CSCI162 - Lab 09 1

Lab 09: Postx Evaluator

1 Objective

In this assignment you will read and gure out how to use several classes that I provide, write your

own class from scratch, and implement a classic use of stacks.

2 Setup

Create a new Java Project (Lab09). Download the Lab09Files.zip le, unzip it, and place the

les it contains into your project's source folder. You should have the following les:

Token A class that represents a component of an arithmetic expression (a number, an operator, or

a parenthesis).

TokenScanner A class that breaks apart a mathematical expression into individual tokens.

P02TestFunctionality My tests for the class that you will write.

PostfixProgram A very simple program you can run to manually test your code.

You will be using the Token and TokenScanner classes, so you will need to read their method

specications carefully. (You do not need to understand how their methods work, but you do need

to understand exactly what each of their methods does.) You should nd that Token has public

methods named isNumber, isOperator, getNumberValue, and getSymbol while TokenScanner has

methods named hasNextToken, nextToken, and reachedEnd. You also need to use and understand

the StackE> class that comes with the Java Class Libraries, but you should already be familiar

with it.

3 Class Specications

You must create your own class, which must be named PostfixEvaluator. This class has one

required method:

1 /**

2 * Ev a l u a t e s a p o s t f i x mathemat i cal e x p r e s s i o n .

3 *

4 * @param inp u t A S t r i n g c o n t a i n i n g t h e e x p r e s s i o n .

5 * @return A S t r i n g c o n t a i n i n g t h e v a l u e o f t h e e x p r e s s i on , or an e r r o r message i f

t h e r e was a problem .

6 */

7 public static String evaluate ( String input )

It may also contain as many static helper methods as you would like. It should not have

any static variables, instance variables, or instance methods. This method must implement the

algorithm that we discussed in class, using a StackDouble>.

CSCI162 - Lab 09 2

This method should not allow any exceptions to escape it. This means both that you should not

directly throw any exceptions and that if the other methods you are calling throw exceptions you

should catch them. (See section below.) Instead, you should return precisely the error messages

below for the dierent exceptional situations that might occur:

input string is empty or all spaces or tabs "No input."

empty stack when time to pop "Stack Underflow. Not enough operands on stack."

values remain on stack after end of input "Computed answer, but values remain on stack."

evaluation ended before end of input "Computed answer, but not all input used."

parentheses in input "( has no meaning here." / ") has no meaning here."

Note that in order to simplify the TokenScanner class, we do not allow negative numbers in

the input. (One can be created by adding 0 5 - where you wanted -5.)

4 Instructions

It should be possible to avoid any exceptions being thrown, but you can also allow them to be

thrown and then catch them. The following code sample demonstrates the idea:

1 String result = "";

2 try {

3 callADangerousMethod ();

4 result = callAnotherDangerousMethod ();

5 }

6 catch ( IllegalArgumentException exception ) {

7 result = "I guess I used a bad argument .";

8 }

9 catch ( IllegalStateException exception ) {

10 result = "I guess I wasn 't allowed to do that .";

11 }

The rst new thing we see here is the try block. If we are going to do something that could

potentially cause an exception to be thrown and we want the ability to catch it, we need to do it

inside a try block.

Then we see that a try-block is followed by one or more catch blocks. If the code in the try

block does not throw any exceptions, all of the catch blocks are ignored. But if the code in the try

block does throw an exception, the rest of the try block is skipped and the rst catch block that

matches the type of the exception thrown is run, then the program continues. If no catch block

matches the exception type, then the exception moves on to the method that called this one, and

continues in that way until it either gets caught or runs out of methods and crashes the program.

5 Grading

AutoLab Problem Points Autograded Description

01 code 90 yes Do all of the methods work?

02 style 10 partial Is the program written with good style?

CSCI162 - Lab 09 3

6 Submitting Your Work

Make sure that you have submitted your nal version of your PostfixEvaluator.java le to

AutoLab, and that you have 100/100 autograded points.

You do not need to submit your work through D2L { I will look at whatever your last AutoLab

submission is.

Answered Same Day Nov 13, 2021

Solution

Arun Shankar answered on Nov 15 2021
135 Votes
import java.util.Stack;
public class PostfixEvaluato
{
    /**
    * Evaluates a postfix mathematical expression.
    *
    * @param input A String containing the expression.
    * @return A String containing the value of the expression,
    * or an e
or message if there was a problem.
    *
    
    
input string is empty or all spaces or tabs "No input."
    
empty stack when time to pop "Stack Underflow. Not enough operands on stack."
    
values remain on stack after end of input "Computed answer, but values remain on...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here