Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

for java Q2: Tower of Hanoi is a mathematical puzzle where we have three rods and n disks of different sizes. The objective of the

for java
image text in transcribed
image text in transcribed
image text in transcribed
Q2: Tower of Hanoi is a mathematical puzzle where we have three rods and n disks of different sizes. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: 1) Only one disk can be moved at a time. 2) Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack. 3) No disk may be placed on top of a smaller disk. Design and implement a program that solves the Tower of Hanoi problem. You have 3 towers a, b, and c, and n disks. You need to move the disks from tower a to tower c following the rules above. Hint: you can use tower b to hold disks temporarily. You can represent the towers as stacks. (use linked list implementation) a. Describe how can you solve the problem for n = 1, n = 2, n = 3, then conclude the general case for any value of n. Sample steps for n= 3 is shown in the image. b. Write a Java method towersOfHanoi that takes 4 arguments: the number of disks n, the source tower a, the temporarily tower b, and the destination tower c. The method should move the top n disks from tower a to tower c using tower b as temporarily. c. Implement the LinkedStack and Node as discussed in the lecture. Just change the type of element to char. d. Add a Java method printReverse to the class that prints the contents of the stack in reverse (last element first). e. Write a Java program to test your method. It should take the number of disks n from the user, and then create three stacks to represent each tower, a, b, and c. Then, it should ask the user to enter n characters in alphabetical order (to represent the disks and sizes) and pushed them into the first stack. Then it should print the contents of all stacks. Then, it should call the towers OfHanoi method. Then, print the contents of the three stacks. Check the sample output. Sample output Please enter the number of disks 3 Please, enter 3 characters in alphabetical order: ABC The contents of tower a: | The contents of tower b: The contents of tower c: After moving the disks from tower a to tower c: The contents of tower a: The contents of tower b: The contents of tower c: CBA

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions