5 Replies - 3631 Views - Last Post: 24 April 2008 - 10:22 PM Rate Topic: -----

#1 AdnanShafique  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 24-April 08

Java Generics Static to non-static reference help!

Post icon  Posted 24 April 2008 - 03:03 PM

public class LinkedBinaryTree<E> implements BinaryTree<E> {
protected BTPosition<E> root; // reference to the root
protected int size; // number of nodes
/** Creates an empty binary tree. */
public LinkedBinaryTree() {
root = null; // start with an empty tree
size = 0;
}
/** Adds a root node to an empty tree */
public Position<E> addRoot(E e) throws NonEmptyTreeException {
if(!isEmpty())
throw new NonEmptyTreeException("Tree already has a root");
size = 1;
root = createNode(e,null,null,null);
return root;
}
/** Inserts a left child at a given node. */
public Position<E> insertLeft(Position<E> v, E e)
throws InvalidPositionException {
BTPosition<E> vv = checkPosition(v);
Position<E> leftPos = vv.getLeft();
if (leftPos != null)
throw new InvalidPositionException("Node already has a left child");
BTPosition<E> ww = createNode(e, vv, null, null);
vv.setLeft(ww);
size++;
return ww;
}
//end#fragment LinkedBinaryTree3
/** Inserts a right child at a given node. */
public Position<E> insertRight(Position<E> v, E e)
throws InvalidPositionException {
BTPosition<E> vv = checkPosition(v);
Position<E> rightPos = vv.getRight();
if (rightPos != null)
throw new InvalidPositionException("Node already has a right child");
BTPosition<E> w = createNode(e, vv, null, null);
vv.setRight(w);
size++;
return w;
}
}

I have to write a tester method for this a main function from where I can test this code.
As the main is a static field i cannot access the above methods from there. I need help with how to work with this.
Thank You.

Is This A Good Question/Topic? 0
  • +

Replies To: Java Generics Static to non-static reference help!

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

Re: Java Generics Static to non-static reference help!

Posted 24 April 2008 - 03:21 PM

Please post like this: :code:


public static void main(String[] arg) {

   LinkedBinaryTree tree = new LinkedBinaryTree();
   // then you can access your method  working on the non static tree object
   tree.add(.....);
}




Now, as your class is defined with <E> you will have to do

LinkedBinaryTree<WhateverObject> = new LinkedBinaryTree<WhatveverObject>();
Was This Post Helpful? 0
  • +
  • -

#3 AdnanShafique  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 24-April 08

Re: Java Generics Static to non-static reference help!

Posted 24 April 2008 - 04:06 PM

Thanks a lot for your help pbl.

Now that I've done that I'm facing another weird problem.
I wrote a tester method but the output its giving me is weird.

package net.datastructures;

public class Test<E> extends LinkedBinaryTree<Integer>{

public static void main(String[] args)
{
LinkedBinaryTree<Integer> tree = new LinkedBinaryTree<Integer>();
tree.addRoot(10);
System.out.println("Root: "+tree.root());
tree.insertLeft(tree.root, 12);
tree.insertRight(tree.root, 15);
System.out.println("Left child: "+tree.left(tree.root));
System.out.println("size: "+tree.size());
System.out.println("Right Child: "+tree.right(tree.root));
}

}


Following is the output: Why isn't it returning me the integer values? Am i doing somthing wrong?

Root: net.datastructures.BTNode@1f1fba0
Left child: net.datastructures.BTNode@1befab0
size: 3
Right Child: net.datastructures.BTNode@13c5982
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

Re: Java Generics Static to non-static reference help!

Posted 24 April 2008 - 04:23 PM

The usual one....

You seems fascinated by the <E> aren't you ? They didn't exist before JRE 1.5


public static void main(String[] args) 
	{
	  LinkedBinaryTree<Integer> tree = new LinkedBinaryTree<Integer>();
	  tree.addRoot(10);
	  System.out.println("Root: "+tree.root());
	  tree.insertLeft(tree.root, 12);
	  tree.insertRight(tree.root, 15);
	  System.out.println("Left child: "+tree.left(tree.root));
	  System.out.println("size: "+tree.size());
	  System.out.println("Right Child: "+tree.right(tree.root));
	}
}



Your class LinkedBinaryTree deals with Integer object... not int basic variable
So when you do:
tree.addRoot(10);
JDK 1.4 would have signaled a syntax error: 10 is not an object of type Integer
since JDK 1.5 the compiler does for you
- AdnanShafique wants to add an int
- the class accepts only object
- so I won't send a warning but.... what I will really do is: tree.addRoot(new Integer(10));

so this is good when you are calling your LinkedBinaryTree with an int parameter instead of an Integer object parameter

However, when you retreive an Object from the tree.. the same magic does not operate
The Java compiler do not the conversion from Integer to int for you...
So you'll have to get its Integer object
and then call the getValue() of the Integer class to get its int value

Hope it is clearer
Was This Post Helpful? 0
  • +
  • -

#5 AdnanShafique  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 24-April 08

Re: Java Generics Static to non-static reference help!

Posted 24 April 2008 - 04:48 PM

Well yes I am new to generics in fact I'm even new to Java.
I have another question.
When I'm finished adding two children to the root. How do I refer to them if I have to add nodes to them?
Like when adding children to the root I wrote tree.root but how to point to the children?

If I added the children like this:

tree.insertLeft(tree.root, new Integer(12));
tree.insertRight(tree.root, new Integer(15));

I cannot say tree.12 or tree.15 to refer them.

I really appreciate your help.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

Re: Java Generics Static to non-static reference help!

Posted 24 April 2008 - 10:22 PM

View PostAdnanShafique, on 24 Apr, 2008 - 04:48 PM, said:

When I'm finished adding two children to the root. How do I refer to them if I have to add nodes to them?


Children have to be node.
You don't have to add "node" to them AFTER
You have to make them node BEFORE you add them

So your LinkedTree class must have

Node leftNode;
Node rightNode;

(For that you must have a class Node defined somehere)

So when your LinkedTree method addNode() receives an int to add you must first
- create a new node out of that int

class Node {
   Integer x;
   Node left = null;
   Node rigth = null;
   // constructor
  Node(int value) {
	 x = new Integer(value);
  }
}



then your LinkedTree class should assign the good value to left and right
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1