Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to fill in the handleOperand, handleOperator, handleOpenBracket, handleCl

ID: 3588063 • Letter: I

Question

I need to fill in the handleOperand, handleOperator, handleOpenBracket, handleCloseBracket, handle Remaining Operator methods at the bottom of the code. This is for a infix expression evaluator in java using the stack. Could anybody please help me out?

package cs445.a2;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.StreamTokenizer;

/**

* This class uses two stacks to evaluate an infix arithmetic expression from an

* InputStream. It should not create a full postfix expression along the way; it

* should convert and evaluate in a pipelined fashion, in a single pass.

*/

public class InfixExpressionEvaluator {

// Tokenizer to break up our input into tokens

StreamTokenizer tokenizer;

// Stacks for operators (for converting to postfix) and operands (for

// evaluating)

StackInterface operatorStack;

StackInterface operandStack;

/**

* Initializes the evaluator to read an infix expression from an input

* stream.

* @param input the input stream from which to read the expression

*/

public InfixExpressionEvaluator(InputStream input) {

// Initialize the tokenizer to read from the given InputStream

tokenizer = new StreamTokenizer(new BufferedReader(

new InputStreamReader(input)));

// StreamTokenizer likes to consider - and / to have special meaning.

// Tell it that these are regular characters, so that they can be parsed

// as operators

tokenizer.ordinaryChar('-');

tokenizer.ordinaryChar('/');

// Allow the tokenizer to recognize end-of-line, which marks the end of

// the expression

tokenizer.eolIsSignificant(true);

// Initialize the stacks

operatorStack = new ArrayStack();

operandStack = new ArrayStack();

}

/**

* Parses and evaluates the expression read from the provided input stream,

* then returns the resulting value

* @return the value of the infix expression that was parsed

*/

public Double evaluate() throws ExpressionError {

// Get the first token. If an IO exception occurs, replace it with a

// runtime exception, causing an immediate crash.

try {

tokenizer.nextToken();

} catch (IOException e) {

throw new RuntimeException(e);

}

// Continue processing tokens until we find end-of-line

while (tokenizer.ttype != StreamTokenizer.TT_EOL) {

// Consider possible token types

switch (tokenizer.ttype) {

case StreamTokenizer.TT_NUMBER:

// If the token is a number, process it as a double-valued

// operand

handleOperand((double)tokenizer.nval);

break;

case '+':

case '-':

case '*':

case '/':

case '^':

// If the token is any of the above characters, process it

// is an operator

handleOperator((char)tokenizer.ttype);

break;

case '(':

case '<':

// If the token is open bracket, process it as such. Forms

// of bracket are interchangeable but must nest properly.

handleOpenBracket((char)tokenizer.ttype);

break;

case ')':

case '>':

// If the token is close bracket, process it as such. Forms

// of bracket are interchangeable but must nest properly.

handleCloseBracket((char)tokenizer.ttype);

break;

case StreamTokenizer.TT_WORD:

// If the token is a "word", throw an expression error

throw new ExpressionError("Unrecognized token: " +

tokenizer.sval);

default:

// If the token is any other type or value, throw an

// expression error

throw new ExpressionError("Unrecognized token: " +

String.valueOf((char)tokenizer.ttype));

}()

// Read the next token, again converting any potential IO exception

try {

tokenizer.nextToken();

} catch(IOException e) {

throw new RuntimeException(e);

}

}

// Almost done now, but we may have to process remaining operators in

// the operators stack

handleRemainingOperators();

// Return the result of the evaluation

// TODO: Fix this return statement

return null;

}

/**

* This method is called when the evaluator encounters an operand. It

* manipulates operatorStack and/or operandStack to process the operand

* according to the Infix-to-Postfix and Postfix-evaluation algorithms.

* @param operand the operand token that was encountered

*/

void handleOperand(double operand) {

// TODO: Complete this method

}

/**

* This method is called when the evaluator encounters an operator. It

* manipulates operatorStack and/or operandStack to process the operator

* according to the Infix-to-Postfix and Postfix-evaluation algorithms.

* @param operator the operator token that was encountered

*/

void handleOperator(char operator) {

// TODO: Complete this method

}

/**

* This method is called when the evaluator encounters an open bracket. It

* manipulates operatorStack and/or operandStack to process the open bracket

* according to the Infix-to-Postfix and Postfix-evaluation algorithms.

* @param openBracket the open bracket token that was encountered

*/

void handleOpenBracket(char openBracket) {

// TODO: Complete this method

}

/**

* This method is called when the evaluator encounters a close bracket. It

* manipulates operatorStack and/or operandStack to process the close

* bracket according to the Infix-to-Postfix and Postfix-evaluation

* algorithms.

* @param closeBracket the close bracket token that was encountered

*/

void handleCloseBracket(char closeBracket) {

// TODO: Complete this method

}

/**

* This method is called when the evaluator encounters the end of an

* expression. It manipulates operatorStack and/or operandStack to process

* the operators that remain on the stack, according to the Infix-to-Postfix

* and Postfix-evaluation algorithms.

*/

void handleRemainingOperators() {

// TODO: Complete this method

}

/**

* Creates an InfixExpressionEvaluator object to read from System.in, then

* evaluates its input and prints the result.

* @param args not used

*/

public static void main(String[] args) {

System.out.println("Infix expression:");

InfixExpressionEvaluator evaluator =

new InfixExpressionEvaluator(System.in);

Double value = null;

try {

value = evaluator.evaluate();

} catch (ExpressionError e) {

System.out.println("ExpressionError: " + e.getMessage());

}

if (value != null) {

System.out.println(value);

} else {

System.out.println("Evaluator returned null");

}

}

}

