Click to See Complete Forum and Search --> : Simple Problem..


Code_Nerd
October 17th, 2005, 08:09 AM
Hello..
I am quite new to Java and havent picked up on the littlew nuances it has.. I am getting an error "Cannot find Symbol" class BTNode..
Do I need to import it or something, I do not understand why it cannot be found? Below are the classes I am trying to use..

BTNode Class
public class BTNode{
protected Object data;
protected BTNode left,right;

public BTNode(){
data = null;
left = right = null;
}
public BTNode(Object d){
data = d;
left = right = null;
}
public void setLeft(BTNode sleft){
left = sleft;
}
public void setRight(BTNode sright){
right = sright;
}
public void setData(Object sdata){
data = sdata;
}
public BTNode getLeft(){
return left;
}
public BTNode getRight(){
return right;
}
public Object getData(){
return data;
}
}

Binary Tree abstract class
public abstract class BinaryTree{
private BTNode root;

protected BTNode getRoot(){
return root;
}
protected void setRoot(BTNode r){
root = r;
}
public BinaryTree(){
setRoot(null);
}
public BinaryTree(Object o){
setRoot(new BTNode(o));
}
public boolean isEmpty(){
return getRoot() == null;
}
public Object getData(){
if(!isEmpty())
return getRoot().getData();
return null;
}
public BTNode getLeft(){
if(!isEmpty())
return getRoot().getLeft();
return null;
}
public BTNode getRight(){
if(!isEmpty())
return getRoot().getRight();
return null;
}
public void setData(Object o){
if(!isEmpty())
getRoot().setData(o);
}
public void insertLeft(BTNode p,Object o){
if((p != null) && (p.getLeft() == null))
p.setLeft(new BTNode(o));
}
public void insertRight(BTNode p,Object o){
if((p != null) && (p.getRight() == null))
p.setRight(new BTNode(o));
}
public void print(int mode){
if(mode == 1) printPreOrder();
else if(mode == 2) printInOrder();
else if(mode == 3) printPostOrder();
}
public void printPreOrder(){
printPreOrder(getRoot());
}
protected void printPreOrder(BTNode p){
if(p == null)
return;
System.out.print(p.getData()+" ");
printPreOrder(p.getLeft());
printPreOrder(p.getRight());
}
public void printInOrder(){
printInOrder(getRoot());
}
protected void printInOrder(BTNode p){
if(p == null)
return;
printInOrder(p.getLeft());
System.out.print(p.getData()+" ");
printInOrder(p.getRight());
}
public void printPostOrder(){
printPostOrder(getRoot());
}
protected void printPostOrder(BTNode p){
if(p == null)
return;
printPostOrder(p.getLeft());
printPostOrder(p.getRight());
System.out.print(p.getData()+" ");
}
}

Any help would be appreciated..
Thankyou :)

Bnt
October 17th, 2005, 09:09 AM
Hi,

Have you posted all your code here? I have tried to compile your code and I get different errors. I can't find methods pretrav(), postrav() and intrav(). As well as this there was a spelling error here

printInOrcder(p.getLeft());
should be
printInOrder(p.getLeft());


On which line do you get your error?

You could be correct in thinking you should import something, but this is only if your classes are in different packages (which they aren't as they aren't packaged.). Also check your file names, are they the same as your class names ie: class BTNode should be in file BTNode.java.

Are these 2 classes in the same/differnet directories?

// please ignore previous comments, I hadn't read the post correctly
Also what symbol is it that can't be found? I assumed it was BTNode but after looking again saw that you hadn't specified. I am getting cannot find symbol for methods pretrav(), postrav() and intrav(). If this is what you are getting it is because the compiler can find no reference to these methods, you would need to add them to your BinaryTree class. ie:


public abstract class BinaryTree{
...All your code...
public void postrav(){
}

public void intrav(){
}

public void pretrav(){
}
}


Byron

Code_Nerd
October 17th, 2005, 09:36 AM
Yes I am sorry about the posttrav(), pretrav() etc.. I changed the names of these methods to make them easier for me and others to comprehend and missed those!

The two classes are located in the same directory and package.. :)

*Edit* Original code has been edited to fix the above mentioned errors..

