Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsOtherProgramming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old June 24th, 2008, 12:11 PM
strakerc strakerc is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Location: Pittsburgh, PA
Posts: 7 strakerc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 16 m 3 sec
Reputation Power: 0
Send a message via AIM to strakerc
Help with a few methods in a java expression tree

I posted here yesterday without a response, but now figured a lot out since then. My goal is to build an expression tree (a binary tree that performs standard arithmetic) and I am stuck on a couple of methods.

I have organized my work into 7 small classes: Expression, which is actually an interface. FloatNode, the class that creates the nodes that hold data (numbers), AddNode (and another 3 for each other respective arithmetic operator) that creates a node to hold the + operator and compute the addition (I do not know how to do the compute method) and finally, there is an abstract OperatorNode class that AddNode, DivNode, MultNode, and SubNode all extend.

Further description can be found at my old post on this here

Below is the OperatorNode.java, the abstract class that MultNode, SubNode, AddNode, and DivNode extend (the classes that perform the arithmetic).

I do not know how to do toString, or if my commute/commuteMutating work. Helping me here would be much appreciated.

Other classes that are given below OperatorNode for clarification.

The only other class I need help with is AddNode, where I do not know how to do the compute method.

OperatorNode
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

	String operatorString;
	private static int size = 0;

	Expression getLeft() { return left; }
	Expression getRight() { return right; }

	public OperatorNode(Expression l, Expression r, String s) { 
		l = left;
		r = right;
		s = operatorString;
		size++;
	}
	
	/**
	 * 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() {
		return size;
	}

	/**
	 * @ returns the height of the tree where this operator node is the root
	 */
	public int height() {
		if (left == null && right == null) { // Node is a leaf node
			return 0;
		}
		else { // Find the max height in either the left or right subtree
			int max = left.height();
			if (max < right.height()) {
				max = right.height();
			}
			return max + 1;
		}
	}
	/** 
	 * 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() {
		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() {
		OperatorNode replacement = this.createNew(left, right);
		Expression ptr = right;
		right = left;
		left = ptr;
		return replacement;
	}

	/** 
	 * As commute(), but modify the node rather than creating a new one.
		 */
	public Expression commuteMutating() {
		Expression ptr = right;
		right = left;
		left = ptr;
		return this;
	}

	   
}


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 -- Nodes where just data (numbers) are stored.
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
	}
}

AddNode -- SubNode, MultNode, and DivNode are exactly like this. They perform the +, -, /, or x .

Here I do not know how to do the compute method.
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 very much for your help!

Reply With Quote
Reply

Viewing: ASP Free ForumsOtherProgramming Help > Help with a few methods in a java expression tree


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT