|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Building a Java Expression Tree; PLEASE HELP
Hey all. I need to build an expression tree that can perform standard arithmetic operations. I organized the project into 7 classes and an interface, and here are descriptions of what each is supposed to do:
1) Expression.java is an interface which the expression tree nodes will implement. It supports three operations: toString(), height(), and compute(). This is complete 2) FloatNode.java contains code for a node that just holds a float. This is complete 3) OperatorNode.java is an abstract class which the AddNode, SubNode, MultNode, and DivNode classes will inherit from. There are a number of methods which are not marked as abstract, and so are to be filled in here. This is incomplete 4) AddNode.java and SubNode.java contain code for the addition and subtraction nodes in the evaluation tree. I've done the constructors and createNew() methods, but still need to do the compute() methods. These are incomplete 5) MultNode.java and DivNode.java should contain code for the multiplication and division nodes in the evaluation tree. They should be really similar to AddNode and SubNode. These are incomplete 6) Driver.java is the test class. Where I am asking for help: I do not know where to go from here. I need to complete OperatorNode first, and then if I can get AddNode I will be able to do the rest. What is confusing me is that OperatorNode is an abstract class. I understand this concept in theory (ie that other classes extend the class as their parent class, somewhat like an interface) but when it actually comes time to program it I am confused. So if someone could please help here it would be much appreciated. The code: Expression.java -- The interface Code:
/**
* Do not change this interface!
*/
public interface Expression {
/**
* Returns a fully parenthesized inorder String representation of
* this expression.
* Example: ((1+2)*(3-(4/5)))
* Example: 142
*/
public String toString();
/**
* Computes and returns the value of this expression as a float.
*/
public float compute();
/**
* Returns the height of this expression tree. Note, the height of a leaf
* node is one.
*/
public int height();
}
FloatNode.java Code:
/**
* This file is fully implemented
*/
public class FloatNode implements Expression {
float f;
/**
* Constructs a FloatNode that represents the specified number
* @param a
*/
public FloatNode(float a) {f = a;}
/**
* @see Expression#height()
*/
public int height()
{
return 0;
}
/**
* @see Expression#compute()
*/
public float compute()
{
return f;
}
/**
* @see Expression#toString()
*/
public String toString()
{
return "" + f; // use implicit type coercion to get a string from a double
}
}
OperatorNode.java -- WHERE I NEED HELP Code:
public abstract class OperatorNode implements Expression {
protected Expression left;
protected Expression right;
// the "protected" keyword means only this class and
// its subclasses can access these variables
// TODO: modify as needed for getNumCreated() method
String operatorString;
Expression getLeft() { return left; }
Expression getRight() { return right; }
public OperatorNode(Expression l, Expression r, String s)
{ // TODO: modify as needed for getNumCreated() method
//Do I want to just instantiate the variables? ie left = l; right = r; operatorString = s; ?
}
/**
* Returns the total number of OperatorNodes created
* since the program began.
* You will need to add an instance variable and modify the
* OperatorNode constructor to get this to work.
*/
public static int getNumCreated()
{
/* TODO Fill in code here */
return 0;
}
/**
* @ returns the height of the tree where this operator node is the root
*/
public int height()
{
/* TODO Fill in code here */
while ( )
return 0;
}
/**
* implemented by other classes
*/
public abstract float compute();
/**
* Returns a fully parenthesized inorder String representation of
* this expression.
* Example: ((1+2)*(3-(4/5)))
* Example: 142
*/
public String toString()
{
/* TODO Fill in code here */
return "";
}
/**
* Creates a new OperatorNode of the same type as this one.
* We don't know the exact type of this OperatorNode yet,
* so this method is marked abstract.
* Subclasses of OperatorNode will have methods to create copies
* of themselves.
* @param left The left child of the newly created instance
* @param right The right child of the newly created instance
*/
public abstract OperatorNode createNew(Expression left, Expression right);
/**
* Commutes this expression over the operator.
* Expect an expression (a op b) , and produce (b op a) .
* If the operator does not commute, results of calculating the
* resulting expression may not be equivalent to those of the
* initial expression.
*
* Do not modify any nodes; instead, create a new one to return.
*/
public Expression commute()
{
/* TODO Fill in code here */
return null;
}
/**
* As commute(), but modify the node rather than creating a new one.
*/
public Expression commuteMutating()
{
/* TODO Fill in code here */
return null;
}
}
AddNode -- multnode, divnode, and subnode are all similar to this and I will not post the code because if we can get AddNode I will be able to do them. Code:
public class AddNode extends OperatorNode {
/**
* Constructs a new expression that will add the two parameters
* @param l The left child of this expression
* @param r The right child of this expression
*/
public AddNode(Expression l, Expression r)
{
super(l, r, "+");
}
/**
* Computes the value of this AddNode
*
* @see Expression#compute()
*/
public float compute()
{
/* TODO Fill in code here */
return 0;
}
/**
* Creates a new AddNode with the specified children
*
* @param l The left child of the returned expression
* @param r The right child of the returned expression
*/
public OperatorNode createNew(Expression l, Expression r)
{
return new AddNode(l, r);
}
}
Thank you for your help |
![]() |
| Viewing: ASP Free Forums > Other > Programming Help > Building a Java Expression Tree; PLEASE HELP |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|