Question
Question: Starting with the BinaryExpression learning activity solution from this week, Code a new class DebugVisitor that visits an expression tree and returns a list
Question: Starting with the BinaryExpression learning activity solution from this week, Code a new class DebugVisitor that visits an expression tree and returns a list of the calls made to the visitor. For example, if the expression tree represented the expression 3 + 5, then the output would be:
preVisit AddExpression
visit ConstantExpression 3
visit AddExpression
visit ConstantExpression 5
postVisit AddExpression
Hint: You can use obj.getClass().getSimpleName() to get the name of an object.
AddExpression.java
public class AddExpression extends BinaryExpression
{
public AddExpression(Expression left, Expression right)
{
super(left, right);
}
@Override
public Operation getOperation()
{
return Operation.ADD;
}
}
BinaryExpression.java
public abstract class BinaryExpression extends Expression
{
Expression leftChild;
Expression rightChild;
public BinaryExpression(Expression left, Expression right) {
leftChild = left;
rightChild = right;
}
@Override
public void accept(Visitor visitor)
{
visitor.preVisit(this);
leftChild.accept(visitor);
visitor.visit(this);
rightChild.accept(visitor);
visitor.postVisit(this);
}
public abstract Operation getOperation();
}
ConstantExpression.java
public class ConstantExpression extends Expression
{
Integer value;
public ConstantExpression(Integer value) {
this.value = value;
}
public Integer getValue() {
return value;
}
@Override
public void accept(Visitor visitor)
{
visitor.visit(this);
}
}
DebugVisitor.java
import java.util.*;
public class DebugVisitor implements Visitor {
public DebugVisitor() {
}
public List getResult() {
return null;
}
@Override
public void preVisit(BinaryExpression expr) {
}
@Override
public void visit(BinaryExpression expr) {
}
@Override
public void postVisit(BinaryExpression expr) {
}
@Override
public void visit(ConstantExpression expr) {
}
}
DivideExpression.java
public class DivideExpression extends BinaryExpression
{
public DivideExpression(Expression left, Expression right)
{
super(left, right);
}
@Override
public Operation getOperation()
{
return Operation.DIVIDE;
}
}
Expression.java
public abstract class Expression implements Visitable
{
}
MultiplyExpression.java
public class MultiplyExpression extends BinaryExpression
{
public MultiplyExpression(Expression left, Expression right)
{
super(left, right);
}
@Override
public Operation getOperation()
{
return Operation.MULTIPLY;
}
}
Operation.java
public abstract class Operation
{
private String value;
private Operation(String str)
{
this.value = str;
}
public abstract Integer apply(Integer left, Integer right);
public String toString()
{
return this.value;
}
public static final Operation ADD = new Operation("+") {
@Override
public Integer apply(Integer left, Integer right)
{
return left.intValue() + right.intValue();
}
};
public static final Operation SUBTRACT = new Operation("-") {
@Override
public Integer apply(Integer left, Integer right)
{
return left.intValue() - right.intValue();
}
};
public static final Operation MULTIPLY = new Operation("*") {
@Override
public Integer apply(Integer left, Integer right)
{
return left.intValue() * right.intValue();
}
};
public static final Operation DIVIDE = new Operation("/") {
@Override
public Integer apply(Integer left, Integer right)
{
return left.intValue() / right.intValue();
}
};
}
SubtractExpression.java
public class SubtractExpression extends BinaryExpression
{
public SubtractExpression(Expression left, Expression right)
{
super(left, right);
}
@Override
public Operation getOperation()
{
return Operation.SUBTRACT;
}
}
Visitable.java
public interface Visitable
{
void accept(Visitor visitor);
}
Visitor.java
public interface Visitor
{
public void preVisit(BinaryExpression expr);
public void visit(BinaryExpression expr);
public void postVisit(BinaryExpression expr);
public void visit(ConstantExpression expr);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started