Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with this part of my java assignment, comments appreciated. This class extends class Expression and represents PrefixExpressions Interface for class PrefixExpression public PrefixExpression(

Need help with this part of my java assignment, comments appreciated.

This class extends class Expression and represents PrefixExpressions

Interface for class PrefixExpression

public PrefixExpression( String str) : This constructor receives a String containing the expression text. It will invoke the constructor for class Expression, passing in the expression text.

public boolean isLegal(): This method returns true if the first token in the tokenList is an operator, AND the last two tokens are operands, AND the number of operands in the expression is equal to the number of operators plus 1.

PrefixExpression

NO ADDITIONAL DATA MEMBERS

+ PrefixExpression( in expr: String): constructor + isLegal(): boolean

+ evaluate(): double

public double evaluate(): This method uses a queue of Strings to evaluate the prefix expression.

o Ensure the expression is legal, if not Stop and throw an exception, using class ArithmeticException

o Enqueue all of the tokens in the tokenList into the queue o if the # of tokens in the queue >= 3,

? Dequeue the 1st 3 values. Into 3 String variables: ie: a, b, & c ? else Stop and throw an exception, using class

ArithmeticException o While not done

? If A is not an operator, and B & C are not operands, then we do not have a subexpression. So;

Enqueue A back onto the queue, and swap values

A =B

B=C

Dequeue a new value into C

Repeat the previous steps until A is an operator and B & C operands

? Once is we have a subexpression

Convert the operands into values of type double

Call the method evaluateSubexpression

Enqueue the result back onto the queue.

o If the number of elements in the queue equals 1, we are done.

o Else, dequeued 3 new values into a , b, & c and continue processing.

o When done (one value remaining on the queue) dequeue and return the result of the expression.

Expression Class:

package project2;

import java.util.ArrayList;

public abstract class Expression {

// data member

protected String expression;

protected ArrayList tokens;

// constructor

public Expression(String expression){

this.expression = expression;

tokens = new ArrayList();

String[] tokenList = expression.split(" "); // convert the string to array of tokens

// insert the tokens in the list

for(int i=0;i

tokens.add(tokenList[i]);

}

// method to return the expression

public String getExpression() {

return expression;

}

// method to return the tokens

public ArrayList getTokens() {

return tokens;

}

// method to check if the token is operand or not(i.e can be converted to double or not)

protected static boolean isOperand(String token) {

try {

double val = Double.valueOf(token);

return true; // token can be converted to double value

}catch(NumberFormatException e)

{

return false; // token cannot be converted to double value

}

}

// method to check if the token is operator or not

protected static boolean isOperator(String token) {

if(token.equals("+") || token.equals("-")|| token.equals("*") || token.equals("/") || token.equals("%"))

return true;

return false;

}

// method to return the value of the token(operand value)

protected static double getOperandValue(String token) throws NumberFormatException {

return(Double.valueOf(token));

}

// method to evaluate the subexpression and return the result

protected static double evaluateSubExpression(double operand1, String operator, double operand2) {

double result;

switch(operator) {

case "+" : result = operand1 + operand2;

break;

case "-" : result = operand1 - operand2;

break;

case "*" : result = operand1 * operand2;

break;

case "/" : result = operand1 / operand2;

break;

case "%" : result = operand1 % operand2;

break;

default : result = -1;

}

return result;

}

// abstract methods to be implemented in subclass

public abstract boolean isLegal(); // See my comment on page 3 of instructions.

public abstract double evaluate();

}

Prefix:

package project2;

public class PrefixExpression extends Expression {

public PrefixExpression(String expression) {

// Remember the super constructor

}

@Override

public boolean isLegal() {

/*

* See my comment on page 3 of instructions.

* You can decide to implement isLegal() in Expression class.

* In which case, you can get rid of this method here.

*/

}

@Override

public double evaluate() {

}

}

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

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

c. How is trust demonstrated?

Answered: 1 week ago

Question

Have issues been prioritized?

Answered: 1 week ago

Question

d. How will lack of trust be handled?

Answered: 1 week ago