Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Change the towers of Hanoi program so that it does the following. import java.util.Scanner; public class Towers { public static void doTowers( int n, //
Change the towers of Hanoi program so that it does the following.
import java.util.Scanner; public class Towers { public static void doTowers( int n, // Number of rings to move int startPeg, // Peg containing rings to move int auxPeg, // Peg holding rings temporarily int endPeg ) // Peg receiving rings being moved { if (n == 1) // Base case - Move one ring System.out.println("Move ring " + n + " from peg " + startPeg + " to peg " + endPeg); else { // Move n - 1 rings from starting peg to auxiliary peg doTowers(n - 1, startPeg, endPeg, auxPeg); // Move nth ring from starting peg to ending peg System.out.println("Move ring " + n + " from peg " + startPeg + " to peg " + endPeg); // Move n - 1 rings from auxiliary peg to ending peg doTowers(n - 1, auxPeg, startPeg, endPeg); } } public static void main(String[] args) { Scanner conIn = new Scanner(System.in); // Number of rings on starting peg. int n; System.out.print("Input the number of rings: "); if (conIn.hasNextInt()) n = conIn.nextInt(); else { System.out.println("Error: you must enter an integer."); System.out.println("Terminating program."); return; } if (n = 1."); System.out.println("Terminating program."); return; } System.out.println("Towers of Hanoi with " + n + " rings "); doTowers(n, 1, 2, 3); } }2. Change the Towers of Hanoi program so that it does the following: a. Counts the number of ring moves and prints that out instead of the sequence of moves. Use a static variable count of type int to hold the number of moves. Repeatedly prompts the user for the number of rings and reports the results, until the user enters a number less than 0. b. 2. Change the Towers of Hanoi program so that it does the following: a. Counts the number of ring moves and prints that out instead of the sequence of moves. Use a static variable count of type int to hold the number of moves. Repeatedly prompts the user for the number of rings and reports the results, until the user enters a number less than 0. b
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started