| | |
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
ValueNode
OperatorNode
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?
ArithmeticNode
Java Syntax (Toggle Plain Text)
abstract class ArithmeticNode { protected Object data; protected String infixNotation; abstract Object printNode(); abstract String infix(); }
ValueNode
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
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?
Check this link on how to determine what kind of class is an instance:
http://en.wikibooks.org/wiki/Java_Pr...rds/instanceof
http://en.wikibooks.org/wiki/Java_Pr...rds/instanceof
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- Threads - need little help (Java)
- Send data on a serial port (C++)
- SVCHOST.exe Issues (Windows NT / 2000 / XP)
- Windows 2000 pro - svchost.exe using 100% cpu (Windows NT / 2000 / XP)
- Single instance (Visual Basic 4 / 5 / 6)
- Who has the best web hosting? (Web Hosting Deals)
- Quick Polymorphism Tutorial (C)
- computer freeze up when opening a folder (Windows NT / 2000 / XP)
- I don't know how to start to determine what hand of the poker, any ideas??? (Java)
Other Threads in the Java Forum
- Previous Thread: determine the type of hand 2
- Next Thread: Interpreting JAR's launched with parameters
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide image int j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux mac main map method mobile myregfun netbeans nonstatic notdisplaying number online pearl printf problem program project qt researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor






