Help me   a TowersOfHanoi problem

// Lab 3: TowersOfHanoi.java
// Program solves the Towers of Hanoi problem
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TowersOfHanoi extends JApplet implements ActionListener {
JLabel label;
JTextField input;
JTextArea outputArea;
String output;
public void init()
output = "";
// create components
label = new JLabel( "Enter number of disks ( 1-9 ): " );
input = new JTextField( 5 );
input.addActionListener( this );
outputArea = new JTextArea( 15, 20 );
/* Write code that creates a JScrollPane and attach outputArea to it */
// outputArea.setText( output );
JScrollPane scroll = new JScrollPane(outputArea);
// add components to applet
Container container = getContentPane();
container.setLayout( new FlowLayout() );
/* Write code to add the components to the content pane */
input.addActionListener(this);
container.add(label);
container.add(input);
container.add(scroll);
// recusively move disks through towers
/* write header for method tower */
String tower(int n,String peg1,String peg2,String peg3)
/* Write code here that tests for the base case (i.e., one disk).
In this case, move the last disk from peg 1 to peg 3 and return. */
if(n==1)
return output+="\n"+peg1+"--->"+peg3;
// move ( disks - 1 ) disks from peg1 to peg2 recursively
/* Write a recursive call to method tower that moves
( disks - 1 ) disks from peg1 to peg2 */
else
tower(n-1,peg1,peg3,peg2);
// move last disk from peg1 to peg3 recursively
output += "\n" + peg1 + " --> " + peg3;
// move ( disks - 1 ) disks from peg2 to peg3 recursively
/* Write a recursive call to method tower that moves
( disks - 1 ) disks from peg2 to peg3 */
tower(n-1,peg2,peg1,peg3);
return output;
// actually sort the number of discs specified by user
public void actionPerformed( ActionEvent e )
output = "";
/* call method tower and pass it the number input by the user,
a starting peg of 1, an ending peg of 3 and a temporary peg of 2 */
int n=Integer.parseInt(input.getText());
String peg1="1";
String peg2="2";
String peg3="3";
output+=tower(n,peg1,peg2,peg3);
outputArea.setText( output );
} // end class TowersOfHanoi
when n=3 , its answer : 1-->3;1-->2;3-->2;1-->3;2-->1;2->3;1-->3
I really don't understand it , Who can explain the operation course of this procedure ? thank you so much!!!

when n=3, the result is 1-->3 , 1-->2, 3-->2,1-->3,
2-->1, 2-->3, 1-->3. I don't
understand 1-->2, 3-->2.It means move the top disk of peg 1 to be on top of
all the disks on peg 2, then move the top disk on peg
3 to be on top of all disks on peg 3.Two, sir.
Signed,
Brother Maynard.

Similar Messages

Maybe you are looking for

  • FCC conversion for receiver

    Hi All, i am doing FCC conversion in FTP receiver adapter. i need to produce text file converting below structure in given format with field seperating by comma. Messages .......Message1 ............MT_Employee ..................NODE1 ...............

  • Not all Prefixes in VRF Tables are Reachable?

    Hello, During my studies with an MPLS VPN [MP-BGP] lab I found something unexpected.  I wonder if I am mistaken in my comprehension or if this normal: Not all prefixes in a VRF Table are pingable/reachable ? Seems that in the standard routing tables,

  • A need a function module to read a file and write back into it

    Hello , My requirement is to provide the path to a file , read the file contents and then write back to it. Thanks, Mridu.

  • Track mix-ups

    When Thanks I import aiff files, some work and others remain silent when in theArrange Window, while the same files will play in the sample editor. Also, some aiff files seem to play another track instead of the track that is recorded on it. I'm espe

  • What is bank key ?

    I am trying to set up a new house bank for Sweden.What is bank key or bank number ? Is it any number I can put ? or  specific number I have to ask from Bank ? It is asking a number of 04 length. Thanks in advance Satya