java newbie needs help to understand codes..help!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: java newbie needs help to understand codes..help!

  1. #1
    Join Date
    Nov 2005
    Posts
    3

    Question java newbie needs help to understand codes..help!

    Hi.. I am really new to java and needed help with these codes..:

    -------------------------------
    public class LightOut
    { private static final int NLIGHTS = 5;
    private static int line = 0;
    public static void main(String[] args)
    { remove(NLIGHTS);
    }
    private static void remove(int n)
    { if (n>0)
    { remove(n-2);
    printOut(n,"off");
    replace(n-2);
    remove(n-1);

    }
    }
    private static void replace(int n)
    { if (n>0)
    { replace(n-1);
    remove(n-2);
    printOut(n, "on ");
    replace(n-2);

    }
    }
    private static void printOut(int n, String s)
    { System.out.printf("%3d %s", n, s);
    line++;
    if (line%10 == 0)
    System.out.printf("%n");
    }
    }
    -------------------------------

    apparently, i can't seems to figure out how the codes move.. as in, after the main calls for the method remove(NLIGHTS), and then it falls into the recursion of method remove till it prints 1 off, it will call for method replace(n-2). the thing that i tried, but failed , to understand is, what will be the n that goes into the replace method that is being called from the remove method. as well as, after going into the replace method, and it recurs, and throws back to the remove method, it will start from the start of the method or continues from where it stop.. and what will the n be ?
    i am really confused about the movements as well as the new n.

    is this also using recursive backtracking?

    sorry if this if i am confusing you... am i?? please help me out if you can...
    would really appreciate your help

    -tea-

  2. #2
    Join Date
    Oct 2005
    Posts
    107
    the print statements should give you a clue.

    it will start from the start of the method or continues from where it stop.. and what will the n be ?

    it returns to the end of the method call
    n will be the value of the parameter that was originally passed into the current method

  3. #3
    Join Date
    Nov 2005
    Posts
    3
    Quote Originally Posted by Joe Beam
    the print statements should give you a clue.

    it will start from the start of the method or continues from where it stop.. and what will the n be ?

    it returns to the end of the method call
    n will be the value of the parameter that was originally passed into the current method
    Hi,
    Thanks a lot for the help!! Really appreciate it..

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2006, 04:16 PM
  2. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 07:46 AM
  3. DevX does seem one sideded
    By Rob Abbe in forum Talk to the Editors
    Replies: 44
    Last Post: 01-13-2003, 03:57 PM
  4. learning c# very confusing.
    By Mike Tsakiris in forum .NET
    Replies: 11
    Last Post: 10-04-2002, 06:32 PM
  5. Re: VB vs. Visual Age for Java
    By JJ in forum Enterprise
    Replies: 1
    Last Post: 07-06-2000, 05:50 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center