Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// Represents the classic Towers of Hanoi puzzle. //******************************************************************** public class TowersOfHanoi { private int totalDisks; //----------------------------------------------------------------- // Sets up the puzzle with the specified

// Represents the classic Towers of Hanoi puzzle. //********************************************************************

public class TowersOfHanoi { private int totalDisks;

//----------------------------------------------------------------- // Sets up the puzzle with the specified number of disks. //----------------------------------------------------------------- public TowersOfHanoi(int disks) { totalDisks = disks; }

//----------------------------------------------------------------- // Performs the initial call to moveTower to solve the puzzle. // Moves the disks from tower 1 to tower 3 using tower 2. //----------------------------------------------------------------- public void solve() { moveTower(totalDisks, 1, 3, 2); } //----------------------------------------------------------------- // Moves the specified number of disks from one tower to another // by moving a subtower of n-1 disks out of the way, moving one // disk, then moving the subtower back. Base case of 1 disk. //----------------------------------------------------------------- private void moveTower(int numDisks, int start, int end, int temp) { if (numDisks == 1) moveOneDisk(start, end); else { moveTower(numDisks-1, start, temp, end); moveOneDisk(start, end); moveTower(numDisks-1, temp, end, start); } }

//----------------------------------------------------------------- // Prints instructions to move one disk from the specified start // tower to the specified end tower. //----------------------------------------------------------------- private void moveOneDisk(int start, int end) { System.out.println("Move one disk from " + start + " to " + end); } }

1) Modify this code so that it counts the number of moves and reports that number as: Total number of moves = N, whatever N is depending on the number of disks.

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

Database Principles Programming And Performance

Authors: Patrick O'Neil, Elizabeth O'Neil

2nd Edition

1558605800, 978-1558605800

More Books

Students also viewed these Databases questions