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

    Default Friday night java puzzle

    Java Code:
    public class aBSTtree<T extends Comparable<T>>{
    				protected aaBSTNode<T> root=null; 
    		
    				public aBSTtree( ){			
    				}
    		
    				protected void visit (aaBSTNode<T> p) {
    				        System.out.print("p.el is: " + p.el + " "); 					
    				}	
    		
    				public void TreeInit( ) {
    					aaBSTNode<T> b= new aaBSTNode (10); 
    				}
    		
    				public void run( ) {
    					TreeInit(); 					
    					visit(b); 
    				}
    		
    //				public aaBSTNode<T> search(T el) {
    //					aaBSTNode<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 static void main(String[] args) {
    		(new aBSTtree( )).run(); 
    	}
    }
    Java Code:
    public class aaBSTNode <T extends Comparable<T>> {
    				protected T el; 
    				protected aaBSTNode<T> left, right; 
    			
    				public aaBSTNode( ){
    				left=right=null;
    				}
    			
    				public aaBSTNode(T el) {
    				this (el, null, null); 
    				}
    			
    				public aaBSTNode(T el, aaBSTNode<T> lt, aaBSTNode<T> rt) {
    				this.el =el; left =lt; right =rt; 
    				}
    			
    }
    ******************************
    I thought visit(b) would print out the value 10, but it doesn't. what is my problem here?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    I get a compile error. Variable b is not defined at line 18???
    but it doesn't
    What does it print?

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

    Default

    Multiple markers at this line
    - Type safety: The expression of type aaBSTNode needs unchecked conversion to conform to aaBSTNode<Integer>
    - Type safety: The constructor aaBSTNode(Comparable) belongs to the raw type aaBSTNode. References to generic type aaBSTNode<T> should be
    parameterized
    - The local variable b is never read
    - Type safety: The expression of type aaBSTNode needs unchecked conversion to conform to aaBSTNode<T>

    Those are the warning messages I got. but not really "errors".

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by niu_niu View Post
    Those are the warning messages I got. but not really "errors".
    Well, where in your code is there a "b" variable that has scope such to make it visible from within the run method?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    Are you able to execute your program after you compile it?
    Add some more println() statements to the program to show that it is executing.

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

    Default

    Well, where in your code is there a "b" variable that has scope such to make it visible from within the run method?
    It worked. You are really a hero!

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

    Default

    public class aBSTtree<T extends Comparable<T>>{
    protected aaBSTNode<T> root=null;

    public aBSTtree( ){
    }

    protected void visit (aaBSTNode<T> p) {
    System.out.println("in visit, p.el is: " + p.el + " ");
    System.out.println("I am here");
    System.out.println("in visit, p.lt is: " + p.left);
    System.out.println("I am there");
    System.out.println("in visit, p.rt is: " + p.right);
    }

    public aaBSTNode<T> search(T el) {
    aaBSTNode<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 run( ) {
    aaBSTNode<T> b= new aaBSTNode (10);
    aaBSTNode<T> c=new aaBSTNode(25);
    aaBSTNode<T> root= new aaBSTNode(13, b, c);

    visit(root);
    search(13);
    search(b);
    }

    public static void main(String[] args) {
    (new aBSTtree( )).run();
    }
    }

    public class aaBSTNode <T extends Comparable<T>> {
    protected T el;
    protected aaBSTNode<T> left, right;

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

    public aaBSTNode(T el) {
    this (el, null, null);
    }

    public aaBSTNode(T el, aaBSTNode<T> lt, aaBSTNode<T> rt) {
    this.el =el; left =lt; right =rt;
    }

    }
    ------------------------------
    Ok, now I am getting a different problem here. I am trying to make the search work, but it keeps on complaining on the type mismatch. I tried both search(13) and search(root), yet it liked neither. what is wrong here?

    Thanks!
    nn

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

    Default

    Java Code:
    public class aBSTtree<T extends Comparable<T>>{
    				protected aaBSTNode<T> root=null; 
    		
    				public aBSTtree( ){			
    				}
    		
    				protected void visit (aaBSTNode<T> p) {
    					System.out.println("in visit, p.el is: " + p.el + " "); 
    					System.out.println("I am here"); 
    					System.out.println("in visit, p.lt is: " + p.left); 
    					System.out.println("I am there"); 
    					System.out.println("in visit, p.rt is: " + p.right); 
    				}	
    		
    				public aaBSTNode<T> search(T el) {
    					aaBSTNode<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 run( ) {
    					aaBSTNode<T> b= new aaBSTNode (10); 
    					aaBSTNode<T> c=new aaBSTNode(25);	
    					aaBSTNode<T> root= new aaBSTNode(13, b, c);
    					
    					visit(root); 
    					search(13); 
    					search(b); 
    				}
    				
    public static void main(String[] args) {
    		(new aBSTtree( )).run(); 
    	}
    }
    Java Code:
    public class aaBSTNode <T extends Comparable<T>> {
    				protected T el; 
    				protected aaBSTNode<T> left, right; 
    			
    				public aaBSTNode( ){
    				left=right=null;
    				}
    			
    				public aaBSTNode(T el) {
    				this (el, null, null); 
    				}
    			
    				public aaBSTNode(T el, aaBSTNode<T> lt, aaBSTNode<T> rt) {
    				this.el =el; left =lt; right =rt; 
    				}
    			
    }
    -----------------------------------------
    Repasting the code in...did quote instead of code in the above comment. sorry about the mess.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    it keeps on complaining
    When you get errors, please copy and paste them here.

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

    Default

    sorry, here is the error message:

    The method search(T) in the type aBSTtree<T> is not applicable for the arguments (int)

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

    Default

    What compiler are you using? When I compile it, my error messages look like this:
    Running: D:\Java\jdk1.6.0_02\bin\javac.exe -Xlint -g -deprecation -classpath D:\JavaDevelopment\;. aBSTtree2.java

    aBSTtree2.java:28: warning: [unchecked] unchecked call to aaBSTNode(T) as a member of the raw type aaBSTNode
    aaBSTNode<T> b= new aaBSTNode (10);
    ^
    aBSTtree2.java:28: warning: [unchecked] unchecked conversion
    found : aaBSTNode
    required: aaBSTNode<T>
    aaBSTNode<T> b= new aaBSTNode (10);
    ^
    aBSTtree2.java:29: warning: [unchecked] unchecked call to aaBSTNode(T) as a member of the raw type aaBSTNode
    aaBSTNode<T> c=new aaBSTNode(25);
    ^
    aBSTtree2.java:29: warning: [unchecked] unchecked conversion
    found : aaBSTNode
    required: aaBSTNode<T>
    aaBSTNode<T> c=new aaBSTNode(25);
    ^
    aBSTtree2.java:30: warning: [unchecked] unchecked call to aaBSTNode(T,aaBSTNode<T>,aaBSTNode<T>) as a member of the raw type aaBSTNode
    aaBSTNode<T> root= new aaBSTNode(13, b, c);
    ^
    aBSTtree2.java:30: warning: [unchecked] unchecked conversion
    found : aaBSTNode
    required: aaBSTNode<T>
    aaBSTNode<T> root= new aaBSTNode(13, b, c);
    ^
    aBSTtree2.java:34: search(T) in aBSTtree2<T> cannot be applied to (int)
    search(13);
    ^
    aBSTtree2.java:35: search(T) in aBSTtree2<T> cannot be applied to (aaBSTNode<T>)
    search(b);
    ^
    3 errors
    6 warnings

    8 error(s)
    Why do you get such a short message?

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

    Default

    Well, this doesn't help me much. I want to know the right syntax and why I failed. Thx!

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    Your posting the full text of an error message would help us work on the problem. Your short, edited messages leave out important information.

    Why is the run() method in the class? Shouldn't the testing calls be done from outside the class with a reference to an instance of the class?
    Last edited by Norm; 06-19-2010 at 11:56 PM.

Similar Threads

  1. 15 puzzle using 2d arrays
    By GPB in forum New To Java
    Replies: 1
    Last Post: 05-26-2010, 01:35 AM
  2. N-Puzzle Help!
    By evan42781 in forum New To Java
    Replies: 12
    Last Post: 04-30-2009, 12:34 AM
  3. Need help with Trees...(8-puzzle)
    By ventrue in forum New To Java
    Replies: 2
    Last Post: 03-24-2009, 12:04 AM
  4. 8-Square puzzle loop
    By SapphireSpark in forum New To Java
    Replies: 7
    Last Post: 12-04-2008, 08:21 PM
  5. Java Drawing PUZZLE
    By Cyorxamp in forum AWT / Swing
    Replies: 3
    Last Post: 06-09-2008, 11:35 AM

Posting Permissions

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