Problem

How many times is the moveDisks method in Listing 1 invoked for moveDisks(5, 'A',...

How many times is the moveDisks method in Listing 1 invoked for moveDisks(5, 'A', 'B', 'C')?

LISTING 1 TowerOfHanoi.java

1 import java.util.Scanner;

2

3 public class TowerOfHanoi {

4 /** Main method */

5 public static void main(String[] args) {

6 // Create a Scanner

7 Scanner input = new Scanner(System.in);

8 System.out.print("Enter number of disks: ");

9 int n = input.nextInt();

10

11 // Find the solution recursively

12 System.out.println("The moves are:");

13 moveDisks(n, 'A', 'B', 'C');

14 }

15

16 /** The method for finding the solution to move n disks

17 from fromTower to toTower with auxTower */

18 public static void moveDisks(int n, char fromTower,

19 char toTower, char auxTower) {

20 if (n == 1) // Stopping condition

21 System.out.println("Move disk " + n + " from " +

22 fromTower + " to " + toTower);

23 else {

24 moveDisks(n - 1, fromTower, auxTower, toTower);

25 System.out.println("Move disk " + n + " from " +

26 fromTower + " to " + toTower);

27 moveDisks(n - 1, auxTower, toTower, fromTower);

28 }

29 }

30 }

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT