-
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-
-
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
-
 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
-
Replies: 2
Last Post: 06-14-2006, 03:16 PM
-
Replies: 1
Last Post: 05-13-2005, 06:46 AM
-
By Rob Abbe in forum Talk to the Editors
Replies: 44
Last Post: 01-13-2003, 02:57 PM
-
By Mike Tsakiris in forum .NET
Replies: 11
Last Post: 10-04-2002, 05:32 PM
-
By JJ in forum Enterprise
Replies: 1
Last Post: 07-06-2000, 04: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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks