Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2, JDK 5 Edition
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Java 2, JDK 5 Edition
This is the forum to discuss the Wrox book Ivor Horton's Beginning Java 2, JDK 5 Edition by Ivor Horton; ISBN: 9780764568749
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2, JDK 5 Edition section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 7th, 2010, 10:14 PM
Registered User
 
Join Date: Jan 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default [Help]Chapter 10 Practice 4

I downloaded the code, but I am having trouble to understand how the inner function works even though the author stated it. Any one who understand this can give me suggestions?

Comment By the Author:
Quote:
Creating all permutations of the words in the phrase is the most difficult part of the exercise.
If you can crack this, then writing the file is trivial :-)
This method visualizes the permutations of the words as a graph with a dummy node at the top.
The number of levels below the dummy node corresponds to the number of words in the phrase and each path
from the dummy node down through the levels defines one permutation of the words in the phrase.
The argument to the method is the index of the word in the permutation to be assigned. When this method is called with an argument of 0,
it traverses each of the paths in the graph and thus creates all permutations. When a path has been traversed,
it is written to the file. The path variable records the 'length' of a path as it is traversed,
where the maximum length is the number of words in the phrase. When path reaches the maximum, a complete
path has been traversed so it is written to the file.
Reply With Quote
  #2 (permalink)  
Old October 25th, 2010, 01:40 PM
Registered User
 
Join Date: Oct 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Its recursion which maybe throwing you off
If I replace the for loop with a while...
Code:
private static void permute(int n) {
    permutation[n] = ++path == 0 ? null: words[path-1];
    if(path == words.length) { 
       write();
    } else {
       i=0
      while( i<words.length) {
        if(permutation[i] == null) 
           permute(i);
         // You return HERE
         ++i;
      }
    }
      path--;
      permutation[n] = null;
  }
Where I comment YOU RETURN HERE is where, by golly, you return after each recursion (see the method calls itself)
Now when it does the value of i in the loop & the original value of n (the argument) will return to what ever was pushed on the stack at that call, and so the while loop will continue, ++i is it still less?
yes is it a null yes push it all on the stack and call myself again
or no
path --
make n null
return
(and if it was a recursive call that called it it will again jump back into the while loop)

Hope that helps
Reply With Quote
  #3 (permalink)  
Old October 30th, 2010, 08:02 AM
Registered User
 
Join Date: Jan 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your help. I am really glad that there is someone as nice as you.

However...

Sorry to say that this was solved long ago. I couldn't find any help here so I figure that out myself after some hard logics. It was too long ago(as indicated by the date), because right now I am already an oracle certified J2EE developer.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 10, listing 10-10-app kiwibrit BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 2 August 18th, 2009 05:21 AM
Chapter 10 JimSchubert BOOK: Ruby on Rails for Microsoft Developers 1 May 18th, 2009 12:35 AM
Chapter 10 gogeo BOOK: Beginning Access 2003 VBA 1 January 22nd, 2006 09:41 AM
Chapter 10 czambran BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 2 March 29th, 2005 09:35 AM
Chapter 10 columbiasmiles JSP Basics 0 May 17th, 2004 09:09 PM



All times are GMT -4. The time now is 03:45 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.