Determine instance

Reply

Join Date: Oct 2008
Posts: 11
Reputation: efus is an unknown quantity at this point 
Solved Threads: 0
efus's Avatar
efus efus is offline Offline
Newbie Poster

Determine instance

 
0
  #1
Jan 7th, 2009
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
  1. abstract class ArithmeticNode
  2. {
  3. protected Object data;
  4. protected String infixNotation;
  5.  
  6. abstract Object printNode();
  7. abstract String infix();
  8. }

ValueNode
  1.  
  2. class ValueNode extends ArithmeticNode
  3. {
  4.  
  5. public ValueNode(Number value)
  6. {
  7. super.data = value;
  8. }
  9.  
  10. public Object printNode()
  11. {
  12. return super.data;
  13. }
  14.  
  15. public String infix()
  16. {
  17. super.infixNotation += super.data;
  18.  
  19. return super.infixNotation;
  20. }
  21.  
  22. }



OperatorNode
  1. class OperatorNode extends ArithmeticNode
  2. {
  3.  
  4. Object leftChild;
  5. Object rightChild;
  6.  
  7. public OperatorNode(Object operator, Object LC, Object RC)
  8. {
  9. this.leftChild = LC;
  10. this.rightChild = RC;
  11. super.data = operator;
  12. }
  13.  
  14. public String printNode()
  15. {
  16. String node = new String();
  17.  
  18. node = leftChild.toString();
  19. node += super.data.toString();
  20. node += rightChild.toString();
  21.  
  22. return node;
  23. }
  24.  
  25. public String infix()
  26. {
  27. super.infixNotation += leftChild.infix();
  28. super.infixNotation += super.data;
  29. super.infixNotation += rightChild.infix();
  30.  
  31.  
  32. return super.infixNotation;
  33. }
  34.  
  35.  
  36. }



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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,527
Reputation: javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light 
Solved Threads: 209
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Determine instance

 
0
  #2
Jan 7th, 2009
Check this link on how to determine what kind of class is an instance:

http://en.wikibooks.org/wiki/Java_Pr...rds/instanceof
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC