Solved

Can anyone explain line 41 and 43?

Posted on 2002-05-15
3
170 Views
Last Modified: 2012-05-04
//Java2 applet HanoiJ2app.java
import java.awt.Container;
import javax.swing.*;

public class HanoiJ2app extends JApplet
{ // initialize
long numberofdisks;
int start = 1, finale=3;
String output="";

public void init()
{ String disks;
disks=JOptionPane.showInputDialog("Enter the Number of Disks");

// convert the string to a long integer
numberofdisks=Long.parseLong(disks);

output +="For " +numberofdisks +" disks\n the steps are as follows:\n\n";

// call the recursive method
tower(numberofdisks, start, finale);

// Direct the output to the screen. Use Java2 classes and methods
// Instantiate JTextArea as outputArea w/40 rows & 27 cols
JTextArea outputArea=new JTextArea (40,27);

// attach outputArea to scroll bar element
JScrollPane scroller = new JScrollPane (outputArea);

Container c= getContentPane();
c.add(scroller);

outputArea.setText(output);
} // end init method

public void tower(long disks, int st, int end)
{
if (disks <1) ; // do nothing
else
{
tower ( disks-1, st, 6-(st+end) );   //Line 41
output += st + " --> " + end + "\n";
tower( disks-1, 6-(st+end), end );   //Line 43
}
} // end tower method definition

} // end class definition
0
Comment
Question by:brett605
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 7011858
Ah, I saw this question second. What is going on is recursion. An algorithm is being defined in terms of itself.

For example, the mathematical operation "factorial" can be defined iteratively (for n! multiply all numbers from 1 to n) or it can be defined recursively (n!=n*(n-1)! and 1!=1).

To write a recursive algorithm for n! you could write:

int factorial(int n)
{   if(n==1)return(1);
    return(factorial(n-1));
}

This can work because of the stack driven nature of method calls. When factorial is called the value for the argument n is put on the stack, and then the JVM jumps to the code for factorial. If factorial needs to call itself it pushes the new "n" onto the stack, and jumps to the start of the method, just as before. As the methods returns, the stack "unwinds", and eventually, it pops out the answer to the original caller.

The solution to the towers of Hanoi problem is also usually phrased in a recursive fashion. You have three sticks (1, 2 and 3). On stick one there is a number of rings of diminishing size. You must get them onto stick 3 without ever putting a larger on a smaller ring. So, for one ring the solution is simple: move the ring from 1 to 3. For 2 rings it is also simple: move the top ring to 2, move the bottom ring to 3, then move the top ring to 3: done.

However, the solution for 2 rings (or any number) could also be phrased: Move all but the bottom ring to stick 2. Then move the bottom ring to 3. Then move the rings on 2 to 3. And the process for the first and last operation is the same, except for one less ring.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8749745
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to imladris

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question