Results 1 to 12 of 12
  1. #1
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default How to run the run()?

    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();  
    	}
    
    }
    Hi:

    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

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What do we need to do so that you can tell us the errors that you got?

  3. #3
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default

    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(); 
    	}
    }
    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??

    public BSTNode( ){ left=right=null;}

    What am I missing here?

    N.N.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default

    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.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,727
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by niu_niu View Post
    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(); 
    	}
    }
    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??

    public BSTNode( ){ left=right=null;}

    What am I missing here?

    N.N.
    Change everything back to your original version again (and name your file "BSTNode" and show us the compiler messages verbatim.

    kind regards,

    Jo

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default

    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...

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,727
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    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...
    If I read the code correctly there is no inner class, just diabolical indentation; there are two public classes (in one file?) and of course the entire code is a genuine mess.

    kind regards,

    Jos

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default

    The original code in the OP is a class (aBSTtree) that consists solely of a main() and two inner classes (BST and BSTNode).

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,727
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    The original code in the OP is a class (aBSTtree) that consists solely of a main() and two inner classes (BST and BSTNode).
    I see it now; I fell in the diabolical indentation trap, darn; why do people do that? Don't they know they can be defenestrated for that?

    kind regards,

    Jos

  10. #10
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default

    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?

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,766
    Rep Power
    25

    Default

    Eclipse is complaining that search(13) is not valid
    Was there any error message?
    What is the type of args to the search() method? Is it an int? Or Integer if you have autoboxing.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default

    public BSTNode<T> search(T el) {}

    That's not an int is it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •