| | |
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 |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing system test textfields threads time title tree tutorial-sample ubuntu update windows working






