Results 1 to 5 of 5
  1. #1
    2rs1 is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default command input in function?

    Hi,

    not sure about the thread name, but tried to describe my problem good. Tried searching too, but didn't really find anything useful :(

    Got a binarysearchtree task at school. Got the tree set ut and now I am writing functions for calculating height, depths etc. Got at traversal function that calls it self recursive, and it works like a charm :):

    Java Code:
    public void traversal(BinNode<E> n) {
    	if (n != null) {
    		// DO PREFIX
    		traverser(n.left);
    		// DO INFIX
    		traverser(n.right);
    		// DO POSTFIX
    	}
    }

    Is it possible to add an argument that i want to run in this function? Like this:
    Java Code:
    public void traversal(BinNode<E> n, command toexecute) {
    ...
    //DO INFIX
    execute(toexecute)
    ...
    }

    So if I call it:
    traversal(root, System.out.println(n.data));
    It will print out n.data infix (or whereever i placed execute(toexecute))?

    I solved my school task, but there i made alot of traversal functions, and it would be so much neater if it is possible to do it this way? :)

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Do u want to receive arguments from command line?
    Ramya:cool:

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

    Default

    Java isn't a scripting language, so you can't do that. I'm guessing that's where you're coming from.

    As to how you'd write something like this in Java, it depends what and why you want different things to happen in the traversal. However, the usual way would be to have an interface, say, and various implementations of the interface, depending on what it is you want them to do.

  4. #4
    2rs1 is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Java isn't a scripting language, so you can't do that. I'm guessing that's where you're coming from.

    As to how you'd write something like this in Java, it depends what and why you want different things to happen in the traversal. However, the usual way would be to have an interface, say, and various implementations of the interface, depending on what it is you want them to do.
    Ok, thanks :(

    RamyaSivakanth: No :)

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

    Default

    Quote Originally Posted by 2rs1 View Post
    Ok, thanks :(

    RamyaSivakanth: No :)
    If you gave us more to go on there might be other options, but the "single method that does different things depending on the circumstances" is usually an interface thing...not always, but in a learning environment, usually.

Similar Threads

  1. Replies: 8
    Last Post: 06-27-2009, 06:35 PM
  2. How "Pattern.matches(regex, input)" function works?
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 04-26-2009, 01:46 AM
  3. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 01:23 AM
  4. I want to add function
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:25 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 09:46 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
  •