Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Urgent help needed in JavaThanx ! :) PostfixTester.java import java.util.Stack; import java.util.Scanner; /** * Represents an integer evaluator of postfix expressions. Assumes * the operands

Urgent help needed in JavaThanx ! :)

image text in transcribed

PostfixTester.java

import java.util.Stack;

import java.util.Scanner;

/**

* Represents an integer evaluator of postfix expressions. Assumes

* the operands are constants.

*

* @author Lewis and Chase

* @version 4.0

*/

public class PostfixEvaluator

{

private final static char ADD = '+';

private final static char SUBTRACT = '-';

private final static char MULTIPLY = '*';

private final static char DIVIDE = '/';

private Stack stack;

/**

* Sets up this evalutor by creating a new stack.

*/

public PostfixEvaluator()

{

stack = new Stack();

}

/**

* Evaluates the specified postfix expression. If an operand is

* encountered, it is pushed onto the stack. If an operator is

* encountered, two operands are popped, the operation is

* evaluated, and the result is pushed onto the stack.

* @param expr string representation of a postfix expression

* @return value of the given expression

*/

public int evaluate(String expr)

{

int op1, op2, result = 0;

String token;

Scanner parser = new Scanner(expr);

while (parser.hasNext())

{

token = parser.next();

if (isOperator(token))

{

op2 = (stack.pop()).intValue();

op1 = (stack.pop()).intValue();

result = evaluateSingleOperator(token.charAt(0), op1, op2);

stack.push(new Integer(result));

}

else

stack.push(new Integer(Integer.parseInt(token)));

}

return result;

}

/**

* Determines if the specified token is an operator.

* @param token the token to be evaluated

* @return true if token is operator

*/

private boolean isOperator(String token)

{

return ( token.equals("+") || token.equals("-") ||

token.equals("*") || token.equals("/") );

}

/**

* Peforms integer evaluation on a single expression consisting of

* the specified operator and operands.

* @param operation operation to be performed

* @param op1 the first operand

* @param op2 the second operand

* @return value of the expression

*/

private int evaluateSingleOperator(char operation, int op1, int op2)

{

int result = 0;

switch (operation)

{

case ADD:

result = op1 + op2;

break;

case SUBTRACT:

result = op1 - op2;

break;

case MULTIPLY:

result = op1 * op2;

break;

case DIVIDE:

result = op1 / op2;

}

return result;

}

}

PostfixEvaluator.java

import java.util.Scanner;

/**

* Demonstrates the use of a stack to evaluate postfix expressions.

*

* @author Lewis and Chase

* @version 4.0

*/

public class PostfixTester

{

/**

* Reads and evaluates multiple postfix expressions.

*/

public static void main(String[] args)

{

String expression, again;

int result;

Scanner in = new Scanner(System.in);

do

{

PostfixEvaluator evaluator = new PostfixEvaluator();

System.out.println("Enter a valid post-fix expression one token " +

"at a time with a space between each token (e.g. 5 4 + 3 2 1 - + *)");

System.out.println("Each token must be an integer or an operator (+,-,*,/)");

expression = in.nextLine();

result = evaluator.evaluate(expression);

System.out.println();

System.out.println("That expression equals " + result);

System.out.print("Evaluate another expression [Y/N]? ");

again = in.nextLine();

System.out.println();

}

while (again.equalsIgnoreCase("y"));

}

}

Part A. 1. Copy and paste the class called PostfixTester from the slide and name your file to be PostfixTester java. 2. Copy and paste the class called PostfixEvaluator from the slide and name your file to be PostfixEvaluator java. 3. Compile these two files (%javac Postfix"java) and execute the program by running PostfixTester (% Java PostfixTester) Part B. Modify the program from Part A to include two additional operators. (the exponentiation operator) and % (the modulo operator) so that your new program can perform exponentiation operations and modulo operations. For example, the postfix expression 2 3 means 2 to the power of 3 and your program should output the answer 8, and the postfix expression 7 3 % represents the remainder of 7 divided by 3, which yields 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions