Results 1 to 2 of 2
  1. #1
    bluekswing is offline Member
    Join Date
    Jun 2007
    Posts
    14
    Rep Power
    0

    Default invoking method and output questions

    Quick question

    Java Code:
    public static int fun(int n, int k) {
       if (k<= 0) return 2;
       if (k>=n) return 5;
       return fun(n-1, k-1) + fun(n-1, k);
    }

    Trying to get a feel for methods and how they work, especially being able to look at methods and try to tell what they do and what types of output I would get. For the above example, suppose I invoke the method with the following parameters fun(5,2), what would my output be?


    Also, how would I invoke this method in my main()?



    Much appreciate the help as always! :)
    Last edited by levent; 08-05-2007 at 11:51 PM. Reason: bluekswing: please do not forget to place [code] tag around your code segments.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
        public static void main(String[] args) {
            int n = fun(5,2);
            System.out.println("n = " + n);
        }
    
        public static int fun(int n, int k) {
            if (k<= 0) return 2;
            if (k>=n) return 5;
            return fun(n-1, k-1) + fun(n-1, k);
        }

Similar Threads

  1. [SOLVED] Invoking a Stateful Session Bean as Stateless
    By JThangiah in forum Enterprise JavaBeans (EJB)
    Replies: 3
    Last Post: 07-17-2008, 09:35 AM
  2. questions
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 11-27-2007, 11:18 PM
  3. 3 Questions
    By hiranya in forum AWT / Swing
    Replies: 4
    Last Post: 11-14-2007, 04:57 AM
  4. Invoking superclass methods... how?
    By rhobincu in forum New To Java
    Replies: 7
    Last Post: 08-09-2007, 03:10 PM
  5. Replies: 0
    Last Post: 07-16-2007, 08:45 PM

Posting Permissions

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