These are the errors I am getting:
E:\Tommy Uni\Yoda DS and Algo\Java Progs\BT2\src\bt2\BinaryTree.java:18: cannot find symbol
symbol : class BTNode
location: class BinaryTree
private BTNode root;
E:\Tommy Uni\Yoda DS and Algo\Java Progs\BT2\src\bt2\BinaryTree.java:20: cannot find symbol
symbol : class BTNode
location: class BinaryTree
protected BTNode getRoot(){
E:\Tommy Uni\Yoda DS and Algo\Java Progs\BT2\src\bt2\BinaryTree.java:23: cannot find symbol
symbol : class BTNode
location: class BinaryTree
protected void setRoot(BTNode r){
E:\Tommy Uni\Yoda DS and Algo\Java Progs\BT2\src\bt2\BinaryTree.java:40: cannot find symbol
symbol : class BTNode
location: class BinaryTree
public BTNode getLeft(){
E:\Tommy Uni\Yoda DS and Algo\Java Progs\BT2\src\bt2\BinaryTree.java:45: cannot find symbol
symbol : class BTNode
location: class BinaryTree
public BTNode getRight(){
E:\Tommy Uni\Yoda DS and Algo\Java Progs\BT2\src\bt2\BinaryTree.java:54: cannot find symbol
symbol : class BTNode
location: class BinaryTree
public void insertLeft(BTNode p,Object o){
E:\Tommy Uni\Yoda DS and Algo\Java Progs\BT2\src\bt2\BinaryTree.java:58: cannot find symbol
symbol : class BTNode
location: class BinaryTree
public void insertRight(BTNode p,Object o){
E:\Tommy Uni\Yoda DS and Algo\Java Progs\BT2\src\bt2\BinaryTree.java:70: cannot find symbol
symbol : class BTNode
location: class BinaryTree
protected void printPreOrder(BTNode p){
E:\Tommy Uni\Yoda DS and Algo\Java Progs\BT2\src\bt2\BinaryTree.java:80: cannot find symbol
symbol : class BTNode
location: class BinaryTree
protected void printInOrder(BTNode p){
E:\Tommy Uni\Yoda DS and Algo\Java Progs\BT2\src\bt2\BinaryTree.java:90: cannot find symbol
symbol : class BTNode
location: class BinaryTree
protected void printPostOrder(BTNode p){
E:\Tommy Uni\Yoda DS and Algo\Java Progs\BT2\src\bt2\BinaryTree.java:30: cannot find symbol
symbol : class BTNode
location: class BinaryTree
setRoot(new BTNode(o));
E:\Tommy Uni\Yoda DS and Algo\Java Progs\BT2\src\bt2\BinaryTree.java:56: cannot find symbol
symbol : class BTNode
location: class BinaryTree
p.setLeft(new BTNode(o));
E:\Tommy Uni\Yoda DS and Algo\Java Progs\BT2\src\bt2\BinaryTree.java:60: cannot find symbol
symbol : class BTNode
location: class BinaryTree
p.setRight(new BTNode(o));

Bnt
October 17th, 2005, 09:42 AM
Have you set up your classpath to include these two classes?

When you compile BinaryTree have you already compiled BTNode?

Code_Nerd
October 17th, 2005, 09:49 AM
My God..
That is all it was!

I am in a funny situation where the first 2 years of my course have all been C++, then there is this course "Data Structures and Algorithms" in Java, which is the language my course did before I started, and for some reason they havent changed over..

Anyway, I have been thrown straight into Java and havent really picked up on all these small things..

Thankyou for your help

Tommy

Bnt
October 17th, 2005, 09:53 AM
That is all it was!



What was it?

Byron

Code_Nerd
October 17th, 2005, 10:17 AM
I hadnt compiled BTNode before Binary Tree..
Maybe you could tell me why I get errors with my Main class now??

package bt2;

/**
*
* @author Tommy
*/
public class Lab21 {

/** Creates a new instance of Lab21 */
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int UpperLimit = 50000;
int LowerLimit = 1;
int Num_Nodes = 500;
//Random generator = new Random();
int TR_create = (int) (Math.random() * (UpperLimit - LowerLimit + 1) ) + LowerLimit;
Integer cast = new Integer(TR_create); // Needed to cast initial value to BTNode object
BTNode Tree_Root = new BTNode(cast); // Creating root object
BTNode result = new BTNode(); // Creating new node object to use for tree
result.insertNode(Tree_Root,TR_create); // Inserting first Node

if (UpperLimit <=0)
{
throw new IllegalArgumentException("UpperLimit must be positive: " + UpperLimit);
}
if (Num_Nodes <=0)
{
throw new IllegalArgumentException("Size of returned List must be greater than 0.");
}
long range = (long)UpperLimit - (long)LowerLimit + 1;
for( int x = 0; x < Num_Nodes; ++x)
{


int value = (int) (Math.random() * (UpperLimit - LowerLimit + 1) ) + LowerLimit;
System.out.println("Random number : " + value);
result.insertNode(Tree_Root,value);

//Integer value = new Integer (generator.nextInt(UpperLimit));
//result.insertNode(Tree_Root,value);
//System.out.println("Value" + x + value);
}
//result.inorderPrint();
System.out.println("Root " + TR_create);
System.out.println("The height of this tree is " + BTNode.treeHeight(Tree_Root));
System.out.println("The smallest number in the tree is " + result.getLeftmostData());
System.out.println("The Largest number in the tree is " + result.getRightmostData());
}



}

The error I get is:
E:\Tommy Uni\Yoda DS and Algo\Java Progs\BT2\src\bt2\Lab21.java:30: cannot access bt2.BTNode
bad class file: E:\Tommy Uni\Yoda DS and Algo\Java Progs\BT2\src\bt2\BTNode.java
file does not contain class bt2.BTNode
Please remove or make sure it appears in the correct subdirectory of the classpath.
BTNode Tree_Root = new BTNode(cast); // Creating root object

Thankyou :)

BTW I have added the other methods that you see in the main into my BTNode class.. :) ie: getRightMostdata() etc etc..

Bnt
October 17th, 2005, 10:23 AM
I see you have packaged your main class in a package called bt2. If this file is in the same folder as the other 2 files, then the other 2 also need to be in package bt2 (The code you posted earlier didn't have a package specified).

Byron

Code_Nerd
October 17th, 2005, 10:30 AM
I am not to familiar with the Netbeans IDE, however I do believe that all the files are part of the same package!
That is they are attached to the same bt2 tree in the "Projects" list on the left..

BTNode and BinaryTree class do not have "package bt2;" at the start of the code however. Is this needed?

randomjunk
October 17th, 2005, 10:43 AM
yes.

that last error means it found a compiled class file in the right place, but it didn't contain what it should have done. Add the package line and recompile and it should work.

Code_Nerd
October 18th, 2005, 04:34 AM
Thankyou for your help I finally have it sorted.. :)