Explanation / Answer

/* Hi , I have wrote the incomlete function. In the given StackInterface class is used may be it was define it in your custom package.

I have added the respective code in all the TODO function. when I ran my code in my own code it works.

I have not have the package menton in the code and respective classess.

But all the TODO funtion is worknig

*/

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.StreamTokenizer;

/**

* This class uses two stacks to evaluate an infix arithmetic expression from an

* InputStream. It should not create a full postfix expression along the way; it

* should convert and evaluate in a pipelined fashion, in a single pass.

*/

public class InfixExpressionEvaluator {

// Tokenizer to break up our input into tokens

// Stacks for operators (for converting to postfix) and operands (for

// evaluating)

StreamTokenizer tokenizer;

StackInterface operatorStack;

StackInterface operandStack;

/**

* Initializes the evaluator to read an infix expression from an input

* stream.

* @param input the input stream from which to read the expression

*/

public InfixExpressionEvaluator(InputStream input) {

// Initialize the tokenizer to read from the given InputStream

tokenizer = new StreamTokenizer(new BufferedReader(

new InputStreamReader(input)));

// StreamTokenizer likes to consider - and / to have special meaning.

// as operators

// Tell it that these are regular characters, so that they can be parsed

tokenizer.ordinaryChar('-');

tokenizer.ordinaryChar('/');

// Allow the tokenizer to recognize end-of-line, which marks the end of

// the expression

tokenizer.eolIsSignificant(true);

// Initialize the stacks

operatorStack = new ArrayStack();

operandStack = new ArrayStack();

}

/**

* Parses and evaluates the expression read from the provided input stream,

* then returns the resulting value

* @return the value of the infix expression that was parsed

*/

public double evaluate() throws ExpressionError {

// Get the first token. If an IO exception occurs, replace it with a

// runtime exception, causing an immediate crash.

try {

tokenizer.nextToken();

} catch (IOException e) {

throw new RuntimeException(e);

}

// Continue processing tokens until we find end-of-line

while (tokenizer.ttype != StreamTokenizer.TT_EOL) {

// Consider possible token types

switch (tokenizer.ttype) {

case StreamTokenizer.TT_NUMBER:

// If the token is a number, process it as a double-valued

// operand

handleOperand((double)tokenizer.nval);

break;

case '+':

case '-':

case '*':

case '/':

case '^':

// If the token is any of the above characters, process it

// is an operator

handleOperator((char)tokenizer.ttype);

break;

case '(':

case '<':

// If the token is open bracket, process it as such. Forms

// of bracket are interchangeable but must nest properly.

handleOpenBracket((char)tokenizer.ttype);

break;

case ')':

case '>':

// If the token is close bracket, process it as such. Forms

// of bracket are interchangeable but must nest properly.

handleCloseBracket((char)tokenizer.ttype);

break;

case StreamTokenizer.TT_WORD:

// If the token is a "word", throw an expression error

throw new ExpressionError("Unrecognized token: " +

tokenizer.sval);

default:

// If the token is any other type or value, throw an

// expression error

throw new ExpressionError("Unrecognized token: " +

String.valueOf((char)tokenizer.ttype));

}()

// Read the next token, again converting any potential IO exception

try {

tokenizer.nextToken();

} catch(IOException e) {

throw new RuntimeException(e);

}

}

// Almost done now, but we may have to process remaining operators in

// the operators stack

handleRemainingOperators();

// Return the result of the evaluation

// TODO: Fix this return statement

return null;

}

/**

* This method is called when the evaluator encounters an operand. It

* manipulates operatorStack and/or operandStack to process the operand

* according to the Infix-to-Postfix and Postfix-evaluation algorithms.

* @param operand the operand token that was encountered

*/

void handleOperand(double operand) {

operandStack.push(operand);

// TODO: Complete this method

}

/**

* This method is called when the evaluator encounters an operator. It

* manipulates operatorStack and/or operandStack to process the operator

* according to the Infix-to-Postfix and Postfix-evaluation algorithms.

* @param operator the operator token that was encountered

*/

void handleOperator(char operator) {

if (operator == '^' || operator == '+' || operator == '-' ||

operator == '*' || operator == '/' )

{

while (!operatorStack.empty() && hasPrecedence(operator , operatorStack.peek()))

operandStack.push(applyOperator(operatorStack.pop(), operandStack.pop(), operandStack.pop()));

// Push current token to 'operatorStack'.

operatorStack.push(operator);

}

// TODO: Complete this method

}

/**

* This method is called when the evaluator encounters an open bracket. It

* manipulates operatorStack and/or operandStack to process the open bracket

* according to the Infix-to-Postfix and Postfix-evaluation algorithms.

* @param openBracket the open bracket token that was encountered

*/

void handleOpenBracket(char openBracket) {

// TODO: Complete this method

operatorStack.push(openBracket);

}

/**

* This method is called when the evaluator encounters a close bracket. It

* manipulates operatorStack and/or operandStack to process the close

* bracket according to the Infix-to-Postfix and Postfix-evaluation

* algorithms.

* @param closeBracket the close bracket token that was encountered

*/

void handleCloseBracket(char closeBracket) {

// TODO: Complete this method operatorStack = new ArrayStack();operandStack = new ArrayStack();

while (operatorStack.peek() != '(' || operatorStack.peek() != '<')

operandStack.push(applyOperator(operatorStack.pop(), operandStack.pop(), operandStack.pop()));

operatorStack.pop();}

/**

* This method is called when the evaluator encounters the end of an

* expression. It manipulates operatorStack and/or operandStack to process

* the operators that remain on the stack, according to the Infix-to-Postfix

* and Postfix-evaluation algorithms.

*/

void handleRemainingOperators() {

// TODO: Complete this method

while (!operatorStack.empty())
operandStack.push(applyOperator(operatorStack.pop(), operandStack.pop(), operandStack.pop()));

}

public boolean hasPrecedence(char operand1, char operand2)

{

if (operand2 == '(' || operand2 == ')')

return false;

if ((operand1 == '*' || operand1 == '/') && (operand2 == '+' || operand2 == '-'))

return false;

else

return true;

}

public double applyOperator(char operator, double x, double y)

{

switch (operator)

{

case '^':

return Math.pow(x, y);

case '+':

return x + y;

case '-':

return x - y;

case '*':

return x * y;

case '/':

if (y == 0)

throw new

UnsupportedOperationException("Cannot divide by zero");

return x / y;

}

return 0;

}

/**

* Creates an InfixExpressionEvaluator object to read from System.in, then

* evaluates its input and prints the result.

* @param args not used

*/

public static void main(String[] args) {

System.out.println("Infix expression:");

InfixExpressionEvaluator evaluator =

new InfixExpressionEvaluator(System.in);

Double value = null;

try {

value = evaluator.evaluate();

} catch (ExpressionError e) {

System.out.println("ExpressionError: " + e.getMessage());

}

if (value != null) {

System.out.println(value);

} else {

System.out.println("Evaluator returned null");

}

}

}