DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Determine instance (http://www.daniweb.com/forums/thread166745.html)

efus Jan 7th, 2009 6:52 am
Determine instance
 
I am trying to learn some oop in java. What I am trying to do is building a binary tree to convert an ekspresion in reverse police notation to infix notation. The problem is that I have three classes, one abstract called ArithmeticNode which represents a general node, a class called ValueNode representing a leaf with no children and the last class called OperatorNode representing a node with two children.

ArithmeticNode
abstract class ArithmeticNode 
{       
        protected Object data;
        protected String infixNotation;
       
        abstract Object printNode();
        abstract String infix();
}

ValueNode

class ValueNode extends ArithmeticNode
{
       
        public ValueNode(Number value)
        {
                        super.data = value;
        }
       
        public Object printNode()
        {
                        return super.data;
        }
       
        public String infix()
        {
                super.infixNotation += super.data;
               
                return super.infixNotation;
        }
       
}



OperatorNode
class OperatorNode extends ArithmeticNode
{

        Object leftChild;
    Object rightChild;
       
        public OperatorNode(Object operator, Object LC, Object RC)
        {
                this.leftChild = LC;
                this.rightChild = RC;
                super.data = operator;
        }
       
        public String printNode()
        {
                String node = new String();
               
                node = leftChild.toString();
                node += super.data.toString();
                node += rightChild.toString();
               
                return node;
        }
       
        public String infix()
        {
                super.infixNotation += leftChild.infix();
                super.infixNotation += super.data;
                super.infixNotation += rightChild.infix();
               
               
                return super.infixNotation;
        }
               

}



The problem I am having right now, is how do I know what the children in OperatorNode is an instance of. They can be both an OperatorNode or a ValueNode. Right now I have them as Object which does not work.
So the question is: is there a way to decide what instance each child is or do I need to restructure?

javaAddict Jan 7th, 2009 7:19 am
Re: Determine instance
 
Check this link on how to determine what kind of class is an instance:

http://en.wikibooks.org/wiki/Java_Pr...rds/instanceof


All times are GMT -4. The time now is 2:34 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC