Question: Need help with this java assignment. Part 1: Class Expression : This is an abstract class representing those features common all expressions regardless of notation.
Need help with this java assignment.
Part 1: Class Expression:
This is an abstract class representing those features common all expressions regardless of notation.
| Expression |
| - expression: String = null -tokens: ArrayList = null |
| + Expression( in expr: String): constructor + getExpression() : String + getTokens() : ArrayList # isOperand( in str: String): boolean # isOperator( in str: String): boolean # getOperandValue( in str: String) : double # evaluateSubExpression( in op1: double, in operator: String, in op2: double) : double + isLegal(): boolean [abstract]* + evaluate(): double [abstract] |
Interface for class Expression
public Expression( String str) : This constructor receives a String containing the expression text. It will then break the expressions into tokens (individual symbols separated by spaces) and store the resulting String array into the data member tokenList.
protected static boolean isOperand(String str): returns true if the String parameters is a valid operand (double number)
protected static double getOperandValue(String str): given a String containing a valid operand this method returns the value of the operand as type double.
protected static boolean isOperator(String str): returns true if the String parameter contains one of +, -, *, / or %.
public String getExpression(): returns the expression string.
public ArrayList getTokens(): returns the token list
protected static double evaluateSubExpression( double operand1, String operator, double operand2): This method evaluates a subexpression as:
operand1 operator operand2
and returns the result as type double.
public abstract boolean isLegal(): abstract method to be implement in each subclass.* public abstract double evaluate(): abstract method to be implement in each subclass.
| While it could also make sense to not let the user create an invalid expression in the first place, lets allow the creation of illegal expressions as an excuse to practice more with subclasses and super constructors This raises the question of when to check the legality of the expression. To check legality, we must scan through all the tokens. Should we do this just once and save the result? Or should we do it every time? You can decide what approach to take. If you want to check legality only once, my recommendation is to check if an expression is legal in the Postfix and Prefix constructors. Then set a global private final boolean flag. If you take this approach, isLegal() would not need to be abstract because it would do the same thing regardless of being prefix or postfix isLegal() would just return the boolean flag. |
Part 2: Class PrefixExpression: 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.
Part 3: Class PostfixExpression: This class extends class Expression and represents PostfixExpressions
Interface for class PostfixExpression
public PostfixExpression( 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 last token in the tokenList is an operator, AND the first two tokens are operands, AND the number of operands in the expression is equal to the number of operators plus 1.
| PostfixExpression |
| NO ADDITIONAL DATA MEMBERS |
| + PostfixExpression( in expr: String): constructor + isLegal(): boolean + evaluate(): double |
public double evaluate(): This method uses a stack of type Double to evaluate the postfix expression.
o Ensure the expression is legal, if not Stop and throw an exception, using class ArithmeticException
o Obtain the tokenList o if the # of tokens in the tokenList < 3, Stop and throw an exception,
using class ArithmeticException
o Iterate through the token list ? if the current token is an operand, convert it to type double and
push onto the stack. ? if the current token is an operator:
if the size of the stack is >=2, o pop a value from the stack into operand2, and
pop a second value from the stack into operand1 o Evaluate the subexpression.. calling
evaluateSubExpression o push the result back onto the stack
else o Stop and throw an exception, using class
ArithmeticException
o When done (you are at the end of the token list): o if ONE value remains on the stack, pop and return the value o else Stop and throw an exception, using class ArithmeticException
Given code and junit test:
package project2;
import java.util.ArrayList;
public abstract class Expression {
// Feel free to create other private/protected members, methods, or constructors.
protected String expression;
protected ArrayList tokens;
public Expression(String expression){
}
public String getExpression() {
}
public ArrayList getTokens() {
}
protected static boolean isOperand(String token) {
}
protected static boolean isOperator(String token) {
}
protected static double getOperandValue(String token) throws NumberFormatException {
}
protected static double evaluateSubExpression(double operand1, String operator, double operand2) {
}
public abstract boolean isLegal(); // See my comment on page 3 of instructions.
public abstract double evaluate();
}
package project2;
public class PostfixExpression extends Expression {
// Feel free to create other private/protected members, methods, or constructors.
public PostfixExpression(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() {
}
}
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() {
}
}
package project2;
import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.AfterClass; import org.junit.Test;
public class ExpressionTest { private static final double DELTA = 0.00000000001; private static final double LEGAL_DRINKING_AGE = 21; private static int score = 0;
@AfterClass public static void printScore() { System.out.println(score + "/90 + ?/10 Documentation"); }
// Testing Getters @Test public void testGetPrefixExpression() { assertEquals("", new PrefixExpression("").getExpression()); assertEquals(" ", new PrefixExpression(" ").getExpression()); assertEquals("+", new PrefixExpression("+").getExpression()); assertEquals("1", new PrefixExpression("1").getExpression()); assertEquals("+ 1.0 .1", new PrefixExpression("+ 1.0 .1").getExpression()); assertEquals("- 0.5 -.1", new PrefixExpression("- 0.5 -.1").getExpression()); assertEquals("0.5 -.1 /", new PrefixExpression("0.5 -.1 /").getExpression()); assertEquals("0.5 -.1 * % abc", new PrefixExpression("0.5 -.1 * % abc").getExpression()); assertEquals("", new PostfixExpression("").getExpression()); assertEquals(" ", new PostfixExpression(" ").getExpression()); assertEquals("+", new PostfixExpression("+").getExpression()); assertEquals("1", new PostfixExpression("1").getExpression()); assertEquals("+ 1.0 .1", new PostfixExpression("+ 1.0 .1").getExpression()); assertEquals("- 0.5 -.1", new PostfixExpression("- 0.5 -.1").getExpression()); assertEquals("0.5 -.1 /", new PostfixExpression("0.5 -.1 /").getExpression()); assertEquals("0.5 -.1 * % abc", new PostfixExpression("0.5 -.1 * % abc").getExpression()); score += 5; }
@Test public void testGetTokens() { assertEquals(Arrays.asList("+"), new PrefixExpression("+").getTokens()); assertEquals(Arrays.asList("1"), new PrefixExpression("1").getTokens()); assertEquals(Arrays.asList("+", "1.0", "0.1"), new PrefixExpression("+ 1.0 0.1").getTokens()); assertEquals(Arrays.asList("-", "0.5", "-0.1"), new PrefixExpression("- 0.5 -0.1").getTokens()); assertEquals(Arrays.asList("0.5", "-0.1", "*", "%", "abc"), new PrefixExpression("0.5 -0.1 * % abc").getTokens()); assertEquals(Arrays.asList("+"), new PostfixExpression("+").getTokens()); assertEquals(Arrays.asList("1"), new PostfixExpression("1").getTokens()); assertEquals(Arrays.asList("+", "1.0", "0.1"), new PostfixExpression("+ 1.0 0.1").getTokens()); assertEquals(Arrays.asList("-", "0.5", "-0.1"), new PostfixExpression("- 0.5 -0.1").getTokens()); assertEquals(Arrays.asList("0.5", "-0.1", "*", "%", "abc"), new PostfixExpression("0.5 -0.1 * % abc").getTokens()); score += 10; } @Test public void testIsLegalPrefixExpression() { assertFalse(new PrefixExpression("").isLegal()); assertFalse(new PrefixExpression(" ").isLegal()); assertFalse(new PrefixExpression("-").isLegal()); assertFalse(new PrefixExpression("1 1 -").isLegal()); assertFalse(new PrefixExpression("+ 1- 1").isLegal()); assertFalse(new PrefixExpression("1 / 2").isLegal()); assertFalse(new PrefixExpression("+ 1 2 3 -").isLegal()); assertFalse(new PrefixExpression("* / 5. -2 1 - 2").isLegal()); assertTrue(new PrefixExpression("+ 1 2").isLegal()); assertTrue(new PrefixExpression("+ 1 - 2 3").isLegal()); assertTrue(new PrefixExpression("/ 2 1.5").isLegal()); assertTrue(new PrefixExpression("% .3 -47.99").isLegal()); assertTrue(new PrefixExpression("+ 1 2 3 4 + + 5 + + + 6 7").isLegal()); score += 10; } @Test public void testIsLegalPostfixExpression() { assertFalse(new PostfixExpression("").isLegal()); assertFalse(new PostfixExpression(" ").isLegal()); assertFalse(new PostfixExpression("-").isLegal()); assertFalse(new PostfixExpression("- 1 1").isLegal()); assertFalse(new PostfixExpression("1 1- +").isLegal()); assertFalse(new PostfixExpression("1 / 2").isLegal()); assertFalse(new PostfixExpression("+ 1 2 3 -").isLegal()); assertFalse(new PostfixExpression("5. -2 1 - 2 % * / ").isLegal()); assertTrue(new PostfixExpression("1 2 +").isLegal()); assertTrue(new PostfixExpression("1 2 + 3 -").isLegal()); assertTrue(new PostfixExpression("2 1.5 / ").isLegal()); assertTrue(new PostfixExpression(".3 -47.99 %").isLegal()); assertTrue(new PostfixExpression("1 2 3 4 + + 5 + + + 6 7 +").isLegal()); score += 10; } @Test public void evalSubExpression() { assertEquals(1.0, Expression.evaluateSubExpression(-1, "+", 2), DELTA); assertEquals(-1.0, Expression.evaluateSubExpression(1, "-", 2), DELTA); assertEquals(0.0, Expression.evaluateSubExpression(1, "*", 0), DELTA); assertEquals(0.5, Expression.evaluateSubExpression(-1, "/", -2), DELTA); assertEquals(0.0, Expression.evaluateSubExpression(4, "%", 2), DELTA); score += 15; } @Test public void evalPrefixExpression() { assertEquals(LEGAL_DRINKING_AGE, new PrefixExpression("* - 9 2 3").evaluate(), DELTA); assertEquals(10.5, new PrefixExpression("/ * + 5 2 3 2").evaluate(), DELTA); assertEquals(0.5, new PrefixExpression("% / * + 5 2 3 2 2").evaluate(), DELTA); assertEquals(0.5, new PrefixExpression("/ * - 9 11 -.5 2").evaluate(), DELTA); score += 20; } @Test public void evalPostfixExpression() { assertEquals(LEGAL_DRINKING_AGE, new PostfixExpression("3 9 2 - *").evaluate(), DELTA); assertEquals(10.5, new PostfixExpression("3 5 2 + * 2 /").evaluate(), DELTA); assertEquals(0.5, new PostfixExpression("3 5 2 + * 2 / 2 %").evaluate(), DELTA); assertEquals(0.5, new PostfixExpression("-.5 9 11 - * 2 /").evaluate(), DELTA); score += 20; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
