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
-
hello,
i restored and updated my iphone 4 to the latest version of 5.1.1 and after that when i connect my mobile to i tunes all i see is a big rectangle and a apple logo on left and a small lock on right side please help me fix this problem.I sloved this issue by resting my phone from settings>general>reset>reset all settings...the problem will be fixed
-
Please help me with Bios problem
I have laptop toshiba Satellite L735-S9311D.
I used windows service station and update bios to the latest version during installion I forgot to unplug my external HDD.
Error message appeared , Now no display on the screen.
So, Please I need help to solve my problem.Please..............
-
Hi
I've a big problem with adobe acrobat reader XI pro and I hope you can help me.
The problem is; when I past copied text from some pdf books (not all of them) it past symbols only! wherever I past it! and even if I coped that text from another pdf reader (adobe pdf reader, internet browsers, ...etc.).
This problem started to happen since yesterday when I installed adobe acrobat reader XI pro to try it before I buy it, and before that when I was using the free adobe pdf reader I was totally able to copy any text from any pdf and past it anywhere with nothing wrong.
What can I do?
thank you a lot.There is no product called Adobe Acrobat Reader Pro. There is
- Adobe Acrobat Pro ($$)
- Adobe Reader (free)
Which do you have? And are you a programmer? -
Can anyone help me with a problem i am having with my music on my iPhone 4S. I have put alot of Compilation CDs in my library on iTunes. I download these tracks onto my phone, everything is ok so far. Now, this is what is niggling me and I don.t know how to resolve it. This is my problem:
Have downloaded for example: Queen – Bohemium Rhapsody from a compilation album as well as a few complete Queen Album CDs into the iTunes library and then put them into playlists,
When I go onto my phone and select Queen on the MUSIC app using Songs tab at the bottom of the screen it will display all Queen songs and their resective Alum pics, that is all those not in a complilation album, .
If I know the song title I can select the songs tab and find the song that way,
I’ve tried fiddling with the settings in the iTunes app by going to ‘get info’ tab and trying to sort the problem out that way but am not having much luck.What I want the phone to do is show, for example all Queens songs including those in compilation albums. Can this be done, would be grateful for any ideas on how it can be done, that is if ic can be done, any ideas
Thanks for your helpTry assigning Queen as the Album Artist on the compilations in iTunes on your computer.
-
Tabs only open in a new window. Ctl T opens a new window NOT a new tab help-last update this problem started 2 days ago. How do I set FF back to open tabs in 1 window?
There is no plus sign and going into optins and changing settings has not worked. I have uninstall FF and reinstall-keeping my personalization-and that was no help.
Right clicking on FF Icon to open a new tab, or going up to FF drop down tab and clicking on new tab both do not work.
Help everything opens a new window not a new tab. there seems to be no way to reset this or to chose a different optin. I just downloaded new tabs at end addon and it didn't do a thing.as a first troubleshooting step - does it work like normally when you open & run firefox in [[Safe Mode|safemode]] by pressing the shift key while you open firefox (all other instances have to be closed before)...
-
Dear sirs,
Could you please help me on my problem with my apple ID
I create new account with security questions, and when I try to purchase paid application,it ask me for answers the questions but it's not working,
My account have 50$ and still didn't buy anythingYou need to ask Apple to reset your security questions; ways of contacting them include clicking here and picking a method for your country, phoning AppleCare and asking for the Account Security team, and filling out and submitting this form.
(100546) -
I have an iPhone 4s and it is now deactivated. the phone is asking for my apple account and password. but i have forgotten my password. i tried to reset it on iforgot.apple.com, and when i entered my apple id the next thing that appeared on screen is that i have to chose either email verification or security questions. then i click the security question then it asked about my birthday. there where only asking the month and day. then i entered my birthday which is March 8.
so can somebody help me with this problem?
my apple id account is "[email protected]"
thanks in advance!The email you should NOT have published, is your iCloud, not Apple ID
You Apple ID is a valid email address
This is a user forum, not Apple customer service.
I gather the security question did not work.
If you have an email address, why not use that?
If this fails you have to contact support and schedule help. -
My iphone 4 was on ios 5.0.1, then a few days ago I upgraded it to ios 6.1.4, then the apple logo won't stop flashing. Can anyone help to resolve my problem? Thanks in advance
I'm trying to restore my iphone by using the recovery mode. But it always seems to run out of time, any idea why is that? Thank you again
-
I have a problem with my iphone 4. My 3G always stays activated but I lose my network signal. The bars all get lost and I'm not able to receive / send sms, mms or phone calls. Could you guys please help me fix this problem? Ios 5.1.1
I haven't gotten a new sim card because the problem has been presenting itself in various cards not only mine. So far, all I've done is reset my network settings.
Last night, I turned off the 3G tab and it had all the signal bars. Today, I did the network reset and it's working apparently. But like I said before, previously the bars just disappear and the iphone only has the 3G activated. -
Hi can you help I was having problems with my iPod so I restored it but I
accidentally set it up as a new iPod is there any way to restore it from an old
backup when I right click iPod in iTunes there isn't one anyway to recover one
or find it on computerThe following says how to restore from backup.
iOS: How to back up
If you go to iTunes>Preferences>Devices you can see if you have an iTunes backup. You need one dated before or the exact time you started the restore. -
Hi,
A few songs from my old purchase is not available for download. It shows as purchased but I can't download it. Please help me resolve this problem, what do I need to do to enable download for all my purchased songs/movies etc.
- AvinashThe purchases are probably hidden:
http://support.apple.com/kb/ht4919 -
hi everyone, i have a problem about my iphone 4S, doesn't work with wifi connection and bluetooth since upgrade to the IOS 7.0.3. Can anyone can help me tosolve this problem?????Thank's regards paulus
Try the suggestions here to see if they resolve your problem:
http://support.apple.com/kb/ts1559
If these don't work you may have a hardware problem. Visit an Apple store for an evaluation or contact Apple Support. -
I cannot install Itunes and Icloud on my Sony Vaio Laptop - Window 7. Error says " computer/system not modified". Please help me with this problem.
Hello there, jag123059.
The following Knowledge Base article offers up some great steps to follow for resolving issues with installing iTunes:
Issues installing iTunes or QuickTime for Windows
http://support.apple.com/kb/ht1926
Thanks for reaching out to Apple Support Communities.
Cheers,
Pedro. -
I need help in resolving a problem that prevents me from accessing the iTunes store. Message reads " iTunes cannot contact the iTunes store" and also says that my laptop is no longer authorized to access my account. Help!
Go up to the top of your screen on iTunes and click on 'Store'. Then go down to 'Authorize This Computer'. That should cover part of it unless you've already authorized a bunch of other computers to use your account. If that's the case, you'll have to go to one of those computers and click the button just below it to 'deauthorize your account' from that computer. If you're not able to access the store, check your internet connection to make sure you are connected. Hope this helps.. good luck!
Maybe you are looking for
-
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.
-
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
-
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