Results 1 to 12 of 12
Thread: How to run the run()?
- 06-17-2010, 03:56 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 48
- Rep Power
- 0
How to run the run()?
Hi:Java Code:import java.io.*; public class aBSTtree { public class BSTNode<T extends Comparable<T>> { protected T el; protected BSTNode<T> left, right; public BSTNode( ){ left=right=null; } public BSTNode(T el) { this (el, null, null); } public BSTNode(T el, BSTNode<T> lt, BSTNode<T> rt) { this.el =el; left =lt; right =rt; } } public class BST<T extends Comparable<T>>{ protected BSTNode<T> root=null; public BST( ){ } protected void visit (BSTNode<T> p) { System.out.print(p.el + " "); } public void TreeInit( ) { BSTNode b=new BSTNode(10, null,null); BSTNode c=new BSTNode(25, null, null); BSTNode root= new BSTNode(13, b, c); } public void run( ) { TreeInit(); System.out.println("b is: " + root); } } public static void main(String[] args) { (new BST<Integer> ()).run(); } }
I am trying to run the run() method, but my program is giving me syntax error messages on "(new BST<Integer> ()).run();"...I tried a couple of some other things, but none worked. What should I put here?
Niu Niu
- 06-17-2010, 03:59 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
What do we need to do so that you can tell us the errors that you got?
- 06-17-2010, 04:36 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 48
- Rep Power
- 0
OK, I realized what was potentially wrong and I re-organized the structure a little. However, I am now getting a different error. It somehow doesn't like the line: "public class BSTNode<T extends Comparable<T>>". And the error is: The public type BSTNode must be defined in its own type. But isn't it obvious that I have already defined it??Java Code:import java.io.*; public class BSTNode<T extends Comparable<T>> { protected T el; protected BSTNode<T> left, right; public BSTNode( ){ left=right=null; } public BSTNode(T el) { this (el, null, null); } public BSTNode(T el, BSTNode<T> lt, BSTNode<T> rt) { this.el =el; left =lt; right =rt; } } public class aBSTtree<T extends Comparable<T>>{ protected BSTNode<T> root=null; public aBSTtree( ){ } protected void visit (BSTNode<T> p) { System.out.print(p.el + " "); } public void TreeInit( ) { BSTNode b=new BSTNode(10, null,null); BSTNode c=new BSTNode(25, null, null); BSTNode root= new BSTNode(13, b, c); } public void run( ) { TreeInit(); System.out.println("b is: " + root); } public static void main(String[] args) { (new aBSTtree( )).run(); } }
public BSTNode( ){ left=right=null;}
What am I missing here?
N.N.
- 06-17-2010, 04:38 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
You can only have one public top level class in a file.
What you wanted was to keep the original code, but move all the stuff in BST<T> up into aBSTtree and get rid of that class.
- 06-17-2010, 04:41 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 06-17-2010, 04:45 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
He's trying to instantiate BST, however it is a non-static inner class and so can only be instantiated in relation to an aBSTtree object. That's the error (cannot instantiate non-static class).
I can't believe I actually cut and pasted that code into IntelliJ...
- 06-17-2010, 05:37 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 06-17-2010, 05:42 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
The original code in the OP is a class (aBSTtree) that consists solely of a main() and two inner classes (BST and BSTNode).
- 06-17-2010, 06:27 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 06-17-2010, 07:23 PM #10
Member
- Join Date
- Jun 2010
- Posts
- 48
- Rep Power
- 0
***********************************Java Code:import java.io.*; class BSTNode<T extends Comparable<T>> { protected T el; protected BSTNode<T> left, right; public BSTNode( ){ left=right=null; } public BSTNode(T el) { this (el, null, null); } public BSTNode(T el, BSTNode<T> lt, BSTNode<T> rt) { this.el =el; left =lt; right =rt; } } public class aBSTtree<T extends Comparable<T>>{ protected BSTNode<T> root=null; public aBSTtree( ){ } protected void visit (BSTNode<T> p) { System.out.print(p.el + " "); } public BSTNode<T> search(T el) { BSTNode<T> p = root; while(p != null) if (el.equals(p.el)) return p; else if (el.compareTo(p.el)<0) p= p.left; else p = p.right; return null; } public void TreeInit( ) { BSTNode<T> b=new BSTNode(10, null,null); BSTNode<T> c=new BSTNode(25, null, null); BSTNode<T> root= new BSTNode(13, b, c); } public void run( ) { TreeInit(); search(13); } public static void main(String[] args) { (new aBSTtree( )).run(); } }
why is that Eclipse is complaining that search(13) is not valid?
- 06-17-2010, 07:36 PM #11
Was there any error message?Eclipse is complaining that search(13) is not valid
What is the type of args to the search() method? Is it an int? Or Integer if you have autoboxing.
- 06-18-2010, 08:56 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks