Question 1: In this question, you are provided with a class TowersofHanoi you will be solving the Towers of Hanoi problem using a stack-based iterative solution. To save your time, this lab will allow you to use the Java built-in Stack class. Here's a code snippet of creating a stack that accepts integers and some of its operations: and

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question
Question 1: In this question, you are provided with a class TowersofHanoi
and you will be solving the Towers of Hanoi problem using a stack-based iterative
solution. To save your time, this lab will allow you to use the Java built-in Stack
class. Here's a code snippet of creating a stack that accepts integers and some of its
operations:
Stack<Integer> myStack
myStack.push (5); // add 5 to the top oI the stack
myStack.peek ();
- new Stack<> ();
// return the top of the stack this
7/ is actually the top method we
/ defined in our NumebrStack ADT.
myStack.pop();
// remove and return the element at
/ the top of the stack
Step 1-Crcate a helper mcthod named move which takes two parameters of type
Stack called source and destination.
Your method should check the following conditions.
•if the destination stack is empty, then add a dise from source.
• if the souEce stack is empty, then add a disc from the destination.
• if the top clement of the source stack is smaller than the top element of the
destinaton stack, then pop the source stack and push the clement
onto the destination stack, otherwise, do the opposite,
Step 2-Create a method name solveTOH that takes one int and three Stack
parameters named, n, 2ource, helper, and dest respectively.
Your method should contain:
OAn int variable named numofMoves that stores 2"
-1 minimum number
of moves to solve the puzzle.
•A loop that iterates for nusOfMoves times.
Inside the loop. create three if statements, each Statement should move a
disc based on the result of:% 3, where i is the cITent number of moves and
3 is the number of stacks.
樂
When 1 % 3 is equal to 1. move a dise from source to destination.
Transcribed Image Text:Question 1: In this question, you are provided with a class TowersofHanoi and you will be solving the Towers of Hanoi problem using a stack-based iterative solution. To save your time, this lab will allow you to use the Java built-in Stack class. Here's a code snippet of creating a stack that accepts integers and some of its operations: Stack<Integer> myStack myStack.push (5); // add 5 to the top oI the stack myStack.peek (); - new Stack<> (); // return the top of the stack this 7/ is actually the top method we / defined in our NumebrStack ADT. myStack.pop(); // remove and return the element at / the top of the stack Step 1-Crcate a helper mcthod named move which takes two parameters of type Stack called source and destination. Your method should check the following conditions. •if the destination stack is empty, then add a dise from source. • if the souEce stack is empty, then add a disc from the destination. • if the top clement of the source stack is smaller than the top element of the destinaton stack, then pop the source stack and push the clement onto the destination stack, otherwise, do the opposite, Step 2-Create a method name solveTOH that takes one int and three Stack parameters named, n, 2ource, helper, and dest respectively. Your method should contain: OAn int variable named numofMoves that stores 2" -1 minimum number of moves to solve the puzzle. •A loop that iterates for nusOfMoves times. Inside the loop. create three if statements, each Statement should move a disc based on the result of:% 3, where i is the cITent number of moves and 3 is the number of stacks. 樂 When 1 % 3 is equal to 1. move a dise from source to destination.
o When i % 3 is equal to 2, move a disc from the source to the helper
stack
o When i % 3 is equal to 0, move a disc in from the destination to the
helper stack.
• An if statement that swaps the destination and helper stacks.
• Print statements that print the contents of all three stacks as arrays.
With the given main method, enter the number of dises you would like to test.
Sample Outputs:
Please enter the number of disks: 3
State 1:
[3,2] [) [1)
State 2: [3] [2] [1]
State 3: [3] (2, 1] O
State 4: [] (2, 1] [3]
State 5: [1] (2] (3]
State 6:
[1] 0 [3, 2]
State 7: 00 (3, 2, 1]
Transcribed Image Text:o When i % 3 is equal to 2, move a disc from the source to the helper stack o When i % 3 is equal to 0, move a disc in from the destination to the helper stack. • An if statement that swaps the destination and helper stacks. • Print statements that print the contents of all three stacks as arrays. With the given main method, enter the number of dises you would like to test. Sample Outputs: Please enter the number of disks: 3 State 1: [3,2] [) [1) State 2: [3] [2] [1] State 3: [3] (2, 1] O State 4: [] (2, 1] [3] State 5: [1] (2] (3] State 6: [1] 0 [3, 2] State 7: 00 (3, 2, 1]
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning